Home > General, Java > Java JCombobox Tutorial

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);

}

}

VN:F [1.9.11_1134]
Rating: 1.0/10 (1 vote cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)
Java JCombobox Tutorial, 1.0 out of 10 based on 1 rating
Categories: General, Java Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.