/* $Id$ */
/* Copyright 2010 Sebastian Haufe
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.Formatter;
import javax.swing.*;
public class ComboBoxItemSelectableTest {
/**
* Test main method.
*
* @param args ignored
*/
public static void main(String[] args) {
final JComboBox combo = new JComboBox(new String[] { "A", "B", "C" });
final JTextArea feedbackArea = new JTextArea(5, 40);
feedbackArea.setEditable(false);
class ComboListener implements ActionListener, ItemListener {
private Object previous;
private Object current;
public void itemStateChanged(ItemEvent e) {
switch (e.getStateChange()) {
case ItemEvent.SELECTED:
current = e.getItem();
break;
case ItemEvent.DESELECTED:
previous = e.getItem();
break;
}
}
public void actionPerformed(ActionEvent e) {
feedbackArea.append(new Formatter().format("%s => %s%n", previous,
current).toString());
}
}
final ComboListener comboListener = new ComboListener();
combo.addActionListener(comboListener);
combo.addItemListener(comboListener);
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(combo, BorderLayout.NORTH);
contentPane.add(feedbackArea, BorderLayout.CENTER);
final JFrame f = new JFrame("Test Frame: ComboBoxItemSelectableTest"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}