jesric1029
jesric1029

Reputation: 718

Adding action listener to JOptionsPane w/ JTextFields

I am attempting to create another GUI screen for an ongoing work project. This GUI screen if triggered will open a JOptionsPane containing three JTextFields and one confirm button (that is part of the actual JOptionsPane).

I am having trouble getting the action listener on the JTextField to work. I want the textfield to pick up whatever value is entered in it when the user clicks confirm. Then through various conditions the program will continue. I've created a test class to plan and create this GUI isolated from the rest of my program so I am able to share it below. Let me also add that I apologize for the way I created this GUI. I am not very good at the visual aspect of GUI's so I used the method that was easiest for me which was losts and lots of JPanels with border layouts. May not be the best but it works :)

Here is my code:

package guitesting;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class GUITesting 

{

    public static int tester = 0;
    public static String resultString = null;
    static JTextField tRDFI = new JTextField("RDFI Number Here",30);

    public static void NOCGUI(){

        String text = "<html>"
                + "This is where the entry detail will go"+
                "<br><br>"
                +"</html>"; 

        JLabel RDFI = new JLabel("RDFI Number:");
        JLabel DDA = new JLabel("DDA:");
        JLabel TCode = new JLabel("Transaction Code:     ");



        tRDFI.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                resultString = tRDFI.getText();
            }
        });

        JTextField tDDA = new JTextField("DDA Number Here",30);
        JTextField tTCode = new JTextField("TCode Here",30);
        JLabel label = new JLabel(text);

        JPanel mainPanel = new JPanel();
        JPanel fieldPanel = new JPanel();
        JPanel textPanel = new JPanel();
        JPanel northFieldPanel = new JPanel();
        JPanel southFieldPanel = new JPanel();
        JPanel tCodePanel = new JPanel();
        JPanel RDFIPanel = new JPanel();
        JPanel DDAPanel = new JPanel();

        mainPanel.setLayout(new BorderLayout());
        fieldPanel.setLayout(new BorderLayout());
        textPanel.setLayout(new BorderLayout());
        northFieldPanel.setLayout(new BorderLayout());
        southFieldPanel.setLayout(new BorderLayout());
        tCodePanel.setLayout(new BorderLayout());
        RDFIPanel.setLayout(new BorderLayout());
        DDAPanel.setLayout(new BorderLayout());

        textPanel.add(label,BorderLayout.CENTER);
        RDFIPanel.add(RDFI,BorderLayout.WEST);
        RDFIPanel.add(tRDFI,BorderLayout.EAST);
        tCodePanel.add(TCode,BorderLayout.WEST);
        tCodePanel.add(tTCode,BorderLayout.EAST);
        DDAPanel.add(DDA,BorderLayout.WEST);
        DDAPanel.add(tDDA,BorderLayout.EAST);
        northFieldPanel.add(RDFIPanel,BorderLayout.NORTH);
        northFieldPanel.add(DDAPanel,BorderLayout.SOUTH);
        southFieldPanel.add(tCodePanel,BorderLayout.NORTH);
        fieldPanel.add(northFieldPanel,BorderLayout.NORTH);
        fieldPanel.add(southFieldPanel,BorderLayout.SOUTH);
        mainPanel.add(textPanel,BorderLayout.NORTH);
        mainPanel.add(fieldPanel,BorderLayout.SOUTH);       

        String options[] = {"Confirm"};

        int result = JOptionPane.showOptionDialog(null, mainPanel, "NOC Builder", JOptionPane.YES_OPTION, 
                JOptionPane.PLAIN_MESSAGE,null, options, options[0]);

        if(result==0 && resultString.equals("A")){
            System.out.println("pass");
        }

    }

    public static void main(String args[]){

        GUITesting.NOCGUI();



        }

}

Upvotes: 1

Views: 45

Answers (1)

martinez314
martinez314

Reputation: 12332

I don't think an ActionListener is what you want here. It will only be called if the user presses Return when the textfield has focus.

If you want to listen for any and all changes in your textfields, look into DocumentListener.

textField.getDocument().addDocumentListener(documentListener);

But why not just reference the actual textfield when the dialog returns? This might be the simplest solution. Try this:

if (result == 0) {
    System.out.println("tRDFI: " + tRDFI.getText());
    System.out.println("tDDA: " + tDDA.getText());
    System.out.println("tTCode: " + tTCode.getText());
}

Upvotes: 3

Related Questions