Tag: JComboBox
Example Code #2 – JOptionPane JCombobox
by Samet Kilictas on Dec.21, 2008, under Java, Programming
This code shows how to add ComboBox property to JOptionPane.showInputDialog. And the class is able to take an input from this combobox
import javax.swing.JOptionPane;
public class Options {
public Options() {
String smallList[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thrusday", "Friday", "Saturday" };
Java ActionListener Tutorial
by Samet Kilictas on Nov.06, 2008, under Java
This tutorial has two classes. ActionListener and Main class are seperated from each other.
- ParamListener.java
- BtnsListener.java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* ParamListener.java
*/
public class ParamListener extends JFrame{
public ParamListener() {
JButton btn10 = new JButton("10");
JButton btn20 = new JButton("20");
JButton btn30 = new JButton("30");
JButton btn40 = new JButton("40");
btn10.addActionListener( new BtnsListener ("ten"));
btn20.addActionListener( new BtnsListener ("twenty"));
btn30.addActionListener( new BtnsListener ("thirty"));
btn40.addActionListener( new BtnsListener ("fourty"));
JPanel content = new JPanel();
content.add(btn10);
content.add(btn20);
content.add(btn30);
content.add(btn40);
setContentPane(content);
setTitle("A Tutorial");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public static void main(String[] args) {
new ParamListener();
}
}
Java JCombobox Tutorial
by Samet Kilictas on Nov.06, 2008, under General, Java
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();
}

Thanks for dropping by! Feel free to join the discussion by leaving comments, and stay updated by subscribing to the 