Reputation: 13
I'm new in Java programming language and I try to write a simple code
public class TextPanel extends JPanel {
private JTextArea textArea;
public TextPanel() {
textArea = new JTextArea();
setLayout(new BorderLayout());
setVisible(true);
add(new JScrollPane(textArea), BorderLayout.CENTER);
}
public String getTextAreaText() {
String text = textArea.getText();
return text;
}
}
And I added an action listener to star button (startBtn
) but when I run the program nothing is shown in console even if i put a System.out.println(textPanel.getTextAreaText())
in actionPerformed()
method (code below).
public class Toolbar extends JPanel {
private JButton startBtn;
private JButton stopBtn;
private TextPanel textPanel;
public Toolbar() {
startBtn = new JButton("Start");
stopBtn = new JButton("Stop");
textPanel = new TextPanel();
setLayout(new FlowLayout(FlowLayout.LEFT));
add(startBtn);
add(stopBtn);
startBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(textPanel.getTextAreaText());
}
});
}
}
I need help to fix this.
Upvotes: 0
Views: 67
Reputation: 285440
For decent resources, please check out the info section of your Java and Swing tags:
So consider adding a main method that creates your JFrame and adds components to the JFrame, something like:
private static void createAndShowGui() {
JFrame frame = new JFrame("My JFrame");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// add your components to your JFrame here
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
// the main method which Java uses as the starting point for your program
public static void main(String[] args) {
// let's call our Swing GUI in a thread-safe manner
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
Edit
Your code also shows possible variable shadowing. You create a TextPanel object inside of your Toolbar class, but you add this TextPanel to nothing. This suggests that you may have a TextPanel object elsewhere that is being displayed (and we can only guess because it appears that you're not showing enough code for us to know for sure). If so, then pressing your start button will get the text from a non-displayed Toolbar's JTextArea. Instead consider passing a TextPanel reference into Toolbar, something like this:
class Toolbar extends JPanel {
private JButton startBtn;
private JButton stopBtn;
private TextPanel textPanel;
public Toolbar() {
startBtn = new JButton("Start");
stopBtn = new JButton("Stop");
// textPanel = new TextPanel(); // *** note change
setLayout(new FlowLayout(FlowLayout.LEFT));
add(startBtn);
add(stopBtn);
startBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
if (textPanel != null) {
System.out.println(textPanel.getTextAreaText());
}
}
});
}
// **** note this method ****
public void setTextPanel(TextPanel textPanel) {
this.textPanel = textPanel;
}
}
And then when creating your objects, pass in your reference:
private static void createAndShowGui() {
Toolbar toolBar = new Toolbar();
TextPanel textPanel = new TextPanel();
toolBar.setTextPanel(textPanel); // ****** passing in my reference *******
JFrame frame = new JFrame("Add New Lines");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(textPanel);
frame.getContentPane().add(toolBar, BorderLayout.PAGE_START);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
Upvotes: 2