Java JCombobox Tutorial
This tiny program lets you to choose background color of your pane.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class tut1 extends JFrame{
Color[] colors = {
Color.RED, Color.BLUE, Color.YELLOW, Color.BLACK, Color.ORANGE,
new Color(0,143,222)
};
String[] renk = {
"red", "blue", "yellow", "black", "orange", "turkuaz"
};
public JComboBox cmb = new JComboBox(renk);
public JPanel main = new JPanel();
public tut1() {
cmb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
int index = cmb.getSelectedIndex();
main.setBackground(colors[index]);
}
});
cmb.setBackground(Color.blue);
JLabel lbl = new JLabel();
main.add(cmb);
main.add(lbl);
setContentPane(main);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setTitle("my title");
pack();
}
public static void main(String[] args) {
tut1 window = new tut1();
window.setVisible(true);
}
}
Tweet
Thanks for dropping by! Feel free to join the discussion by leaving comments, and stay updated by subscribing to the

Recent Comments