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.

查看“表行渲染”以获取示例,该示例显示如何根据行中的数据为行着色。

更多相关文章

  1. 我无法让这个简单的ajax代码工作
  2. javascript实现拖动层效果代码(许愿墙)
  3. 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchroniz
  4. 在java自动生成hashCode代码问题? 请大神赐教
  5. 转贴:JavaScript实现Ajax请求简单示例
  6. 如何修改代码,使其仅打印单元格E和F?
  7. Java常量表达式相关的编译优化代码
  8. java代码获知该方法被哪个类、哪个方法、在哪一行调用
  9. 我的构建可以规定我的代码覆盖范围永远不会变得更糟吗?

随机推荐

  1. Android 修改开机动画(bootanimation)
  2. Android 支持网络协议以及简单用法
  3. Android 基础知识点(持续更新)
  4. android强制隐藏软键盘以及取消EditText
  5. Android Studio 导入so
  6. uiautomatorviewer.bat的使用
  7. 获取apk信息工具(android SDK的aapt工具)
  8. android多框架实现短视频应用、3D手势旋
  9. 自定义Dialog的几种实现方式
  10. Android开发人员必备的10 个开发工具