seenuv
asked on
Combobox solution please
I have a combobox to which i am adding values, for example
JComboBox jb = new JComboBox();
jb.addItem("first");
jb.addItem("second");
when i view that in the applet the firstitem is "first" and when i drag the menu therealso the first item is "first" idont want that i wanna one similar to menu since we cant put a menu ina applet i am going for combobox how shall i get rid of the first element i tried with setSelectedIndex too it is showing the nth item specified i dont want that too the first item to be displayed and when i drag i dont need that first item again, how shall i go it. Please help me to solve this error. Please....
Thanks
Srini
JComboBox jb = new JComboBox();
jb.addItem("first");
jb.addItem("second");
when i view that in the applet the firstitem is "first" and when i drag the menu therealso the first item is "first" idont want that i wanna one similar to menu since we cant put a menu ina applet i am going for combobox how shall i get rid of the first element i tried with setSelectedIndex too it is showing the nth item specified i dont want that too the first item to be displayed and when i drag i dont need that first item again, how shall i go it. Please help me to solve this error. Please....
Thanks
Srini
To take a leaf out of the example,
U can simply add a JMenuBar to a JPanel.
Here is an example..
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//import javax.swing.border.*;
public class InternalMenu extends JFrame{
InternalMenu(){
setSize(400,300);
setTitle("Set Focus");
JLabel lbl=new JLabel("This frame has got the menu on a panel");
getContentPane().add(lbl,B orderLayou t.NORTH);
getContentPane().add(new menuPanel(),BorderLayout.C ENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
}
class menuPanel extends JPanel{
menuPanel(){
JMenuBar menubar=new JMenuBar();
JMenu menu=new JMenu("Item 1");
menu.add(new JMenuItem("Item 2"));
menu.add(new JMenuItem("Item 3"));
menu.add(new JMenuItem("Item 4"));
menu.add(new JMenuItem("Item 5"));
menubar.add(menu);
add(menubar);
}
}
public static void main(String args[]){
( new InternalMenu()).setVisible (true);
}
}
U can simply add a JMenuBar to a JPanel.
Here is an example..
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//import javax.swing.border.*;
public class InternalMenu extends JFrame{
InternalMenu(){
setSize(400,300);
setTitle("Set Focus");
JLabel lbl=new JLabel("This frame has got the menu on a panel");
getContentPane().add(lbl,B
getContentPane().add(new menuPanel(),BorderLayout.C
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
}
class menuPanel extends JPanel{
menuPanel(){
JMenuBar menubar=new JMenuBar();
JMenu menu=new JMenu("Item 1");
menu.add(new JMenuItem("Item 2"));
menu.add(new JMenuItem("Item 3"));
menu.add(new JMenuItem("Item 4"));
menu.add(new JMenuItem("Item 5"));
menubar.add(menu);
add(menubar);
}
}
public static void main(String args[]){
( new InternalMenu()).setVisible
}
}
if you want to remove an item from a combo use the removeItem(object) or removeItemAt(index) methods.
is that what you are trying to do?
use JApplet if you can though, it's much easier, and combos make bad looking menus.
is that what you are trying to do?
use JApplet if you can though, it's much easier, and combos make bad looking menus.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
take a look at SwingSet example applet.