1

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:

https://i.stack.imgur.com/E3Gzu.jpg

WithSelect:

https://i.stack.imgur.com/AOQgM.jpg

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
  • 1
    You need to use a custom renderer for the column. Check out: https://stackoverflow.com/questions/42179972/change-color-jtable-cells-with-gettablecellrenderercomponent-nothing-happends/42180634#42180634 for a similar example you can customize for your exact requirement. – camickr Jun 08 '20 at 22:25
  • i used it but nothink chenge ! i edite my post –  Jun 08 '20 at 22:30
  • You didn't use the code I suggested. The editor is not the same as a renderer. Note the example changes the background, you will want to change the foreground color (even if the row is selected). – camickr Jun 08 '20 at 22:40
  • Edit :- I Used the code but i think im using it rong cuse it is Error = `Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet. at Forms.production_management.lambda$formWindowOpened$0(production_management.java:202) at java.desktop/javax.swing.JTable.prepareRenderer(JTable.java:5741)` –  Jun 08 '20 at 22:55
  • 1) *"I Used the code but i think im using it rong cuse it is Error"* You seem to have no idea what the code automatically inserted by the IDE, does. It is being caused by a line like this `throw new UnsupportedOperationException("Not supported yet.");` (copy/pasted from the above). 2) `} catch (SQLException error) { }` Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` – Andrew Thompson Jun 09 '20 at 00:11
  • *but i think im using it rong* - 1) I did not suggest to use a lambda.2) where is the code to invoke `super.getTableCellRendererComponent(…)`? Start with the working code from the provided link and customize it for your requirement first. Then once you prove the concept works you fit the code into your real application. – camickr Jun 09 '20 at 00:19

0 Answers0