import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
class MyPanel extends JPanel implements ActionListener, ListSelectionListener, ChangeListener
{
String[] drinks = {"콜라", "사이다", "환타", "마운틴듀", "레드불"};
JComboBox combo;
JList list;
JSpinner spin;
public MyPanel() {
combo = new JComboBox(drinks);
list = new JList(drinks);
SpinnerListModel model = new SpinnerListModel(drinks);
spin = new JSpinner(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
combo.setSelectedIndex(0);
list.setSelectedIndex(0);
combo.addActionListener(this);
list.addListSelectionListener(this);
spin.addChangeListener(this);
add(combo);
add(list);
add(spin);
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
int num = list.getSelectedIndex(); // 리스트에서 인덱스 얻어옴
combo.setSelectedIndex(num); // 콤보박스에 인덱스 설정
spin.setValue(list.getSelectedValue());
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int num = combo.getSelectedIndex(); // 콤보박스에서 인덱스 얻어옴
list.setSelectedIndex(num); // 리스트에 인덱스 설정
spin.setValue(combo.getSelectedItem());
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
list.setSelectedValue(spin.getValue(), false);
combo.setSelectedItem(spin.getValue());
}
}
public class MyFrame extends JFrame {
public MyFrame() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new MyPanel());
pack();
setVisible(true);
}
public static void main(String[] args) {
MyFrame t = new MyFrame();
}
}