简体   繁体   中英

Action listener does not seem to be firing

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.

  1. Java programs require a public static main method to run. Your code does not have this, and so there is no start point for your program.
  2. Swing GUI's require that components be placed in a top-level window such as a JFrame and that this window be displayed for the components to be seen. Your code does not have this.
  3. You will want to read the Java and Swing tutorials as all of this is very well explained there, complete with sample code.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM