2012-09-13 137 views

回答

2

您可以實施ListSelectionListener以跟蹤表中的選擇更改。然後執行TableCellRenderer,這將改變具有與選定對象相同值的單元格的背景。有關JTable,渲染器和選擇的更多詳細信息,請查看How to Use Tables

這是一個非常簡單的例子,證明了這種想法:

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 

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

public class TableDemo { 
    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("TableDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPanel = new JPanel(); 
     String[] columnNames = { "Column1", "Column2" }; 
     Object[][] data = { { "1", "3" }, { "2", "5" }, { "7", "1" }, 
       { "5", "3" } }; 

     JTable table = new JTable(); 
     MyModel model = new MyModel(Color.RED, table.getBackground()); 
     model.setDataVector(data, columnNames); 

     table.setModel(model); 
     table.setColumnSelectionAllowed(true); 
     table.setDefaultRenderer(Object.class, new TestCellRenderer()); 

     SelectionListener listener = new SelectionListener(table); 
     table.getSelectionModel().addListSelectionListener(listener); 
     table.getColumnModel().getSelectionModel() 
       .addListSelectionListener(listener); 

     JScrollPane scrollPane = new JScrollPane(table); 
     contentPanel.add(scrollPane); 

     contentPanel.setOpaque(true); 
     frame.add(contentPanel); 

     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    static class TestCellRenderer extends DefaultTableCellRenderer { 
     public Component getTableCellRendererComponent(JTable table, 
       Object value, boolean isSelected, boolean hasFocus, int row, 
       int column) { 
      Component c = super.getTableCellRendererComponent(table, value, 
        isSelected, hasFocus, row, column); 
      MyModel model = (MyModel) table.getModel(); 
      c.setBackground(model.getCellColor(row, column)); 
      return c; 
     } 
    } 

    static class MyModel extends DefaultTableModel { 
     private Object selectedObject; 
     private Color selectedColor; 
     private Color normalColor; 

     public MyModel(Color selectedColor, Color normalColor) { 
      this.selectedColor = selectedColor; 
      this.normalColor = normalColor; 
     } 

     public Color getCellColor(int row, int column) { 
      if (getValueAt(row, column).equals(selectedObject)) 
       return selectedColor; 
      return normalColor; 
     } 

     public void setSelectedObject(Object selectedObject) { 
      this.selectedObject = selectedObject; 
      fireTableRowsUpdated(0, getRowCount()); 
     } 
    } 

    static class SelectionListener implements ListSelectionListener { 
     private JTable table; 

     SelectionListener(JTable table) { 
      this.table = table; 
     } 

     public void valueChanged(ListSelectionEvent e) { 
      int rowIndex = table.getSelectedRow(); 
      int colIndex = table.getSelectedColumn(); 
      if (!e.getValueIsAdjusting() && colIndex != -1 && rowIndex != -1) { 
       ((MyModel) table.getModel()).setSelectedObject(table 
         .getValueAt(rowIndex, colIndex)); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

這裏是一個結果:

enter image description here

編輯:解決方案只使用渲染器,沒有表格模型

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 

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

public class TableDemo { 
    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("TableDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPanel = new JPanel(); 
     String[] columnNames = { "Column1", "Column2" }; 
     Object[][] data = { { "1", "3" }, { "2", "5" }, { "7", "1" }, 
       { "5", "3" } }; 

     JTable table = new JTable(new DefaultTableModel(data, columnNames)); 
     table.setColumnSelectionAllowed(true); 
     TestCellRenderer renderer = new TestCellRenderer(); 
     table.setDefaultRenderer(Object.class, renderer); 

     SelectionListener listener = new SelectionListener(table); 
     table.getSelectionModel().addListSelectionListener(listener); 
     table.getColumnModel().getSelectionModel() 
       .addListSelectionListener(listener); 

     JScrollPane scrollPane = new JScrollPane(table); 
     contentPanel.add(scrollPane); 

     contentPanel.setOpaque(true); 
     frame.add(contentPanel); 

     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    static class TestCellRenderer extends DefaultTableCellRenderer { 

     public Component getTableCellRendererComponent(JTable table, 
       Object value, boolean isSelected, boolean hasFocus, 
       int row, int column) { 

      Component c = super.getTableCellRendererComponent(table, value, 
        isSelected, hasFocus, row, column); 

      int columnIndex = table.getSelectedColumn(); 
      int rowIndex = table.getSelectedRow(); 

      if (columnIndex != -1 && rowIndex != -1){ 
       Object selectedValue = table.getValueAt(rowIndex, columnIndex); 

       if (value.equals(selectedValue)) { 
        c.setBackground(Color.RED); 
       } else { 
        c.setBackground(table.getBackground()); 
       } 
      } 
      return c; 
     } 
    } 

    static class SelectionListener implements ListSelectionListener { 
     private JTable table; 

     SelectionListener(JTable table) { 
      this.table = table; 
     } 

     public void valueChanged(ListSelectionEvent e) { 
      int rowIndex = table.getSelectedRow(); 
      int colIndex = table.getSelectedColumn(); 

      if (!e.getValueIsAdjusting() && colIndex != -1 && rowIndex != -1){ 
       table.repaint(); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+1

+1好主意,但是我認爲[應該在Renderer和沒有模型的情況下完成](http://stackoverflow.com/questions/7132400/jtable-row-hightlighter-based-on-value-from-tablecell) , – mKorbel

+0

@mKorbel是的!似乎更清潔。我也包括它。 – tenorsax

+1

你的衚衕在這裏....,我的尊重(並通過接受偉大的代碼默認情況下不能upvoted) – mKorbel

相關問題