2014-07-20 189 views
4

嗨,我是新的java jtable cellrended。我正在尋找一種能夠在我的程序中工作的方式,但我沒有任何運氣可以找到它。 這裏是我的JTable根據列值更改JTable行的背景顏色

Employee ID | Name  | Status | Position 
    00565651  Roger  Active  Manager 
    00565652  Gina  Active  Crew 
    00565652  Alex  Inactive  Crew 
    00565652  Seph  Active  Manager  

從MS Access數據庫中的數據來了,但我想改變其在狀態欄中的「無效」值的行的背景/前景。我在互聯網上發現了很多例子,但所有這些在我的程序中都是不可能的。有人能幫我嗎? 這是我的模型

String[] columnNames = {"Employee ID","Name", "Status", "Position"}; 
DefaultTableModel model = new DefaultTableModel(columnNames, 0); 

,這是創造我的表的方式,以及如何我從數據庫中獲取數據

public MyList(){//my constructor 
    frame(); 
    loadListFromDB(); 
} 
public void frame(){// 
    //codes for frame setsize,titles etc... 
    tblList = new JTable(); 
    tblList.getTableHeader().setPreferredSize(new Dimension(100, 40)); 
    tblList.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 25)); 
    tblList.setAutoCreateRowSorter(true); 
    tblList.setModel(model); 
    scrollPane.setViewportView(tblList); 
    loadListFromDB(); 

} 
public void loadListFromDB(){ 
    String sql = "SELECT emp_id,lname,fname,positional_status from tblEmployee"; 
    try{ 
     PreparedStatement ps = conn.prepareStatement(sql); 
     ResultSet rs = ps.executeQuery(); 
     while (rs.next()){ 
      Vector row = new Vector(); 
      for (int i = 1; i <= 4; i++){ 
       row.addElement(rs.getObject(i)); 
      } 
      model.addRow(row); 
     } 
    }catch(Exception err){ 
     //for error code 
    } 
} 

我怎麼想加入這樣的tableredered?任何人都可以舉個簡單的例子來改變行的顏色?在此先感謝..我的程序停在這個問題上。

+0

可能重複http://stackoverflow.com/questions/3548986/how-do-i-set-the-jtable-column-and -row-color) – DavidPostill

+0

但是數據剛剛從一開始就被初始化了,我的數據來自數據庫,我不知道如何把我的數據放在字符串[] []的基礎上。 –

+0

你的問題不在於把數據放在表格中,而是改變行的顏色。您應該查看示例中的TableCellRenderer部分並將渲染添加到您的表中。 – DavidPostill

回答

14

「我想改變它的值爲行的背景/前景‘’在狀態欄」

不活躍這真的只是一個正從表/模型中的價值的問題。如果該行的狀態爲非活動狀態,則爲該行中的每個單元格設置背景/前景。由於渲染器是針對每個單元渲染的,因此基本上需要獲取[row][statusColumn]的值,並且這將是每行的狀態值。像

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){ 
    @Override 
    public Component getTableCellRendererComponent(JTable table, 
      Object value, boolean isSelected, boolean hasFocus, int row, int col) { 

     super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); 

     String status = (String)table.getModel().getValueAt(row, STATUS_COL); 
     if ("active".equals(status)) { 
      setBackground(Color.BLACK); 
      setForeground(Color.WHITE); 
     } else { 
      setBackground(table.getBackground()); 
      setForeground(table.getForeground()); 
     }  
     return this; 
    } 
}); 

的東西在這裏是一個簡單的例子

enter image description here

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

import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 

public class TableRowDemo { 

    private static final int STATUS_COL = 1; 

    private static JTable getTable() { 
     final String[] cols = {"col 1", "status", "col 3"}; 
     final String[][] data = { 
       {"data", "active", "data"}, 
       {"data", "inactive", "data"}, 
       {"data", "inactive", "data"}, 
       {"data", "active", "data"} 
     }; 
     DefaultTableModel model = new DefaultTableModel(data, cols); 
     return new JTable(model) { 
      @Override 
      public Dimension getPreferredScrollableViewportSize() { 
       return new Dimension(350, 150); 
      } 
     }; 
    } 

    private static JTable getNewRenderedTable(final JTable table) { 
     table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){ 
      @Override 
      public Component getTableCellRendererComponent(JTable table, 
        Object value, boolean isSelected, boolean hasFocus, int row, int col) { 
       super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); 
       String status = (String)table.getModel().getValueAt(row, STATUS_COL); 
       if ("active".equals(status)) { 
        setBackground(Color.BLACK); 
        setForeground(Color.WHITE); 
       } else { 
        setBackground(table.getBackground()); 
        setForeground(table.getForeground()); 
       }  
       return this; 
      } 
     }); 
     return table; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       JOptionPane.showMessageDialog(null, new JScrollPane(getNewRenderedTable(getTable()))); 
      } 
     }); 
    } 
} 

另一種選擇是@Override表prepareRenderer。它會給你同樣的結果。

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

import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellRenderer; 

public class TableRowDemo { 

    private static final int STATUS_COL = 1; 

    private static JTable getTable() { 
     final String[] cols = {"col 1", "status", "col 3"}; 
     final String[][] data = { 
       {"data", "active", "data"}, 
       {"data", "inactive", "data"}, 
       {"data", "inactive", "data"}, 
       {"data", "active", "data"} 
     }; 
     DefaultTableModel model = new DefaultTableModel(data, cols); 
     return new JTable(model) { 
      @Override 
      public Dimension getPreferredScrollableViewportSize() { 
       return new Dimension(350, 150); 
      } 
      @Override 
      public Component prepareRenderer(TableCellRenderer renderer, int row, int col) { 
       Component c = super.prepareRenderer(renderer, row, col); 
       String status = (String)getValueAt(row, STATUS_COL); 
       if ("active".equals(status)) { 
        c.setBackground(Color.BLACK); 
        c.setForeground(Color.WHITE); 
       } else { 
        c.setBackground(super.getBackground()); 
        c.setForeground(super.getForeground()); 
       } 
       return c; 
      } 
     }; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       JOptionPane.showMessageDialog(null, new JScrollPane(getTable())); 
      } 
     }); 
    } 
} 
的[如何設置JTable中列和行顏色?(
+0

非常感謝,這正是我正在尋找... – Zohaib

+0

很好的答案我一直在尋找。 我們可以同時更改表頭的樣式嗎? –

+0

@peeskillet如何修改prepare renderer方法以更改列而不是行的背景顏色? – Aequitas