JTable,仿Outlook介面

來源:互聯網
上載者:User
import   javax.swing.JFrame;  
import   javax.swing.JPanel;  
import   javax.swing.JScrollPane;  
import   javax.swing.JTable;  
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import java.awt.Component;
import   java.awt.Dimension;  
import   java.awt.GridLayout;  
import   java.awt.event.MouseAdapter;  
import   java.awt.event.MouseEvent;  
import java.lang.Runnable;
import java.awt.Font;

public   class   SimpleTableDemo   extends   JPanel   {  
    private   boolean   DEBUG   =   false;  

    public   SimpleTableDemo()   {  
        super(new   GridLayout(1,0));  

        String[]   columnNames   =   {"First   Name",  
                "Last   Name",  
                "Sport",  
                "#   of   Years",  
        "Vegetarian"};  

        Object[][]   data   =   {  
                {"Mary",   "Campione",  
                    "Snowboarding",   new   Integer(5),   new   Boolean(true)},  
                    {"Alison",   "Huml",  
                        "Rowing",   new   Integer(3),   new   Boolean(true)},  
                        {"Kathy",   "Walrath",  
                            "Knitting",   new   Integer(2),   new   Boolean(true)},  
                            {"Sharon",   "Zakhour",  
                                "Speed   reading",   new   Integer(20),   new   Boolean(true)},  
                                {"Philip",   "Milne",  
                                    "Pool",   new   Integer(10),   new   Boolean(true)}  
        };  

        final   JBoldTable   table   =   new   JBoldTable(data,   columnNames);  
        table.setPreferredScrollableViewportSize(new   Dimension(500,   70));  

        if   (DEBUG)   {  
            table.addMouseListener(new   MouseAdapter()   {  
                public   void   mouseClicked(MouseEvent   e)   {  
                    printDebugData(table);  
                }  
            });  
        }  

        //Create   the   scroll   pane   and   add   the   table   to   it.  
        JScrollPane   scrollPane   =   new   JScrollPane(table);  

        //Add   the   scroll   pane   to   this   panel.  
        add(scrollPane);  
    }  

    private   void   printDebugData(JTable   table)   {  
        int   numRows   =   table.getRowCount();  
        int   numCols   =   table.getColumnCount();  
        javax.swing.table.TableModel   model   =   table.getModel();  

        System.out.println("Value   of   data:   ");  
        for   (int   i=0;   i   <   numRows;   i++)   {  
            System.out.print("         row   "   +   i   +   ":");  
            for   (int   j=0;   j   <   numCols;   j++)   {  
                System.out.print("     "   +   model.getValueAt(i,   j));  
            }  
            System.out.println();  
        }  
        System.out.println("--------------------------");  
    }  

    /**  
     *   Create   the   GUI   and   show   it.     For   thread   safety,  
     *   this   method   should   be   invoked   from   the  
     *   event-dispatching   thread.  
     */  
    private   static   void   createAndShowGUI()   {  
        //Make   sure   we   have   nice   window   decorations.  
        //JFrame.setDefaultLookAndFeelDecorated(true);  

        //Create   and   set   up   the   window.  
        JFrame   frame   =   new   JFrame("SimpleTableDemo");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        //Create   and   set   up   the   content   pane.  
        SimpleTableDemo   newContentPane   =   new   SimpleTableDemo();  
        newContentPane.setOpaque(true);   //content   panes   must   be   opaque  
        frame.setContentPane(newContentPane);  

        //Display   the   window.  
        frame.pack();  
        frame.setVisible(true);  
    }  

    public   static   void   main(String[]   args)   {  
        //Schedule   a   job   for   the   event-dispatching   thread:  
        //creating   and   showing   this   application's   GUI.  
        javax.swing.SwingUtilities.invokeLater(new   Runnable()   {  
            public   void   run()   {  
                createAndShowGUI();  
            }  
        });  
    }  
}  

class JBoldTable extends JTable implements ListSelectionListener{
    MyBold bold = new MyBold(this);
    Thread td = new Thread(bold);
    boolean checkRun = false;
    int row = -1;
    public JBoldTable(Object[][] data, Object[] column) {
        super(data, column);
        setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {         
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                // TODO 自動產生方法存根
                super.getTableCellRendererComponent(table, value, isSelected,
                        hasFocus, row, column);
                Boolean bFlag = (Boolean)getValueAt(row, 4);
                int nFlag = 0;
                if (bFlag) {
                    nFlag = Font.BOLD;
                }
                Font font = new Font("宋體", nFlag, 12);
                setFont(font);
                return this;
            }

        });
        getSelectionModel().addListSelectionListener(this);

    }

    public void valueChanged(ListSelectionEvent e) {
        // TODO 自動產生方法存根
        repaint();
        if (row != getSelectedRow()) {
            row = getSelectedRow();

            if (td.isAlive()) {
                bold.stop();
                try {
                    td.join();
                } catch (InterruptedException e1) {
                    // TODO 自動產生 catch 塊
                    e1.printStackTrace();
                }
            }
            bold.start();
            td = new Thread(bold);
            td.start();

        }
    }   

    class MyBold implements Runnable {
        private JTable table;
        volatile boolean isStop = false;

        public MyBold(JTable table) {
            this.table = table;
        }

        public  void start() {
            isStop = false;
        }

        public  void stop() {
            isStop = true;
        }

        public synchronized void run() {
            // TODO 自動產生方法存根
            int row1 = table.getSelectedRow();
            for (int i = 0; i < 1000; ++i) {
                try {
                    Thread.sleep(1);
                    if (isStop)
                        return;
                } catch (InterruptedException e) {
                    // TODO 自動產生 catch 塊
                    e.printStackTrace();
                }
            }
            int row2 = table.getSelectedRow();
            if (row1 != row2)
                return;
            Boolean bFlag = (Boolean) table.getValueAt(row1, 4);
            if (!bFlag)
                return;
            table.setValueAt((Object)(new Boolean(!bFlag.booleanValue())), row1, 4);
            table.repaint();
            //table.updateUI();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.