In actual projects, we often need to limit the number of the choices available in a list. The key may be the simplest one mentioned in our website: the method SelectionModel of DataBox. When the number of choices has surpassed the specified one, the earliest choices will be deleted.
box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener() {We can set the property max. If max=1, then it becomes an exclusive selection.
@Override
public void selectionChanged(DataBoxSelectionEvent e) {
if (e.getBoxSelectionModel().size() > max)
e.getBoxSelectionModel().firstElement().setSelected(false);
}
}
});
In the following, a simple example has been made according to the above way: making a simple order list. There must be some similar scenarios in systems you have made: letting your users choose the specified number of data in list.
package test;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import twaver.*;
import twaver.list.*;
public class TestFrame extends JFrame {
public TestFrame() {
UIManager.put("Label.font", new Font("Dialog", Font.BOLD, 12));
UIManager.put("OptionPane.font", new Font("Dialog", Font.BOLD, 12));
this.setTitle("TWaver's menu orderer");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700, 350);
TWaverUtil.centerWindow(this);
JPanel optionPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 120));
final JComboBox cbMax = new JComboBox();
for (int i = 1; i < 6; i++) {
cbMax.addItem(i);
}
optionPane.add(new JLabel("At most you could order"));
optionPane.add(cbMax);
optionPane.add(new JLabel("dishes. "));
optionPane.add(new JLabel("The dishes you have ordered:"));
final JLabel lbOptions = new JLabel(" ");
optionPane.add(lbOptions);
JButton btnOption = new JButton("order");
optionPane.add(btnOption);
btnOption.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
ArrayList<String> options = pickOption((Integer) cbMax.getSelectedItem());
String text = "";
for (String option : options) {
text += option + " ";
}
lbOptions.setText(text);
}
});
this.getContentPane().add(optionPane, BorderLayout.CENTER);
}
private ArrayList<String> pickOption(final int max) {
String[] options = {
"double-cooked pork slices",
"Kung Pao Chicken",
"Sauteed Shredded Pork with Sweet - Bean Sauce",
"Braised chicken giblets",
"Braised large intestine",
"green pepper and scrambled eggs",
"stir-fried eggs with tomato",
};
TDataBox box = new TDataBox();
for (String option : options) {
ResizableNode node = new ResizableNode();
node.setName(option);
box.addElement(node);
}
box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener() {
@Override
public void selectionChanged(DataBoxSelectionEvent e) {
if (e.getBoxSelectionModel().size() > max) {
e.getBoxSelectionModel().firstElement().setSelected(false);
}
}
});
TList list = new TList(box);
list.setTListSelectionMode(TList.CHECK_SELECTION);
list.setIconVisible(false);
JScrollPane scroll = new JScrollPane(list);
Object[] message = new Object[] { "What would you like? Notice: You could order at most " + max + " dishes", scroll };
int answer = JOptionPane.showConfirmDialog(this, message, "order", JOptionPane.OK_CANCEL_OPTION);
ArrayList<String> result = new ArrayList<String>();
if (answer == JOptionPane.OK_OPTION) {
Iterator it = box.getSelectionModel().selection();
while (it.hasNext()) {
result.add(((Element) it.next()).getName());
}
}
return result;
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
TestFrame ui = new TestFrame();
ui.setVisible(true);
}
}
No comments:
Post a Comment