2016-11-20 134 views
1

在我的JTable上,我使用prepareRenderer函數設置行的背景顏色。我也刪除了焦點邊框,後來在代碼中禁用了行選擇。JTable中使用JCheckBox的背景顏色閃爍

JTable table = new JTable() { 
    private static final long serialVersionUID = -2965586838134675413L; 
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
     Component c = super.prepareRenderer(renderer, row, column); 
     c.setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248)); 
     ((JComponent) c).setBorder(null); 
     return c; 
    } 
}; 

DefaultTableModel tableModel = new DefaultTableModel(sampleData, headers) { 
    private static final long serialVersionUID = 9186050244728809804L; 

    @Override 
    public Class<?> getColumnClass(int columnIndex) { 
     switch(columnIndex) { 
      case 0: 
       return Boolean.class; 
      default: 
       return String.class; 
     } 
    } 


    @Override 
    public boolean isCellEditable(int row, int column) { 
     switch(column) { 
      case 0: 
       return true; 
      default: 
       return false; 
     } 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public void setValueAt(Object aValue, int row, int column) { 
     if (aValue instanceof Boolean && column == 0) { 
      //System.out.println(aValue); 
      Vector<Boolean> rowData = (Vector<Boolean>) getDataVector().get(row); 
      rowData.set(0, (boolean) aValue); 
      fireTableCellUpdated(row, column); 
     } 
    } 
}; 

table.setModel(tableModel); 
table.setRowSelectionAllowed(false); 

問題是當我點擊背景色開關中的JCheckBox時,白色按住鼠標按鈕。在點擊JCheckBox時是否保持顏色不變?


使用默認的布爾渲染器來實現這個還是有同樣的問題,但它不經常發生

private class BooleanRenderer extends JCheckBox implements TableCellRenderer 
{ 
    private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 

    public BooleanRenderer() { 
     super(); 
     setHorizontalAlignment(JLabel.CENTER); 
     setBorderPainted(true); 
    } 

    public Component getTableCellRendererComponent(JTable table, Object value, 
      boolean isSelected, boolean hasFocus, int row, int column) { 
     if (isSelected) { 
      setForeground(table.getSelectionForeground()); 
      super.setBackground(table.getSelectionBackground()); 
     } else { 
      setForeground(table.getForeground()); 
      setBackground(table.getBackground()); 
     } 
     setSelected((value != null && ((Boolean)value).booleanValue())); 
     setBorder(noFocusBorder); 

     return this; 
    } 
} 

table.setDefaultRenderer(Boolean.class, new BooleanRenderer()) 

注2
我也嘗試添加一個MouseListenerJCheckBox但這並沒有工作,因爲鼠標聽衆似乎並沒有附加到JCheckBox

JTable fileList = new JTable() { 
    private static final long serialVersionUID = -2965586838134675413L; 
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
     Component c = super.prepareRenderer(renderer, row, column); 
     c.setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248)); 
     ((JComponent) c).setBorder(null); 

     if(c instanceof JCheckBox) { 
      c.addMouseListener(new MouseAdapter() { 
       public void mouseClicked(MouseEvent e) { 
        setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248)); 
       } 

       public void mousePressed(MouseEvent e) { 
        setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248)); 
       } 

       public void mouseReleased(MouseEvent e) { 
        setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248)); 
       } 
      }); 
     } 
     return c; 
    } 
}; 

編輯 - 在您的JTable例如使用第一個例子的代碼

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

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellRenderer; 

public class JTableTest extends JFrame { 
    private JTableTest() { 
     super("Button Layout"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(1, 1)); 
     createPanel(); 
     pack(); 
     setVisible(true); 
    } 

    JPanel panel = new JPanel(new GridLayout(1, 1)); 
    JScrollPane scroll = new JScrollPane(); 

    private void createPanel() { 
     Object[] headers = {"Select", "Title", "Artist", "Length"}; 
     Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673"}, 
       {false, "Spanish Train", "Chris De Burgh", "358"}}; 
     JTable table = new JTable() { 
      public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
       Component c = super.prepareRenderer(renderer, row, column); 
       c.setBackground(row % 2 == 1 ? Color.WHITE : Color.RED); 
       ((JComponent) c).setBorder(null); //Left in so it is easier to see problem 
       return c; 
      } 
     }; 

     DefaultTableModel tableModel = new DefaultTableModel(sampleData, headers) { 
      @Override 
      public Class<?> getColumnClass(int columnIndex) { 
       switch(columnIndex) { 
        case 0: 
         return Boolean.class; 
        default: 
         return String.class; 
       } 
      } 
     }; 

     table.setModel(tableModel); 
     table.setRowSelectionAllowed(false); //Left in so it is easier to see problem 

     scroll.getViewport().add(table); 
     panel.add(scroll); 
     getContentPane().add(panel); 
    } 

    public static void main(String[] args) { 
     new JTableTest(); 
    } 
} 
+1

爲了更快得到更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

+0

@AndrewThompson包括。對於那個很抱歉。我認爲JTable代碼就夠了。你有什麼想法可能會導致它? – Dan

+0

@Dan也許你應該修剪其他部分,現在你包含了一個SSCCE。 –

回答

1

你缺少一個prepareEditor覆蓋SSSCE,我你的樣品中添加一個。當你點擊複選框時,顯示的是編輯器組件而不是渲染器組件。您還需要準備該組件以獲得所需的着色。

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

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellEditor; 
import javax.swing.table.TableCellRenderer; 

@SuppressWarnings("serial") 
public class JTableTest extends JFrame { 
    private JTableTest() { 
     super("Button Layout"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridLayout(1, 1)); 
     createPanel(); 
     pack(); 
     setVisible(true); 
    } 

    JPanel panel = new JPanel(new GridLayout(1, 1)); 
    JScrollPane scroll = new JScrollPane(); 

    private void createPanel() { 
     Object[] headers = {"Select", "Title", "Artist", "Length"}; 
     Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673"}, 
       {false, "Spanish Train", "Chris De Burgh", "358"}}; 
     JTable table = new JTable() { 
      public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
       Component c = super.prepareRenderer(renderer, row, column); 
       c.setBackground(row % 2 == 1 ? Color.WHITE : Color.RED); 
       ((JComponent) c).setBorder(null); //Left in so it is easier to see problem 
       return c; 
      } 

      @Override 
      public Component prepareEditor(TableCellEditor editor, int row, int column) { 
       Component c = super.prepareEditor(editor, row, column); 
       c.setBackground(row % 2 == 1 ? Color.WHITE : Color.RED); 
       ((JComponent) c).setBorder(null); //Left in so it is easier to see problem 
       return c; 
      } 
     }; 

     DefaultTableModel tableModel = new DefaultTableModel(sampleData, headers) { 
      @Override 
      public Class<?> getColumnClass(int columnIndex) { 
       switch(columnIndex) { 
        case 0: 
         return Boolean.class; 
        default: 
         return String.class; 
       } 
      } 
     }; 

     table.setModel(tableModel); 
     table.setRowSelectionAllowed(false); //Left in so it is easier to see problem 

     scroll.getViewport().add(table); 
     panel.add(scroll); 
     getContentPane().add(panel); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater (
      new Runnable() { 
       @Override 
       public void run() { 
        new JTableTest(); 
       } 
      } 
     ); 
    } 
} 
+0

非常感謝你 – Dan

+0

@Dan You '歡迎你! –

+0

出於興趣,你對[this]有任何見解(http://stackoverflow.com/questions/40698612/setting-jtableheader-holder-background-colour) – Dan