1

I created a GUI using java swing and in a specific situation, the JButton is unresponsive and I have to click it twice. On the click, it takes the info in the textArea and sends it to a TextParser class to be parsed. If I type more stuff in the area after, and click the evaluateButton, it doesn't respond and I have to click it again to work. Does anyone know if this is a known bug or how I can fix this?

The code for the class is as follows.

/**
 * Add the components to the GUI.
 * @param pane - the pane for the GUI
 */
public static void addComponentsToPane(Container pane) {
    pane.setLayout(new BorderLayout());
    JPanel instructionsPanel = new JPanel();
    JLabel instructions = new JLabel("Enter the email text below");
    instructionsPanel.setBackground(Color.LIGHT_GRAY);
    instructionsPanel.add(instructions);
    pane.add(instructionsPanel, BorderLayout.NORTH);

    JPanel textAreaPanel = new JPanel();
    textAreaPanel.setBackground(Color.LIGHT_GRAY);
    final JTextArea textArea = new JTextArea();
    textArea.setBackground(Color.WHITE);
    textArea.setMinimumSize(new Dimension(400,350));
    textArea.setMaximumSize(new Dimension(400,350));
    textArea.setPreferredSize(new Dimension(400,350));
    textArea.setLineWrap(true);
    Border border = BorderFactory.createLineBorder(Color.BLACK);
    textArea.setBorder(border);

    textArea.setMinimumSize(new Dimension(500, 200));
    textArea.setFont(new Font("Serif", Font.PLAIN, 16));
    textAreaPanel.add(textArea);
    pane.add(textAreaPanel, BorderLayout.CENTER);

    JPanel scoringPanel = new JPanel();
    JButton evaluateButton = new JButton("Evaluate Email");
    final JLabel scoreLabel = new JLabel("");
    JButton uploadFileBtn = new JButton("Upload File");
    JButton importTermsBtn = new JButton("Import Terms");
    scoringPanel.add(evaluateButton);
    scoringPanel.add(uploadFileBtn);
    scoringPanel.add(importTermsBtn);
    scoringPanel.add(scoreLabel);

    pane.add(scoringPanel, BorderLayout.SOUTH);

    evaluateButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                String email = textArea.getText();
                TextParser textParser = new TextParser(email);
                double score = textParser.parse();
                scoreLabel.setText(score+"");
            } catch (Exception ex) {
                System.out.println(ex);
            }

        }
    });

    uploadFileBtn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            scoreLabel.setText("Feature not yet available.");
        }
    });

    importTermsBtn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            DatabaseInput d = new DatabaseInput();
            d.main(null);
        }
    });
}

/**
 * Create the GUI and show it.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("EmailGUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setLocationRelativeTo(null);
    frame.setPreferredSize(new Dimension(500,500));
    frame.setTitle("Email Text Input");
    frame.setResizable(true);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setSize(screenSize.width, screenSize.height);
    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

My main method just calls createAndShowGUI(). I am new to StackOverflow so if I need to give more or less information in my post please let me know!

Chris Deck
  • 146
  • 2
  • 9
  • I assume from your description that of the three buttons you have, you're talking about `evaluateButton`? Are you getting an exception printed out? – Jason C Oct 30 '16 at 23:04
  • 2
    Use an `ActionListener` for the buttons – Reimeus Oct 30 '16 at 23:04
  • Btw you should be using `ActionListener`, not `MouseListener`. – Jason C Oct 30 '16 at 23:05
  • Yes I am talking about the evaluateButton, I will update my question. And I will give that a shot and change it to ActionListener! – Chris Deck Oct 30 '16 at 23:08
  • @Chris Note also that by using MouseListener, your button will incorrectly respond to middle and right clicks, and be unusable with the keyboard. So regardless of whether or not it solves your problem, ActionListener is the correct approach. – Jason C Oct 30 '16 at 23:11
  • Awesome thanks alot. ActionListener works perfectly, that was a silly mistake of mine, idk why I thought I needed MouseListener. – Chris Deck Oct 30 '16 at 23:15
  • `final JTextArea textArea = new JTextArea();` should be more like `final JTextArea textArea = new JTextArea(5,30);` to suggest a size to the layout manager. See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 31 '16 at 05:01

1 Answers1

0

As Reimeus and Jason C said in the comments, I should have been using ActionListener which works perfectly.

Chris Deck
  • 146
  • 2
  • 9