JComboBox constructor: Is there a bug?

Hello everyone,

I have made a simple GUI which doesn't do much, but it works. That is to say, when I try to add a JComboBox, something goes terribly wrong. After several trial-and-error steps I pinned the problem down to this: If I do not invoke the JComboBox constructor (tried the ( ) and ( String[] ) arguments), everything works nicely. If I, however, try to invoke any of these 2 constructors, no part of my GUI works at all.

Appreciate any help, I have no idea on how to understand/solve this problem.

Paul Anton.

Source Code:

package top;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class MyFrame extends JFrame implements ActionListener {

/**

* Bullshit ID to keep eclipse happy

*/

private static final long serialVersionUID = 1L;

/**

* The button that sends new settings to the scrapers

*/

private JButton applyButton;

/**

* The button to exit scraper app

*/

private JButton exitButton;

/**

* The JComboBox for setting scraper settings

*/

private JComboBox scraperSetting;

/**

* <code>MyFrame</code> constructor

* @param frameTitle

*/

public MyFrame(String frameTitle) {

/*

* Call super constructor, set basic properties

*/

super(frameTitle);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

this.setResizable(false);

this.setLayout(new GridLayout(2,1));

this.setSize(350, 500);

JPanel topPanel = new JPanel(new GridLayout(2, 1));

topPanel.setBorder(BorderFactory.createTitledBorder("Scraper settings"));

JPanel bottomPanel = new JPanel(new GridLayout(1, 1));

bottomPanel.setBorder(BorderFactory.createTitledBorder("Exit Application"));

String[] scraperSettingStrings = {"Setting 1", "Setting 2"};

/* PROBLEM OCCURS WHEN CONSTRUCTOR IS INVOKED */

scraperSetting = new JComboBox();

/* EVERYTHING ELSE WORKS FINE!! */

//scraperSetting.setVisible(true);

//topPanel.add(scraperSetting);

applyButton = new JButton("Apply setting");

applyButton.addActionListener(this);

topPanel.add(applyButton);

this.add(topPanel);

exitButton = new JButton("Exit");

exitButton.addActionListener(this);

bottomPanel.add(exitButton);

this.add(bottomPanel);

}

/**

* <code>main</code> method

* @param args

*/

public static void main(String[] args) {

MyFrame frame = new MyFrame("Scraper Control");

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == applyButton) {

System.out.println("Setting scraper settings...");

} else if (e.getSource() == exitButton) {

System.out.println("Exiting app...");

System.exit(0);

}

}

}

[3175 byte] By [paulantona] at [2007-11-27 9:23:59]
# 1
where are adding to contentpane of the frame?you have to use setVisible after setting the size.there nothing wrong your combobox
dayanandabva at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 2
And please use code tags when posting example code....Unformatted code makes my brain ache :(
c0demonk3ya at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 3

I'm not sure I understand. The JComboBox is added to the "panel1" panel which is in turn added to "this", a "MyFrame" object created in the constructor.

The setvisible should not have anything to do with the fact that all other GUI components disappear when I call the constructor? The mystery here is that it does not matter if I add the JComboBox or not, it is sufficient that I call the constructor...

PS: How do code tags work? I couldn't figure out how people made such nice code examples here...

paulantona at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 4
>>PS: How do code tags work? I couldn't figure out how people made such nice code examples here... Check out http://forum.java.sun.com/help.jspa?sec=formatting
c0demonk3ya at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 5

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class MyFrame extends JFrame implements ActionListener

{

private static final long serialVersionUID = 1;

private JButtonapplyButton;

private JButtonexitButton;

private JComboBox scraperSetting;

public MyFrame(String frameTitle)

{

/*

* Call super constructor, set basic properties

*/

super(frameTitle);

JPanel topPanel = new JPanel(new GridLayout(2, 1));

topPanel.setBorder(BorderFactory.createTitledBorder("Scraper settings"));

JPanel bottomPanel = new JPanel(new GridLayout(1, 1));

bottomPanel.setBorder(BorderFactory.createTitledBorder("Exit Application"));

String[] scraperSettingStrings = { "Setting 1", "Setting 2" };

/* PROBLEM OCCURS WHEN CONSTRUCTOR IS INVOKED */

scraperSetting = new JComboBox();

/* EVERYTHING ELSE WORKS FINE!! */

// scraperSetting.setVisible(true);

topPanel.add(scraperSetting);

applyButton = new JButton("Apply setting");

applyButton.addActionListener(this);

topPanel.add(applyButton);

exitButton = new JButton("Exit");

exitButton.addActionListener(this);

bottomPanel.add(exitButton);

// you have add component to content pane of the frame, this is the way you to add

this.getContentPane().add(topPanel);

this.getContentPane().add(bottomPanel);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(false);

this.setLayout(new GridLayout(2, 1));

this.setSize(350, 500);

this.setVisible(true);

}

public static void main(String[] args)

{

MyFrame frame = new MyFrame("Scraper Control");

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == applyButton)

{

System.out.println("Setting scraper settings...");

}

else if (e.getSource() == exitButton)

{

System.out.println("Exiting app...");

System.exit(0);

}

}

}

dayanandabva at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 6
If I change scraperSetting = new JComboBox(); to scraperSetting = new JComboBox(scraperSettingStrings); it works fine?
c0demonk3ya at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 7
As I tried to explain, I've tried both constructors. Any way I do this, it seems that the minute I call the JComboBox constructor, everything is F*** Up.Of course, this (using scraperSettingStrings) is what I tried to do.
paulantona at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 8
A small detail, may or may not be important: I'm using javac version 1.6.0_01. Also using Eclipse IDE.
paulantona at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 9
it is not working for me i am not able to view any components, just check what i pasted code in above replay
dayanandabva at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 10
I tried the getContentPane() trick, but the situation is still that whenever the JComboBox constructor is called, no components show up. I do not understand why the single line of calling this constructor can completely destroy the GUI.
paulantona at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 11
OK, now I copied the code into a new file and ran that one - everything works fine. I DO NOT understand this AT ALL.... Why should a new file behave different from an old one?
paulantona at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 12
for your information, you cant add componet to the frame directly, you have to add to content pane of jrame becuase it is container to hold the componets.
dayanandabva at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...
# 13
Thanks for your information. I have tried to add both to the JFrame and to the ContentPane, both works, I see no difference. I still do not understand this error/bug, but it seems I must set the visible property in the end. Thanks alot for your help!!
paulantona at 2007-7-12 22:19:37 > top of Java-index,Desktop,Core GUI APIs...