Search code examples
javaswingjtextfieldjcombobox

Effect on JTextfield due to JComboBox


I am using JCombobox and just below that there is a panel which contains JTextFields. Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself. I don't know what to do. I am unable to post a pic of the problem here because i am a new user and my reputation is below 10.

public class MainClass {
public static void main(String args[]){
    Form form = new Form();
    int width = 400;
    int height = 400;
    form.setSize(width, height);
    form.setVisible(true);
    form.setTitle("Create Network");
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    form.setLocation((screenSize.width-width)/2, (screenSize.height-height)/2);
}
}

This is the form class

public class Form extends JFrame {

private JLabel ipAddress, networkTopo, numNodes;
private JTextField nodes;
private JButton addIp;
private JButton removeIp;
private JButton next, back;
private JPanel jPanel, jPanel1, jPanel2, commonPanel;
private JComboBox<String> dropDown;

private String[] topologies = { "Grid", "Diagnol Grid", "Bus", "Ring",
        "Star" };

public Form() {
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setResizable(false);
    this.getContentPane().setBackground(Color.WHITE);

    // add(Box.createRigidArea(new Dimension(0,10)));
    GridLayout commonPanelLayout = new GridLayout(0, 2);
    commonPanelLayout.setVgap(10);
    commonPanel = new JPanel(commonPanelLayout);
    commonPanel.setVisible(true);
    commonPanel.setAlignmentX(commonPanel.LEFT_ALIGNMENT);
    commonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    commonPanel.setBackground(Color.white);

    numNodes = new JLabel("Number of Nodes");
    commonPanel.add(numNodes);

    nodes = new JTextField(20);
    commonPanel.add(nodes);

    networkTopo = new JLabel("Network Topology");
    commonPanel.add(networkTopo);

    dropDown = new JComboBox<String>(topologies);
    dropDown.setBackground(Color.WHITE);
    commonPanel.add(dropDown);

    add(commonPanel);

    add(Box.createRigidArea(new Dimension(0, 10)));
    ipAddress = new JLabel("IP Addresses");
    ipAddress.setAlignmentX(ipAddress.LEFT_ALIGNMENT);
    ipAddress.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
    add(ipAddress);

    add(Box.createRigidArea(new Dimension(0, 10)));
    jPanel = new JPanel(new FlowLayout());
    jPanel.setVisible(true);
    jPanel.setAlignmentX(jPanel.LEFT_ALIGNMENT);
    jPanel.setBackground(Color.WHITE);
    // jPanel1.setAutoscrolls(true);
    add(jPanel);

    GridLayout layout = new GridLayout(0, 1);
    jPanel1 = new JPanel();
    layout.setVgap(10);
    // jPanel1.setBackground(Color.WHITE);
    jPanel1.setVisible(true);

    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(jPanel1);
    scroll.setAutoscrolls(true);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setPreferredSize(new Dimension(300, 150));
    jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
            Integer.MAX_VALUE));
    jPanel.add(scroll);

    jPanel2 = new JPanel(new GridLayout(0, 1, 10, 10));
    jPanel2.setBackground(Color.WHITE);
    jPanel2.setVisible(true);
    jPanel.add(jPanel2);

    addIp = new JButton("Add");
    jPanel2.add(addIp);

    removeIp = new JButton("Remove");
    jPanel2.add(removeIp);

    JPanel savePanel = new JPanel(new FlowLayout());
    savePanel.setVisible(true);
    savePanel.setAlignmentX(savePanel.LEFT_ALIGNMENT);
    savePanel.setBackground(Color.white);

    back = new JButton("Back");
    back.setAlignmentX(next.LEFT_ALIGNMENT);
    back.setEnabled(false);
    savePanel.add(back);

    next = new JButton("Next");
    next.setAlignmentX(next.LEFT_ALIGNMENT);
    savePanel.add(next);

    add(savePanel);

    AddIPEvent addIPEvent = new AddIPEvent();
    addIp.addActionListener(addIPEvent);

    RemoveIPEvent removeIPEvent = new RemoveIPEvent();
    removeIp.addActionListener(removeIPEvent);

}

public class AddIPEvent implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        JPanel subPanel = new JPanel(new FlowLayout());
        // subPanel.setBackground(Color.WHITE);
        subPanel.setVisible(true);
        JCheckBox jCheckBox = new JCheckBox();
        // jCheckBox.setBackground(Color.WHITE);
        subPanel.add(jCheckBox);

        JTextField ip = new JTextField(12);
        subPanel.add(ip);

        JTextField nodesInIp = new JTextField(6);
        nodesInIp.setVisible(false);
        subPanel.add(nodesInIp);

        jPanel1.add(subPanel);
        jPanel1.repaint();
        jPanel1.revalidate();
    }
}

public class RemoveIPEvent implements ActionListener {

    public void removeIP(Container container) {
        for (Component c : container.getComponents()) {
            if (c instanceof JCheckBox) {
                if (((JCheckBox) c).isSelected()) {
                    jPanel1.remove(container);
                    jPanel.revalidate();
                    jPanel1.repaint();
                }
            } else if (c instanceof Container) {
                removeIP((Container) c);
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        removeIP(jPanel1);
    }
}
}

After Clicking on the Add button and then clicking on the JComboBox will make portion of JTextField dissapear. Could someone pls point out the mistake?


Solution

  • Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself.

    //jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width, Integer.MAX_VALUE));
    

    Don't set the preferred size of components. The layout manager will determine the preferred size as components are added/removed. Then scrollbars will appear as required.

    jPanel1.repaint();
    jPanel1.revalidate();
    

    The order should be:

    jPanel1.revalidate(); 
    jPanel1.repaint(); 
    

    The revalidate() first invokes the layout manager before it is repainted.

    //form.setSize(width, height);
    form.pack();
    

    Again, don't try to set the size. Let the layout managers do their jobs. The pack() will size the frame based on the sizes of the components added to the frame.