所以我試圖改變一行中的顏色到jtable中的紅色取決於單元格的結果 把這段代碼放在下面,應該工作但注意改變試圖改變Jtable中的一行的顏色java
她是代碼,如果任何人能做到就可以了一些修改,這將是偉大的,謝謝
public class Test1 extends JFrame {
private JPanel contentPane;
private JTable table;
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);
int status = (int)table.getModel().getValueAt(row, 4);
if (status<=0) {
setBackground(Color.RED);
setForeground(Color.BLACK);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
});
return table;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
try
{ Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
String query="select * from employes";
PreparedStatement pst=con.prepareStatement(query);
ResultSet rs=pst.executeQuery();
ResultSetMetaData rsm=rs.getMetaData();
int c=rsm.getColumnCount();
Vector column=new Vector(3);
column.add(rsm.getColumnName(1));
column.add(rsm.getColumnName(2));
column.add(rsm.getColumnName(3));
column.add("date");
Vector row=new Vector();
Vector data=new Vector();
while(rs.next())
{
row=new Vector();
row.add(rs.getString("id"));
row.add(rs.getString("nom"));
row.add(rs.getString("prenom"));
Timer time=new Timer(rs.getString("date_de_embauche").toString());
int j=(int) time.getResulte();
row.add(j);
data.add(row);
}
table = new JTable(data,column);
}
catch(Exception e)
{
System.out.println(e);
}
table.setBounds(33, 28, 335, 203);
contentPane.add(table);
}}
考慮讓渲染器不透明。另外,永遠不要這樣做:'contentPane.setLayout(null); ' –
重複檢查兩種方法之一[這裏](http://stackoverflow.com/q/24848314/230513)。 – trashgod
你似乎從來沒有cal'getNewRenderedTable' – MadProgrammer