Search code examples
javaswinglayoutlayout-managerflowlayout

Java Swing components not showing


Help me! Whenever I try to start up the code below, it shows only the button on the bottom and the password field everywhere else. I want to be able to see everything, but I can't

public void setup(){
    frame = new JFrame("Votinator 3000");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    voteconfirm = new JLabel("");
    textarea = new JTextField(1);
    submit = new JButton("Submit Vote!");
    chooser = new JList(items);
    password = new JPasswordField(1);
    password.setVisible(true);
    choices = new JComboBox();
    choices.addItem("Choose");
    choices.addItem("Submit Own");
    type = new JPanel();
    type.add(textarea);
    choices.setEditable(false);
    choices.setSelectedIndex(0);
    frame.setBounds(300, 300, 400, 400);
    frame.getContentPane().add(type);
    frame.getContentPane().add(choices);
    frame.getContentPane().add(voteconfirm);
    frame.getContentPane().add(chooser);
    frame.getContentPane().add(textarea);
    frame.getContentPane().add(password,BorderLayout.CENTER);
    frame.getContentPane().add(submit,BorderLayout.SOUTH);
    frame.setVisible(true);
}

Solution

  • This

    frame.getContentPane().add(password,BorderLayout.CENTER);
    

    Will replace anything else you've added to your screen...

    This, will add the button to the bottom of the screen...

    frame.getContentPane().add(submit,BorderLayout.SOUTH);
    

    You could change the layout to a FlowLayout, that will display everything...

    enter image description here

    frame.setLayout(new FlowLayout());
    frame.setBounds(300, 300, 400, 400);
    frame.getContentPane().add(type);
    frame.getContentPane().add(choices);
    frame.getContentPane().add(voteconfirm);
    frame.getContentPane().add(chooser);
    frame.getContentPane().add(textarea);
    frame.getContentPane().add(password);
    frame.getContentPane().add(submit);
    

    But I hardly think that's what you really want.

    Take a read through

    And see if you can find one or more layouts that suit your requirements