zoukankan      html  css  js  c++  java
  • 控制TList的限量多选

         大家知道TWaver Java总的TList继承自Swing的JList,所以我们可以很容易控制其选择,例如单选、多选等。但是本文介绍如何控制按指定数量进行选择。

         人生就是一个不停选择的过程。所以,我们必须要谨慎的控制好你的选择,无论是TList的SelectionModel,还是人生之路。

          在实际项目中,我们经常需要对list进行不能超过限定数量的多重选择控制。本文用到的技巧可能是本站最简单的一次了:利用DataBox的SelectionModel,当选择总数超过限定,就将最早的选择删除。

    box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener() {
    	@Override
    	public void selectionChanged(DataBoxSelectionEvent e) {
    		if (e.getBoxSelectionModel().size() > max)
    			e.getBoxSelectionModel().firstElement().setSelected(false);
    		}
    	}
    });
    

      

          其中max我们可以指定。如果max=1则变成了单选。 通过这个思路,本文写了一个很简单的例子:通过控制多选数量,制作一个简单的点菜选择。相信你的系统中也经常会有类似的场景:通过list让用户选择指定数量的数据。

    package bb.app.leather;
    
    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() {
    		this.setTitle("TWaver点菜器");
    		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("最多可以点"));
    		optionPane.add(cbMax);
    		optionPane.add(new JLabel("个菜。    "));
    		optionPane.add(new JLabel("您点的菜:"));
    		final JLabel lbOptions = new JLabel("          ");
    		optionPane.add(lbOptions);
    		JButton btnOption = new JButton("点菜");
    		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 = {
    				"回锅肉",
    				"宫保鸡丁",
    				"京酱肉丝",
    				"红烧鸡杂",
    				"红烧大肠",
    				"青椒炒蛋",
    				"西红柿炒蛋",
    		};
    
    		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[] { "您要吃点什么?注意:最多只能点" + max + "个菜哦!", scroll };
    		int answer = JOptionPane.showConfirmDialog(this, message, "点菜", 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);
    	}
    }
    

      

  • 相关阅读:
    Requests库常用方法及其详解
    MacOS下搭建python环境
    Requests库与HTTP协议
    MacOS下安装Requests库及使用
    Swing State: Consistent Updates for Stateful and Programmable Data Planes
    Transparent Flow Migration for NFV
    2018软工团队选题报告
    Traffic Steering for Service Function Chaining
    2018年软工第二次结对作业
    【数字图像处理】Tencent视频团队讲座记录
  • 原文地址:https://www.cnblogs.com/twaver/p/2526014.html
Copyright © 2011-2022 走看看