Na ovo nisam obraćao pažnju. Makao sam jTextArea.add, ali onda kad se doda scrollpane samo stoji kao text editor koji nema vertikalni scrollbar i na njega ne mogu iskucat ništa.. e sad kad prosleđujem argument jtextarea-e kontsruktoru jscrollpane-a, ako proslijedim samo objekat (jTextArea) onda dobijem samo text editor koji sam gore naveo, a ako proslijedim getter (getJTextArea()), onda dobijem isto kao da sam prvo dodao instancu jTextArea, pa onda jScrollPane. Evo ti kod cijelog programa:
Code:package gui;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
public class MainWindow {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="10,10"
private JPanel jContentPane = null;
private JDialog aboutDialog = null; // @jve:decl-index=0:visual-constraint="457,33"
private JPanel aboutContentPane = null;
private JLabel aboutVersionLabel = null;
private JTextArea jTextArea = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JTextArea jTextArea1 = null;
private JPanel jPanel2 = null;
private JButton jButtonTrans = null;
private JPanel jPanel3 = null;
private JButton jButtonClip = null;
private JButton jButtonAbout = null;
private JButton jButtonExit = null;
private JPanel jPanel4 = null;
private JCheckBox jCheckBoxExtraZero = null;
private JCheckBox jCheckBoxLetters = null;
private JCheckBox jCheckBoxWords = null;
private JPanel jPanel5 = null;
private JLabel jLabel = null;
private boolean extrazero = true;
private boolean sepletters = false;
private boolean sepwords = false;
private JButton jButton = null;
private JPanel jPanel6 = null;
private JButton jButton2 = null;
private int enter = 1;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
public JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jFrame.setSize(430, 516);
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("Binary Translator");
jFrame.setResizable(false);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(UnsupportedLookAndFeelException ulfe) {
ulfe.printStackTrace();
} catch(IllegalAccessException iae) {
iae.printStackTrace();
} catch(InstantiationException ie) {
ie.printStackTrace();
} catch(ClassNotFoundException nce) {
nce.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(jFrame);
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
jContentPane.add(getJPanel2(), BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes aboutDialog
*
* @return javax.swing.JDialog
*/
private JDialog getAboutDialog() {
if (aboutDialog == null) {
aboutDialog = new JDialog(getJFrame(), true);
aboutDialog.setTitle("About");
aboutDialog.setSize(new Dimension(365, 132));
aboutDialog.setContentPane(getAboutContentPane());
aboutDialog.setResizable(false);
}
return aboutDialog;
}
/**
* This method initializes aboutContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getAboutContentPane() {
if (aboutContentPane == null) {
aboutContentPane = new JPanel();
aboutContentPane.setLayout(new BorderLayout());
aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
aboutContentPane.add(getJPanel6(), BorderLayout.SOUTH);
}
return aboutContentPane;
}
/**
* This method initializes aboutVersionLabel
*
* @return javax.swing.JLabel
*/
private JLabel getAboutVersionLabel() {
if (aboutVersionLabel == null) {
aboutVersionLabel = new JLabel();
aboutVersionLabel.setText("Binary Translator powered by Goran Pejovic");
aboutVersionLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
return aboutVersionLabel;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setPreferredSize(new Dimension(400, 150));
jTextArea.setLineWrap(true);
jTextArea.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jTextArea.setWrapStyleWord(true);
// jTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
// public void keyTyped(java.awt.event.KeyEvent e) {
// if(e.paramString().contains("Enter")) {
// jTextArea.setRows(enter++);
// }
// }
// });
}
return jTextArea;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.add(getJTextArea(), null);
JScrollPane scrollPane = new JScrollPane(jTextArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBorder(BorderFactory.createTitledBorder("Enter Text:"));
scrollPane.setPreferredSize(new Dimension(400,150));
jPanel.add(scrollPane);
}
return jPanel;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new FlowLayout());
jPanel1.setPreferredSize(new Dimension(410, 160));
// jPanel1.add(getJTextArea1(), null);
JScrollPane scrollingResult = new JScrollPane(getJTextArea1());
scrollingResult.setBorder(BorderFactory.createTitledBorder("Binary:"));
scrollingResult.setPreferredSize(new Dimension(400,150));
jPanel1.add(scrollingResult);
}
return jPanel1;
}
/**
* This method initializes jTextArea1
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea1() {
if (jTextArea1 == null) {
jTextArea1 = new JTextArea();
jTextArea1.setPreferredSize(new Dimension(400, 150));
jTextArea1.setBackground(Color.black);
jTextArea1.setForeground(Color.green);
jTextArea1.setBorder(BorderFactory.createBevelBorder(1));
jTextArea1.setEditable(false);
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea1.setFont(new Font("Monospaced", Font.PLAIN, 14));
}
return jTextArea1;
}
/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new BorderLayout());
jPanel2.add(getJPanel3(), BorderLayout.SOUTH);
jPanel2.add(getJPanel4(), BorderLayout.NORTH);
jPanel2.add(getJPanel5(), BorderLayout.CENTER);
}
return jPanel2;
}
/**
* This method initializes jButtonTrans
*
* @return javax.swing.JButton
*/
private JButton getJButtonTrans() {
if (jButtonTrans == null) {
jButtonTrans = new JButton();
jButtonTrans.setName("jButton");
jButtonTrans.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jButtonTrans.setText("Translate");
jButtonTrans.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Translate t = new Translate();
t.start();
}
});
}
return jButtonTrans;
}
/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setLayout(new FlowLayout());
jPanel3.add(getJButtonTrans(), null);
jPanel3.add(getJButtonClip(), null);
jPanel3.add(getJButton(), null);
jPanel3.add(getJButtonAbout(), null);
jPanel3.add(getJButtonExit(), null);
}
return jPanel3;
}
/**
* This method initializes jButtonClip
*
* @return javax.swing.JButton
*/
private JButton getJButtonClip() {
if (jButtonClip == null) {
jButtonClip = new JButton();
jButtonClip.setText("Copy");
jButtonClip.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jButtonClip.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea1.selectAll();
jTextArea1.copy();
}
});
}
return jButtonClip;
}
/**
* This method initializes jButtonAbout
*
* @return javax.swing.JButton
*/
private JButton getJButtonAbout() {
if (jButtonAbout == null) {
jButtonAbout = new JButton();
jButtonAbout.setText("About");
jButtonAbout.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jButtonAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JDialog aboutDialog = getAboutDialog();
aboutDialog.pack();
Point loc = getJFrame().getLocation();
loc.translate(20, 20);
aboutDialog.setLocation(loc);
aboutDialog.setVisible(true);
}
});
}
return jButtonAbout;
}
/**
* This method initializes jButtonExit
*
* @return javax.swing.JButton
*/
private JButton getJButtonExit() {
if (jButtonExit == null) {
jButtonExit = new JButton();
jButtonExit.setText("Exit");
jButtonExit.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jButtonExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jButtonExit;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Clear");
jButton.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea1.setText("");
}
});
}
return jButton;
}
/**
* This method initializes jPanel4
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel4() {
if (jPanel4 == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
jPanel4 = new JPanel();
jPanel4.setLayout(new FlowLayout());
jPanel4.add(getJCheckBoxExtraZero(), null);
jPanel4.add(getJCheckBoxLetters(), null);
jPanel4.add(getJCheckBoxWords(), null);
}
return jPanel4;
}
/**
* This method initializes jCheckBoxExtraZero
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJCheckBoxExtraZero() {
if (jCheckBoxExtraZero == null) {
jCheckBoxExtraZero = new JCheckBox();
jCheckBoxExtraZero.setText("Remove leading zero");
jCheckBoxExtraZero.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jCheckBoxExtraZero.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if(e.getStateChange() == 1) {
extrazero = false;
} else
extrazero = true;
}
});
}
return jCheckBoxExtraZero;
}
/**
* This method initializes jCheckBoxLetters
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJCheckBoxLetters() {
if (jCheckBoxLetters == null) {
jCheckBoxLetters = new JCheckBox();
jCheckBoxLetters.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jCheckBoxLetters.setText("Separated letters");
jCheckBoxLetters.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if(e.getStateChange() == 1)
sepletters = true;
else
sepletters = false;
}
});
}
return jCheckBoxLetters;
}
/**
* This method initializes jCheckBoxWords
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getJCheckBoxWords() {
if (jCheckBoxWords == null) {
jCheckBoxWords = new JCheckBox();
jCheckBoxWords.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jCheckBoxWords.setText("Separated words");
jCheckBoxWords.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if(e.getStateChange() == 1)
sepwords = true;
else
sepwords = false;
}
});
}
return jCheckBoxWords;
}
/**
* This method initializes jPanel5
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel5() {
if (jPanel5 == null) {
jLabel = new JLabel();
jLabel.setText("
[email protected]");
jLabel.setForeground(Color.blue);
jLabel.setFont(new Font("Times New Roman", Font.BOLD, 45));
// jLabel.setIcon(new ImageIcon(getClass().getResource("/images.jpg")));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setPreferredSize(new Dimension(413, 71));
jPanel5 = new JPanel();
jPanel5.setLayout(new BorderLayout());
jPanel5.add(jLabel, BorderLayout.NORTH);
}
return jPanel5;
}
/**
* This method initializes jPanel6
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel6() {
if (jPanel6 == null) {
jPanel6 = new JPanel();
jPanel6.setLayout(new FlowLayout());
jPanel6.add(getJButton2(), null);
}
return jPanel6;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("Exit");
jButton2.setFont(new Font("Lucida Grande", Font.PLAIN, 12));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
aboutDialog.setVisible(false);
}
});
}
return jButton2;
}
private class Translate extends Thread {
public void run() {
if(!extrazero) {
if(sepletters)
translateWithLetterSpaces();
else if(sepwords)
translateWithWordSpaces();
else
translateWithoutSpaces();
}
else
translateWithZero();
}
public synchronized void translateWithLetterSpaces() {
StringBuffer sb = new StringBuffer(jTextArea.getText());
String bin = "";
for(int i = 0; i < sb.length(); i++) {
if(sb.charAt(i) == ' ') {
bin = bin + " ";
} else
bin = bin + Integer.toBinaryString(sb.charAt(i)) + " ";
}
jTextArea1.setText(bin);
}
public synchronized void translateWithWordSpaces() {
StringBuffer sb = new StringBuffer(jTextArea.getText());
String bin = "";
for(int i = 0; i < sb.length(); i++) {
if(sb.charAt(i) == ' ') {
bin = bin + " ";
} else
bin = bin + Integer.toBinaryString(sb.charAt(i));
}
jTextArea1.setText(bin);
}
public synchronized void translateWithoutSpaces() {
StringBuffer sb = new StringBuffer(jTextArea.getText());
String bin = "";
for(int i = 0; i < sb.length(); i++) {
bin = bin + Integer.toBinaryString(sb.charAt(i));
}
jTextArea1.setText(bin);
}
public synchronized void translateWithZero() {
StringBuffer sb = new StringBuffer(jTextArea.getText());
String bin = "";
if(sepletters) {
for(int i = 0; i < sb.length(); i++) {
bin = bin + "0" + Integer.toBinaryString(sb.charAt(i)) + " ";
}
} else if(sepwords) {
for(int i = 0; i < sb.length(); i++) {
if(sb.charAt(i) == ' ') {
bin = bin + " ";
} else
bin = bin + "0" + Integer.toBinaryString(sb.charAt(i));
}
} else if(extrazero){
for(int i = 0; i < sb.length(); i++) {
bin = bin + "0" + Integer.toBinaryString(sb.charAt(i));
}
}
jTextArea1.setText(bin);
}
}
}