2013-05-29 83 views
-1

JTable的渲染是否有任何人知道我可以在這個代碼添加一個複選框:使用複選框

String data[][]={ 
     {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"} 
    }; 
    String column[]={"Fruits","Fruits","Fruits"}; 
    table=new JTable(new DefaultTableModel(data, column)){ 
      private Border outside = new MatteBorder(1, 0, 1, 0, Color.RED); 
      private Border inside = new EmptyBorder(0, 1, 0, 1); 
      private Border highlight = new CompoundBorder(outside, inside); 

      public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 
      { 
       Component c = super.prepareRenderer(renderer, row, column); 
       JComponent jc = (JComponent)c; 

       // Add a border to the selected row 

       if (isRowSelected(row)) 
        jc.setBorder(highlight); 

           return c; 
      } 
     }; 


jScrollPane1.setViewportView(table); 

我只想補充複選框,這樣,如果我選中一個複選框,這將突出和所有選中的複選框會突出顯示。預先感謝您幫助我!

+0

我不明白的問題,所以我會建議你讀了Swing教程[如何使用表(http://docs.oracle.com/javase/tutorial/uiswing/components/table .html)作爲包含複選框的工作示例。 – camickr

回答

0

這裏是僞代碼,我在我的倉庫找到了某處。根據您的使用使用它。

import java.awt.Color; 
import java.awt.Component; 

import javax.swing.*; 
import javax.swing.table.*; 

public class TableCheckBoxHighLight extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JTable table; 

    public TableCheckBoxHighLight() { 
     Object[] columnNames = { "Col1", "Col2", "Select" }; 
     Object[][] data = { 
          { "Item1", "123", false }, 
          { "Item2", "345", false }, 
          { "Item3", "678", false }, 
          { "Item4", "901", false } 
          }; 
     DefaultTableModel model = new DefaultTableModel(data, columnNames); 
     table = new JTable(model) { 

      private static final long serialVersionUID = 1L; 

      @SuppressWarnings("unchecked") 
      @Override 
      public Class getColumnClass(int column) { 
       switch (column) { 
       case 0: 
        return String.class; 
       case 1: 
        return String.class; 

       default: 
        return Boolean.class; 
       } 
      } 

      @Override 
      public Component prepareRenderer(TableCellRenderer renderer, 
        int row, int col) { 
       Component c = super.prepareRenderer(renderer, row, col); 

       int[] selCols = table.getSelectedColumns(); 
       table.setSelectionBackground(Color.GREEN); 
       for (int i : selCols) 
        c.setBackground(Color.RED); 

       return c; 
      } 
     }; 

     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       TableCheckBoxHighLight frame = new TableCheckBoxHighLight(); 
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setLocation(150, 150); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+0

感謝@smit先生的幫助。你可以編輯代碼,以便只有選中複選框的行纔會突出顯示。 TNX。 – user2322019