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

ComboBox  RSS feed

 
Krishna Priya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to Swing can somebody please help me.

I am trying to set a Label when an option in the ComboBox is selected. I Am unable to see the label after the selection. Can somebody tell me why I am unable to see?

I am attaching my code.

protected JComponent makeEditPanel(){
JPanel viewPanel = new JPanel(new BorderLayout());

JCheckBox checkBox = new JCheckBox(" Show HTML");
checkBox.setSelected(false);
JLabel layoutLabel = new JLabel("Select Layout: ");

final String[] layoutString = {"","Into Question Conclusion Selected", "Question, Conclsion Selected" };
final JLabel selLayoutLabel = new JLabel();
final String[] layout = {"Select Layout", "Into Question Conclusion", "Question, Conclsion" };
final JComboBox layoutCombo = new JComboBox(layout);
layoutCombo.setSelectedIndex(0);

viewPanel.add(checkBox,BorderLayout.WEST);
layoutLabel.setHorizontalAlignment(JLabel.RIGHT);
viewPanel.add(layoutLabel);
viewPanel.add(layoutCombo, BorderLayout.EAST);

layoutCombo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JPanel panel = new JPanel(new BorderLayout());
selLayoutLabel.setText(layoutString[layoutCombo.getSelectedIndex()]);
System.out.println(selLayoutLabel);
panel.add(selLayoutLabel,BorderLayout.CENTER);
}
});


return viewPanel;
}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You set the text on selLayoutLabel and add it to a JPanel. Did you want to do something with the JPanel?
 
Krishna Priya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I need to add some text and comboBoxes to this panel.
I am not even able to display a label.
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your actionPerformed method, you add the label to a Panel which did not get to be added to anywhere. That's why you don't see it.
Try add


after

To just make sure you see your selection.
[ March 14, 2006: Message edited by: Ben Zung ]
 
Krishna Priya
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. It worked. but, I still have a lot of questions on how it is laying out the components. I have been looking at Sun's Learn swing by example.
Is there any other documentation that helps me understand better about different action listeners.
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial is a big book with all that you need in Swings. But the best teacher is always practice.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!