I have a JComboBox that used to appear in the JFrame and now it doesn't...Not sure why but I think it might have to do with JPanels and/or Containers. I want there to be a JComboBox with the different clothing types in it. The JFrame appears without the JComboBox, which is weird. Here is my code:
public void init() {
JComboBox dropDown = new JComboBox(description);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cont = frame.getContentPane();
cont.setLayout(new FlowLayout());
cont.add(t);
cont.add(c);
final JPanel pan = new JPanel();
for (int i=0; i<6; i++){
c.addItem(description[count++]);
t.setEditable(false);
}//end of for
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if (count < description.length){
c.addItem(description[count++]);
}//end of if
selected = " "+((JComboBox) evt.getSource()).getSelectedItem();
}//end of method
};//end of listener
}//end of method
Firstly you have applied ";" after your loop. That is a great mistake. I've read your code and done some changes. Have a look at this. This code shows the JComboBox on the screen.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ans
{
JFrame frame=new JFrame();
String description[]={
"Hello","Bye","Java","Python","Fortran","Pascal"
};
int count=0;
JComboBox c=new JComboBox();
public void init() {
final JPanel pan = new JPanel();
pan.setLayout(new FlowLayout());
for (int i = 0; i < 6; i++)
c.addItem(description[count++]);
c.setEditable(false);
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
//Do Something
}//end of if
// ... ? ...
}});
frame.setContentPane(c);
frame.setVisible(true);
frame.pack();
}
public static void main(String arg[])
{
ans a=new ans();
a.init();
}
}
We can't see all the important parts of your code but one likely problem is the ;
after the for
loop.
for (int i = 0; i < 6; i++); c.addItem(description[count++]);
Will only add an item of count 0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.