試圖改變Jtable java中行的顏色

[英]trying to change the color of a row in a Jtable java


so i was trying to change the color of a row to red in a jtable in depending on the outcome of a cell put this code down below and should work but noting change

所以我試圖在jtable中將行的顏色更改為紅色,具體取決於將此代碼放在下面的單元格的結果,並且應該有效但注意到更改

her is the code and if any one can do some modification on it that would be great and thank you

她是代碼,如果任何人可以對它進行一些修改那就太棒了,謝謝你

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);
}}

2 个解决方案

#1


3  

table = new JTable(data,column);

The problem with your current code is that you create a new JTable. Whenever you create a new table you lose the custom renderers. So you need to add the renderer to the table AFTER you create the table. I don't see where you invoke your getNewRenderedTable(...) method to add the renderer.

您當前代碼的問題是您創建了一個新的JTable。無論何時創建新表,都會丟失自定義渲染器。因此,您需要在創建表后將渲染器添加到表中。我沒有看到你在哪里調用你的getNewRenderedTable(...)方法來添加渲染器。

Another problem is that you should use a JScrollPane to display the table and then add the scrollpane to the frame.

另一個問題是您應該使用JScrollPane來顯示表,然后將滾動窗格添加到框架中。

Finally don't use a null layout. Swing was designed to be used with layout managers.

最后不要使用null布局。 Swing旨在與布局管理器一起使用。

i was trying to change the color of a row to red in a jtable in depending on the outcome of a cell

我試圖根據單元格的結果將行的顏色更改為jtable中的紅色

A JTable may use different renderers for each type of data in the columns. Instead of creating custom renderers for each data type you can do row level renderering by override the prepareRenderer(...) method of JTable.

JTable可以為列中的每種類型的數據使用不同的渲染器。您可以通過覆蓋JTable的prepareRenderer(...)方法來執行行級渲染,而不是為每種數據類型創建自定義渲染器。

The basic structure of the code would be:

代碼的基本結構是:

JTable table = new JTable(...)
{
    public Component prepareRenderer(
        TableCellRenderer renderer, int row, int column)
    {
        Component c = super.prepareRenderer(renderer, row, column);

        //  add custom rendering here

        return c;
    }
};

Check out Table Row Rendering for an example that shows how to color rows based on data in the row.

查看“表行渲染”以獲取示例,該示例顯示如何根據行中的數據為行着色。

#2


0  

try this code:

  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());
        }  

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/11/29/2fa438c591024b2de5ae4b3c4e3d690d.html



 
  © 2014-2022 ITdaan.com 联系我们: