Search code examples
javajframejpaneljtextfieldjcombobox

adjust JTextField with JComboBox


Please some one can told me why I don't have the JTextField in the same line with my JComboBox ? I need to have like that:

myJComboBox1      JTextField1
                  JTextField2
myJComboBox2      JTextField1
                  JTextField2

following this example

public class DisplayPanel extends JFrame {

    private JComboBox[] box;
JTextField[] field1, field2;

public DisplayPanel(){
    super(BorderLayoutTest.class.getName());
    setTitle("Simulation");
    setSize(1000,500);
    setResizable(false);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    createComponents();
    initComponents();
}

private void createComponents(){

    box = new JComboBox[3];
    field1 = new JTextField[4];
    field2 = new JTextField[5];     
}



private void initComponents(){
     setLayout(new GridLayout(0, 2));

     for(int i = 0; i < 3; i++) {
         JPanel panel = new JPanel(new BorderLayout());
         box[i] =new JComboBox<>(new String[] { "field1", "field2"});
         panel.add(box[i], BorderLayout.NORTH);
         add(panel);
         add(createPanelWithTextFields(panel));
         box[i].setSelectedIndex(-1);
         box[i].addActionListener(new CustomActionListener(box[i]));
        }

}

private Component createPanelWithTextFields(JPanel panel) {
  //need to keep the same layout as JComboBox
    panel.setLayout(new GridLayout(0, 1));

    for(int x=0; x<4; x++){
        field1[x] = new JTextField("field1 Name " + (x+1));
        field1[x].setVisible(false);   
        panel.add(field1[x]);
    }

    for(int x=0; x<5; x++){
        field2[x] = new JTextField("field2 Name " + (x+1));
        field2[x].setVisible(false);     
        panel.add(field2[x]);
    }

    return panel;
}


class CustomActionListener implements ActionListener {
    JComboBox b;

    public CustomActionListener(JComboBox u) {
        super();
        this.b = u;
    }

    public void actionPerformed(ActionEvent e) {
        int numChosen = b.getSelectedIndex() + 1;

        switch (numChosen){
        case 1:
            for(int x=0; x<4; x++)
                field1[x].setVisible(true);
            break;
        case 2:
            for(int x=0; x<5; x++)
                field2[x].setVisible(true);
            break;
        }

    }

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override public void run() {
            new DisplayPanel().setVisible(true);
        }
    });
    }

Solution

  • you are assigning new textfield to textfield array.but it contain only 4 textfields.but you call assigning more than 4 times.so what happening is last rextfield references get reassigning and you can't use them again.at the end of the the loop your field array contain reference to textfields of last panel.and that's why your see textfields on last panel even you select from combobox1.

    how to fix ?

    change this

     field1 = new JTextField[4];
    

    to this

    field1 = new JTextField[4 * 3];
    

    and then you don't need to reassign jtextfields .you have 3 panels and you have 4 textfields for each panel.

    same for field2

    here is an example .

    public class DisplayPanel extends JFrame {
    
        private JComboBox[] box;
        JTextField[] field1, field2;
        Color col[] = {Color.red, Color.GREEN, Color.blue};
        int i = 0;
        int counter = 0;
        private int boxcount;
        int field1counter = 0;
        int field2counter = 0;
    
        public DisplayPanel() {
            //super(BorderLayoutTest.class.getName());
            setTitle("Simulation");
            setSize(1000, 500);
            //setResizable(false);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            createComponents();
            initComponents();
        }
    
        private void createComponents() {
    
            boxcount = 3;
            box = new JComboBox[1 * boxcount];
            field1 = new JTextField[4 * boxcount];
            field2 = new JTextField[5 * boxcount];
        }
    
        private void initComponents() {
            setLayout(new GridLayout(0, 2));
    
            for (int i = 0; i < 3; i++) {
                JPanel panel = new JPanel(new BorderLayout());
                box[i] = new JComboBox<>(new String[]{"field1", "field2"});
                panel.add(box[i], BorderLayout.NORTH);
                add(panel);
                add(createPanelWithTextFields(panel));
                box[i].setSelectedIndex(-1);
                box[i].addActionListener(new CustomActionListener());
            }
    
        }
    
        private Component createPanelWithTextFields(JPanel panelc) {
    
            JPanel panel = new JPanel(new GridLayout(0, 1));
            panel.setBackground(col[i]);
            System.out.println("......................");
            for (int x = 0; x < 4; x++) {
                System.out.println("iterating .." + (field1counter) + "  counter  " + counter);
                field1[field1counter] = new JTextField("field1 Name " + (x + 1));
                field1[field1counter].setVisible(false);
                panel.add(field1[field1counter]);
                field1counter++;
            }
    
            for (int x = 0; x < 5; x++) {
                field2[field2counter] = new JTextField("field2 Name " + (x + 1));
                field2[field2counter].setVisible(false);
                panel.add(field2[field2counter]);
                field2counter++;
            }
            i++;
            counter++;
            return panel;
        }
    
        class CustomActionListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                JComboBox b = (JComboBox) e.getSource();
                int comboidenty = 0;
                for (int k = 0; k < box.length; k++) {
                    if (box[k] == b) {
                        break;
                    }
                    comboidenty++;
                }
                System.out.println(((JPanel) (b.getParent())).getBackground());
                int numChosen = b.getSelectedIndex() + 1;
    
                System.out.println("hi " + comboidenty);
                switch (numChosen) {
                    case 1:
                        for (int x = 0; x < 4; x++) {
                            System.out.println("field1  " + (comboidenty * 4 + x));
                            field1[comboidenty * 4 + x].setVisible(true);
                        }
                        break;
                    case 2:
                        for (int x = 0; x < 5; x++) {
                            System.out.println("field2  " + (comboidenty * 5 + x));
                            field2[comboidenty * 5 + x].setVisible(true);
                        }
                        break;
                }
    
            }
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DisplayPanel().setVisible(true);
                }
            });
    
        }
    }