0

I have tried a lot to find help on google but i couldn't find anything related to my problem.I am facing a severe problem.I am using a JComboBox in my java program it showed only when I run the program first time but after that first time it's not showing drop-down arrow.I Haven't used any removeAll(); or any kind of remove(); methods. Any help would be appreciated as I Have seen a lot people are suffering from the same problem.`class GPACalculator{ JFrame frame; JLabel selection; JComboBox sub; Font f1; JTextField nameText; JButton enter;

public GPACalculator() {
    frame = new JFrame("GPA Calculator---COMSATS Institute of Information Technology");
    frame.setSize(720, 640);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ImageIcon head = new ImageIcon("images/Header.jpg");
    JLabel header = new JLabel(head);
    header.setSize(720,90);
    header.setLocation(0, 0);

    ImageIcon log = new ImageIcon("images/Logo.png");
    JLabel logo = new JLabel(log);
    logo.setSize(300,300);
    logo.setLocation(480, 400);

    selection = new JLabel("Select Number Of Subjects And Press Enter");
    f1 = new Font("Gabriola",Font.BOLD,30);
    selection.setFont(f1);
    selection.setLocation(10, 150);
    selection.setSize(800, 50);

    String[] subject = {"1","2","3","4","5"};
    sub = new JComboBox<String>(subject);
    sub.setBounds(10, 200, 300, 50);

    Container c = frame.getContentPane();
    c.setLayout(null);
    c.setBackground(new Color(176,196,222));
    c.add(header,BorderLayout.CENTER);
    c.add(logo);
    c.add(selection);
    c.add(sub);
}

public static void main(String[] args){
    new GPACalculator();
}

} `

Usman Anwar
  • 371
  • 6
  • 18

1 Answers1

2

Here's your problem:

c.setLayout(null);

Avoid using null layouts and instead use proper layout managers and your code will likely work well.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • How to use proper Layout Mangers as i am new to java. – Usman Anwar Dec 02 '15 at 18:29
  • @ChaudaryUsman See this tutorial: [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Lukas Rotter Dec 02 '15 at 18:35
  • Thanks a lot it was my first question to any online forum and this experience was awsome :-D – Usman Anwar Dec 02 '15 at 18:38
  • @LuxxMiner using FlowLayout() first worked but after running first time it's again the same problem. – Usman Anwar Dec 02 '15 at 18:42
  • @ChaudaryUsman Try calling `frame.setVisible(true);` **after** you added all components (i.e. just at the end of the constructor) – Lukas Rotter Dec 02 '15 at 18:44
  • @LuxxMiner Thanks but what if i want to place my combobox and button at a specific place as after using flowlayout it put my button at the top – Usman Anwar Dec 02 '15 at 18:49
  • @ChaudaryUsman At what position do you want it to be? (Then I could suggest what layout manager to use). Also see [this example](http://stackoverflow.com/a/5630271/4857909) of nested layouts for some ideas. – Lukas Rotter Dec 02 '15 at 18:51
  • @LuxxMiner Let me try I'll let you know if it didn't worked. By the way thanks a lot for your help. – Usman Anwar Dec 02 '15 at 18:55