Search code examples
javaswingjtextfieldgridbaglayoutsettext

setText(String t) of JTextField cannot change the content of the textfield


The Picture show the problem setting dialog. The dialog's first JTextfield works fine with setText(String t) method when change the first JComboBox selected Item, but the second JTextfield's setText(String t) method doesn't change the content of itself's when change the second JComboBox seleted item.

The source's code is attached below. The two JTextField are construct in the same way. But I don't know why the second one's setText(String) method doesn't work.

package ztio.gui.updateGUI;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;



public class SimpleSettingDialog extends JDialog {
    private final int MAX_THRES_NUM = 10;
    private final int MAX_VALUE_THRES_NUM = 10;
    private JPanel dialogPane;
    private JComboBox testCaseCombo;
    private JComboBox testCaseItemCombo;
    private JComboBox[] judgeOperationComboArray = new JComboBox[MAX_THRES_NUM];
    private JComboBox[] thresTypeComboArray = new JComboBox[MAX_THRES_NUM];
    private String[] judgeOperationStrings;
    private String[] thresTypeStrings;
    private List<JButton> testBtnList;
    private JTextField showTargetChoice;
    private JTextField showTestCase;


    public SimpleSettingDialog(JFrame parent, boolean isModal) {
        // TODO Auto-generated constructor stub
        super(parent, isModal);
        String[] judges = {">", "<", "="};
        String[] types = {"int", "float", "string"};
        judgeOperationStrings = judges;
        thresTypeStrings = types;


        dialogPane = (JPanel) getContentPane();
        dialogPane.setLayout(new GridBagLayout());
        TitledBorder titled = BorderFactory.createTitledBorder("Config Thresholds");
        dialogPane.setBorder(titled);

        JPanel testCasePanel = createTestCasePanel();
         GridBagConstraints c = new GridBagConstraints();
         c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        dialogPane.add(testCasePanel, c);
        c.fill = GridBagConstraints.BOTH;
        c.ipady = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;

        JPanel testItemPanel = createTestCaseItemPanel();

        JPanel debug = new JPanel();
        dialogPane.add(debug, c);
    }

    private JPanel createThresListPanel() {
        JPanel thresListPanel = new JPanel();
        thresListPanel.setLayout(new BoxLayout(thresListPanel, BoxLayout.Y_AXIS));
        for(int i = 0; i < 2; ++i){
            JPanel thresPanel = createThresPanel(i);
            thresListPanel.add(thresPanel);
        }
        return thresListPanel;
    }

    private JPanel createThresPanel(int i) {
        // TODO Auto-generated method stub
        JPanel thresPanel = new JPanel(new GridBagLayout());
        TitledBorder titled = BorderFactory.createTitledBorder("The " + (((i+1)==1)?"1st":((i+1)==2?"2nd":((i+1==3?"3rd":((i+1)+"th"))))) + " Threshold" );
        thresPanel.setBorder(titled);
        JComboBox thresTypeCombo = new JComboBox(thresTypeStrings);
        thresTypeComboArray[i] = thresTypeCombo;
        thresTypeCombo.setSelectedItem("string");
        JComboBox judgeOperationCombo = new JComboBox(judgeOperationStrings);
        judgeOperationComboArray[i] = judgeOperationCombo;
        judgeOperationCombo.setSelectedItem("=");
        JLabel label1 = new JLabel("Threshold's type:");
        JLabel label2 = new JLabel("Judge Operation:");

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        thresPanel.add(label1, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        thresPanel.add(thresTypeCombo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        thresPanel.add(label2, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 1;
        thresPanel.add(judgeOperationCombo, c);

        return thresPanel;
    }

    private JPanel createTestCaseItemPanel() {
        // TODO Auto-generated method stub
        String[] testItemNameArray = {"volume", "brightness"};
        testCaseItemCombo = new JComboBox(testItemNameArray);

        testCaseItemCombo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JComboBox cb = (JComboBox)e.getSource();
                String caseItemName = (String)cb.getSelectedItem();
                String caseName = (String)testCaseCombo.getSelectedItem();
                String targetString = caseName + "->" + caseItemName;
                System.out.println(targetString);
                System.out.println("target text field: " + showTargetChoice.getText());
                showTargetChoice.setText(targetString);
            }
        });

        TitledBorder titled;

        titled = BorderFactory.createTitledBorder("Test Case Item");
        JPanel comp = new JPanel(new GridBagLayout());
        comp.setBorder(titled);

        JButton btnAddTestCaseItem = new JButton("ADD");
        btnAddTestCaseItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });

        JButton btnDelTestCaseItem = new JButton("DEL");
        btnDelTestCaseItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });



         GridBagConstraints c = new GridBagConstraints();
         c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        comp.add(testCaseItemCombo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        comp.add(btnAddTestCaseItem, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridy = 0;
        comp.add(btnDelTestCaseItem, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 3;
        c.gridy = 0;
        comp.add(new JPanel(), c);

        //Row 2
        JLabel labelRegexp = new JLabel("target string:");
        showTargetChoice = new JTextField();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        comp.add(labelRegexp, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 1;
        comp.add(showTargetChoice, c);

        JPanel thresListPanel = createThresListPanel();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 2;
        comp.add(thresListPanel, c);

        return comp;
    }

    private JPanel createTestCasePanel() {
        // TODO Auto-generated method stub
        String[] testCaseArray = {"Light", "Voice"};
        testCaseCombo = new JComboBox(testCaseArray);
        testCaseCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JComboBox cb = (JComboBox)e.getSource();
                String caseName = (String)cb.getSelectedItem();
                System.out.println(caseName);
                showTestCase.setText(caseName);
            }
        });
        TitledBorder titled;

        titled = BorderFactory.createTitledBorder("Test Case");
        JPanel comp = new JPanel(new GridBagLayout());
        comp.setBorder(titled);

        JButton btnAddTestCase = new JButton("ADD");
        btnAddTestCase.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
            }
        });

        JButton btnDelTestCase = new JButton("DEL");
        btnDelTestCase.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });

         GridBagConstraints c = new GridBagConstraints();
         c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        comp.add(testCaseCombo, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 1;
        c.gridy = 0;
        comp.add(btnAddTestCase, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 2;
        c.gridy = 0;
        comp.add(btnDelTestCase, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 1;
        c.gridx = 3;
        c.gridy = 0;
        comp.add(new JPanel(), c);

        JLabel label = new JLabel("selected test case:");
        showTestCase = new JTextField();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        comp.add(label, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 1.0;
        c.gridwidth = 3;
        c.gridx = 1;
        c.gridy = 1;
        comp.add(showTestCase, c);

        JPanel testCaseItemPanel = createTestCaseItemPanel();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.0;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 2;
        comp.add(testCaseItemPanel, c);



        return comp;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("SimpleSettingDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn = new JButton("open setting dialog");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                SimpleSettingDialog simpleSettingDialog = new SimpleSettingDialog(frame, true);
                simpleSettingDialog.pack();
                simpleSettingDialog.setVisible(true);
            }
        });
        frame.getContentPane().add(btn);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });

    }
}

Solution

  • createTestCaseItemPanel() is executed twice. Once it is executed from createTestCasePanel(). The second time at this line in the constructor:

    JPanel testItemPanel = createTestCaseItemPanel();
    

    This second call creates a local variable testItemPanel which is not used. This however overrides some of the members, one of them is showTargetChoice, which now points to a field that is not visible.

    To fix the issue just get rid of that second call.