user3746155
user3746155

Reputation: 45

Getting text from a JTextArea using a JButton and a JTabbedPane

Newbie here trying to make a simple GUI with JTabbedPane. I've looked through a lot of examples but have been unable to find a solution. Basically, I'm trying to print out a String to a JTextArea. While it seems very simple, I have been unable to get everything to work together. I understand the difference between local and global variables, but I think that is where my problem lies. Any guidance would be greatly appreciated. *Please note that we are unable to use a layout manager for this project.

The code below represents part of the tab that has the JButton and JTextArea.

    //Text area that shows details. Scrolls.
        JTextArea areaDeets = new JTextArea(); 
        areaDeets.setBounds(65, 300, 250, 300 ); 
        areaDeets.setText(""); 
        JScrollPane scroll = new JScrollPane (areaDeets); 
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        panel2.add(areaDeets); 
        panel2.add(scroll); 
        areaDeets.addActionListener(new StopTest()); 

   //Stop button--stops tests when pressed. 
        JButton stop = new JButton("Stop"); 
        stop.setBounds(215, 650, 100, 40); 
        panel2.add(stop); 
        stop.addActionListener(new StopTest()); 

The code below is the method that calls the ActionListener.

//Panel 1 - Stop, shows that the test has been stopped
    static class StopTest implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String stop = "The test has been stopped"; 
            areaDeets.setText(stop); 
            panel2.repaint(); 

        }
    }

Edit: Code does not compile. Eclipse says that I cannot call addActionListener on a JTextField.

Upvotes: 1

Views: 91

Answers (2)

dawww
dawww

Reputation: 371

You can pass the reference of the JTextArea to the constructor of the ActionListener:

  public class StopTest implements ActionListener {

            private JTextArea area;

            public StopTest(JTextArea area) {
                this.area = area;   
            }

            public void actionPerformed(ActionEvent e) {
                String stop = "The test has been stopped"; 
                area.setText(stop); 
            }
   }

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

  1. One possible solution (guessing here due to limited information): don't make the StopTest class static. Rather make it a private inner non-static class or a stand-alone class. This will allow it access to non-static fields of your outer class.
  2. Don't add an ActionListener to your JTextArea as this is not allowed and has no real meaning, since JTextAreas allow returns to be entered, and use this to start a new line in the JTextArea.

Other unrelated recommendations:

  • Also, as a general rule, you should avoid use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.
  • Much better would be to use nested JPanels, each using its own layout manager, and then calling pack() on your JFrame after adding all components but prior to displaying it.
  • Never call setBounds(), setSize(), setPreferredSize() or any similar call on a JTextArea that goes inside of a JScrollPane as it will prevent the JScrollPane from working correctly within your JScrollPane due to your setting bounds. This will prevent the JTextArea from expanding when more lines are added, sometimes preventing display of the scrollbars, or if their displayed, preventing them from working properly. Better to set the JTextArea's viewable columns and rows via one of its constructors that has int parameters.
  • There's no need to call repaint() after setting the text of a JTextComponent such as a JTextArea, since the textarea's model will notify its view (the part that is rendered on the GUI) of changes, and the view will then automatically call repaint itself.

Upvotes: 2

Related Questions