Color a distinct field in a Jtable While Data is inserted from SQL

怎甘沉沦 提交于 2020-08-09 09:03:45

问题


i have Jtable in Java that inserting data from SQL

What i need to do if the Field Nummber 3 is = to 0

i need to set the backround of it to be Red and others is Not

Any Help ?

NoSelect:

WithSelect:

Edit : Try to use a different method to change the color using

TableCellRenderer RederColer = (JTable table, Object value, boolean isSelected, boolean hasFocus, int row1, int column) -> {

        if (table.getModel().getValueAt(row, 2).equals("0")) {
            setForeground(Color.RED);
        } else {
            setForeground(Color.white);
        }

        throw new UnsupportedOperationException("Not supported yet.");
    };

    model.setRowCount(0);
    jTable1.removeAll();
    Counter = 0;

    if ("En".equals(Lang)) {
        this.jButton2.setText("Exit");
    }

    db.DBConnect();

    try {

        String query = "SELECT * FROM `required_production` ORDER BY `required_production`.`DEAD_LINE` ASC";

        db.rs = db.st.executeQuery(query);

        Object[] columns = {call1, call2, call5, call3, call4};

        model.setColumnIdentifiers(columns);

        jTable1.setBackground(Color.white);
        jTable1.setForeground(Color.black);
        jTable1.setSelectionBackground(Classes.Setting.greenDF);
        jTable1.setRowHeight(30);
        jTable1.setFont(Classes.Setting.font);
        jTable1.setRowSelectionAllowed(true);
        jTable1.setDefaultEditor(Object.class, null);

        jTable1.setModel(model);

        Counter = 0;

        while (db.rs.next()) {

            model.addRow(new Object[]{db.rs.getString("REQUIRED_PRODUCT"), db.rs.getString("REQUIRED_NUMBER"), db.rs.getString("REMAINING_NUMBER"), db.rs.getString("REQUIRED_COLOR"), db.rs.getString("DEAD_LINE")});

            IDIn[Counter] = db.rs.getString("ID");

            Counter = Counter + 1;

             jTable1.getColumnModel().getColumn(2).setCellRenderer(RederColer);

        }

    } catch (SQLException error) {

    }

enter code here

来源:https://stackoverflow.com/questions/62271937/color-a-distinct-field-in-a-jtable-while-data-is-inserted-from-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!