Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with display of JComboBox  RSS feed

 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a program to display a JLabel and 2 JComboBoxes.

It is compiling correctly but not diisplaying either of them.

Unable to find the bug in my code.Previously I wrote small programs to display JButtons,JradioButtons,JTextFileds,JLabels and now moved on to JComboBox.

here my code goes


import javax.swing.*;
import java.awt.*;

public class comboDemo extends JFrame

{
private JLabel expiryLabel;

private JComboBox yearBox,monthBox;

public static void main(String ar[])

{

comboDemo obj1 = new comboDemo();

}

public comboDemo()
{
JFrame frame1 = new JFrame();
frame1.setVisible(true);
frame1.setTitle("Combo Demo");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container c = getContentPane();

c.setLayout(new FlowLayout());


expiryLabel = new JLabel("Expiration Date");

c.add(expiryLabel);

monthBox= new JComboBox();

yearBox= new JComboBox();

for(int i=1;i<13;i++)

monthBox.addItem(" " +i);//adding month number to first JComboBox.


for(int i=2008;i<2012;i++)

yearBox.addItem(" "+i);//adding year to second JComboBox

c.add(monthBox); //adding first JComboBox to container
c.add(yearBox); //adding second JcomboBox to container.

}


}


Can anybody please point out where iam going wrong in displaying?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you create frame1 and set its visibility to true (the one on the screen)

at the same time you have extended JFrame, and with this line
Container c = getContentPane();
set in place the adding of the components to a non-visible frame

and, if you want to add components to frame1, set its visibility to true
after adding the components
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Michael,

I got my mistake.And I changed my code in constructor of comboDemo() as follows

public comboDemo()
{

super("JComboDemo");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

Container c = getContentPane();

// After this,I am adding Label and JcomboBox components through c.add
//(Component
) method

}

I didnt create any JFrame object here.The components are added to container's ContentPane.

So,there is no use of creating frame1 in the constructor comboDemo() right?

If I create and set its visibility to true after adding all the components,still same problem of not displaying the components.


Apart from the lines I made in itlaics,please clarify any difference in o/p, if I create a new Jframe object and add components to it and without creating JFrame obj as the class already extends JFrame?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this the question?
> Apart from the lines I made in itlaics,please clarify any difference in o/p,
o/p = output??

if your question is should I extend JFrame or create a JFrame, my opinion is
you should not extend JFrame unless you are enhancing JFrame i.e. adding
some functionality to it e.g. TransparentJFrame, RoundedEdgeJFrame etc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!