TWaver - 專注UI技術(shù)

          http://twaver.servasoft.com/
          posts - 171, comments - 191, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          控制TList的限量多選

          Posted on 2012-05-30 09:55 TWaver 閱讀(1402) 評論(1)  編輯  收藏
                大家知道TWaver Java總的TList繼承自Swing的JList,所以我們可以很容易控制其選擇,例如單選、多選等。但是本文介紹如何控制按指定數(shù)量進(jìn)行選擇。
                人生就是一個不停選擇的過程。所以,我們必須要謹(jǐn)慎地控制好你的選擇,無論是TList的SelectionModel,還是人生之路。 



               在實(shí)際項目中,我們經(jīng)常需要對list進(jìn)行不能超過限定數(shù)量的多重選擇控制。本文用到的技巧可能是本站最簡單的一次了:利用DataBox的SelectionModel,當(dāng)選擇總數(shù)超過限定,就將最早的選擇刪除。
           
          1 box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener() {
          2     @Override
          3     public void selectionChanged(DataBoxSelectionEvent e) {
          4         if (e.getBoxSelectionModel().size() > max)
          5             e.getBoxSelectionModel().firstElement().setSelected(false);
          6         }
          7     }
          8 });

               其中max我們可以指定。如果max=1則變成了單選。
           
           通過這個思路,本文寫了一個很簡單的例子:通過控制多選數(shù)量,制作一個簡單的點(diǎn)菜選擇。相信你的系統(tǒng)中也經(jīng)常會有類似的場景:通過list讓用戶選擇指定數(shù)量的數(shù)據(jù)。

           1package bb.app.leather;
           2 
           3 import java.awt.*;
           4 import java.awt.event.*;
           5 import java.util.*;
           6 
           7 import javax.swing.*;
           8 
           9 import twaver.*;
          10 import twaver.list.*;
          11 
          12 public class TestFrame extends JFrame {
          13     public TestFrame() {
          14         this.setTitle("TWaver點(diǎn)菜器");
          15         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          16         this.setSize(700, 350);
          17         TWaverUtil.centerWindow(this);
          18         JPanel optionPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 120));
          19         final JComboBox cbMax = new JComboBox();
          20         for (int i = 1; i < 6; i++) {
          21             cbMax.addItem(i);
          22         }
          23         optionPane.add(new JLabel("最多可以點(diǎn)"));
          24         optionPane.add(cbMax);
          25         optionPane.add(new JLabel("個菜。    "));
          26         optionPane.add(new JLabel("您點(diǎn)的菜:"));
          27         final JLabel lbOptions = new JLabel("          ");
          28         optionPane.add(lbOptions);
          29         JButton btnOption = new JButton("點(diǎn)菜");
          30         optionPane.add(btnOption);
          31         btnOption.addActionListener(new ActionListener() {
          32             @Override
          33             public void actionPerformed(ActionEvent arg0) {
          34                 ArrayList<String> options = pickOption((Integer) cbMax.getSelectedItem());
          35                 String text = "";
          36                 for (String option : options) {
          37                     text += option + "  ";
          38                 }
          39                 lbOptions.setText(text);
          40             }
          41         });
          42         this.getContentPane().add(optionPane, BorderLayout.CENTER);
          43     }
          44 
          45     private ArrayList<String> pickOption(final int max) {
          46         String[] options = {
          47                 "回鍋肉",
          48                 "宮保雞丁",
          49                 "京醬肉絲",
          50                 "紅燒雞雜",
          51                 "紅燒大腸",
          52                 "青椒炒蛋",
          53                 "西紅柿炒蛋",
          54         };
          55 
          56         TDataBox box = new TDataBox();
          57         for (String option : options) {
          58             ResizableNode node = new ResizableNode();
          59             node.setName(option);
          60             box.addElement(node);
          61         }
          62         box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener() {
          63             @Override
          64             public void selectionChanged(DataBoxSelectionEvent e) {
          65                 if (e.getBoxSelectionModel().size() > max) {
          66                     e.getBoxSelectionModel().firstElement().setSelected(false);
          67                 }
          68             }
          69         });
          70         TList list = new TList(box);
          71         list.setTListSelectionMode(TList.CHECK_SELECTION);
          72         list.setIconVisible(false);
          73         JScrollPane scroll = new JScrollPane(list);
          74         Object[] message = new Object[] { "您要吃點(diǎn)什么?注意:最多只能點(diǎn)" + max + "個菜哦!", scroll };
          75         int answer = JOptionPane.showConfirmDialog(this, message, "點(diǎn)菜", JOptionPane.OK_CANCEL_OPTION);
          76         ArrayList<String> result = new ArrayList<String>();
          77         if (answer == JOptionPane.OK_OPTION) {
          78             Iterator it = box.getSelectionModel().selection();
          79             while (it.hasNext()) {
          80                result.add(((Element) it.next()).getName());
          81             }
          82         }
          83         return result;
          84     }
          85 
          86     public static void main(String[] args) throws Exception {
          87         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          88         TestFrame ui = new TestFrame();
          89         ui.setVisible(true);
          90     }
          91 }
          92 


          評論

          # website design bangalore  回復(fù)  更多評論   

          2013-02-05 16:35 by web design bangalore
          I have to say about this is very interesting, it seems that many people like it.Okay, I will recommend my friends to have a look, look forward to the new content.

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 淮滨县| 西贡区| 东乡族自治县| 朝阳市| 通化市| 平昌县| 磐安县| 渭南市| 哈巴河县| 霍城县| 肥西县| 分宜县| 即墨市| 利津县| 东明县| 铜陵市| 嘉善县| 务川| 吉隆县| 商水县| 沅陵县| 调兵山市| 五寨县| 青河县| 柘城县| 游戏| 孝感市| 油尖旺区| 麻城市| 岗巴县| 铁岭市| 上饶县| 达州市| 米泉市| 逊克县| 龙泉市| 茶陵县| 汉寿县| 五指山市| 库车县| 观塘区|