import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class ComboSample extends JFrame implements ActionListener
{
private static final int boxW = 800;
private static final int boxH = 450;
private static final int box_X_o = 180;
private static final int box_Y_o = 200;
private JButton cancelButton;
private JButton confirmButton;
private JComboBox jumpStudent;
public ComboSample()
{
setTitle ("Combo sample that can be moved");
setSize (boxW ,boxH);
setLocation (box_X_o , box_Y_o);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setBackground(Color.white);
contentPane.setLayout(null);
//---JComboBox---
String[] studentNamesVector = {"a", "b", "c"};
jumpStudent = new JComboBox(studentNamesVector);
jumpStudent.setBounds(100, 100, 100, 30);
jumpStudent.addActionListener(this);
contentPane.add(jumpStudent);
addToComboBox(); //Add student names to the vector and to the JComboBox
confirmButton = new JButton("Confirm");
confirmButton.setBounds(610, 375, 80, 30);
contentPane.add(confirmButton);
confirmButton.addActionListener(this);
cancelButton = new JButton("Cancel");
cancelButton.setBounds(695, 375, 80, 30);
contentPane.add(cancelButton);
cancelButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() instanceof JButton)
{
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
String dedicationsRaw;
if (buttonText == "Confirm")
{
JOptionPane.showMessageDialog(null, "Nothing to do yet");
}
if (buttonText == "Cancel")
{
this.setVisible(false);
}
}
if(event.getSource() instanceof JComboBox)
{
JComboBox cb = (JComboBox) event.getSource();
String s = (String) cb.getSelectedItem();
JOptionPane.showMessageDialog(null, "You picked " + s );
}
}
public void addToComboBox()
{
}
public static void main(String[] args)
{
ComboSample frame = new ComboSample();
frame.setVisible(true);
}
}