116 lines
2.8 KiB
Java
116 lines
2.8 KiB
Java
package extra.dialog;
|
|
|
|
import javax.swing.JPanel;
|
|
import javax.swing.Box;
|
|
import javax.swing.BoxLayout;
|
|
import javax.swing.DefaultComboBoxModel;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JDialog;
|
|
import javax.swing.JLabel;
|
|
|
|
import java.awt.Dimension;
|
|
import java.awt.Frame;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.List;
|
|
|
|
class MyDialogWindow extends JDialog {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private int _status;
|
|
private JComboBox<Dish> _dishes;
|
|
private DefaultComboBoxModel<Dish> _dishesModel;
|
|
|
|
public MyDialogWindow(Frame parent) {
|
|
super(parent, true);
|
|
initGUI();
|
|
}
|
|
|
|
private void initGUI() {
|
|
|
|
_status = 0;
|
|
|
|
setTitle("Food Selector");
|
|
JPanel mainPanel = new JPanel();
|
|
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
|
|
setContentPane(mainPanel);
|
|
|
|
JLabel helpMsg = new JLabel("Select your favorite");
|
|
helpMsg.setAlignmentX(CENTER_ALIGNMENT);
|
|
|
|
mainPanel.add(helpMsg);
|
|
|
|
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
|
|
|
|
JPanel viewsPanel = new JPanel();
|
|
viewsPanel.setAlignmentX(CENTER_ALIGNMENT);
|
|
mainPanel.add(viewsPanel);
|
|
|
|
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
|
|
|
|
JPanel buttonsPanel = new JPanel();
|
|
buttonsPanel.setAlignmentX(CENTER_ALIGNMENT);
|
|
mainPanel.add(buttonsPanel);
|
|
|
|
_dishesModel = new DefaultComboBoxModel<>();
|
|
_dishes = new JComboBox<>(_dishesModel);
|
|
|
|
viewsPanel.add(_dishes);
|
|
|
|
JButton cancelButton = new JButton("Cancel");
|
|
cancelButton.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
_status = 0;
|
|
MyDialogWindow.this.setVisible(false);
|
|
}
|
|
});
|
|
buttonsPanel.add(cancelButton);
|
|
|
|
JButton okButton = new JButton("OK");
|
|
okButton.addActionListener(new ActionListener() {
|
|
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (_dishesModel.getSelectedItem() != null) {
|
|
_status = 1;
|
|
MyDialogWindow.this.setVisible(false);
|
|
}
|
|
}
|
|
});
|
|
buttonsPanel.add(okButton);
|
|
|
|
setPreferredSize(new Dimension(500, 200));
|
|
pack();
|
|
setResizable(false);
|
|
setVisible(false);
|
|
}
|
|
|
|
public int open(List<Dish> dishes) {
|
|
|
|
// update the comboxBox model -- if you always use the same no
|
|
// need to update it, you can initialize it in the constructor.
|
|
//
|
|
_dishesModel.removeAllElements();
|
|
for (Dish v : dishes)
|
|
_dishesModel.addElement(v);
|
|
|
|
// You can chenge this to place the dialog in the middle of the parent window.
|
|
// It can be done using uing getParent().getWidth, this.getWidth(),
|
|
// getParent().getHeight, and this.getHeight(), etc.
|
|
//
|
|
setLocation(getParent().getLocation().x + 10, getParent().getLocation().y + 10);
|
|
|
|
setVisible(true);
|
|
return _status;
|
|
}
|
|
|
|
Dish getDish() {
|
|
return (Dish) _dishesModel.getSelectedItem();
|
|
}
|
|
|
|
}
|