編集可能なJComboBox
戻る
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
$Id: JComboBox.html,v 1.1 2009/06/22 16:11:47 kishi Exp kishi $
*/
public class ComboBoxTest extends JFrame implements ActionListener {
private JComboBox combo;
private JButton button;
private final String listItem[] =
{ "新宿", "代々木", "原宿", "渋谷", "恵比寿", "目黒", "五反田", "大崎", "品川", "田町" };
public ComboBoxTest() {
super( "ComboBoxTest" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
combo = new JComboBox( listItem );
combo.setEditable( true );
getContentPane().add( combo );
button = new JButton( "実行" );
button.addActionListener( this );
getContentPane().add( button, BorderLayout.SOUTH );
pack();
}
public void actionPerformed( ActionEvent e ) {
String selectedString = ( String ) combo.getSelectedItem();
System.out.println( "--- 以下の駅名が選択されました! ---" );
System.out.println( selectedString );
for ( int i = 0; i < combo.getItemCount(); i++ ) {
if ( selectedString.equals( combo.getItemAt( i ) ) ) {
return ;
}
}
combo.addItem( selectedString );
}
public static void main( String args[] ) {
new ComboBoxTest().setVisible( true );
}
}
戻る