0

ANSWER: Im not allowed to post answers... thumbs up stackoverflow!!!

But here it is:

Holy freakin'....

I made it, actualy it was easier than i have thought....

Here's my solution:

tbl_needaction = new javax.swing.JTable()
    {
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
        {
            Date d = new Date();
            DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
            java.util.Date acdate = null;
            Component c = super.prepareRenderer(renderer, row, column);

            //  Color row based on a cell value

            if (!isRowSelected(row))
            {
                c.setBackground(getBackground());
                int modelRow = convertRowIndexToModel(row);
                String sd = "";
                sd = (String)getModel().getValueAt(modelRow, 5);
                try {
                    acdate = df.parse(sd);
                } catch (ParseException ex) {
                    Logger.getLogger(EditApplicationJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (d.compareTo(acdate)>=0){
                        c.setBackground(Color.RED);
                }
            }

            return c;
        }
    };

I had to edit this via NotePad++ because NetBeans dosn't allow me to edit the automaticly generated initComponents().


QUESTION: I know, there are lots and lots examples and tutorials for this issue, but none of them seems to work for me....

Im getting data from a sql-database which i show in a JTable. There is a date called "ActionPoint". Now i want to mark every row red where the "ActionPoint" equals today oder is "smaller" than today.

My Code to conpare today with the "ActionPoint" for ebery row in my jTable:

for(int row = 0;row < dbApplicantsTableModel.getRowCount();row++) {

            String sd = "";
            sd = (String) dbApplicantsTableModel.getValueAt(row, 5);
            try {
                acdate = df.parse(sd);
            } catch (ParseException ex) {
                Logger.getLogger(EditApplicationJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (acdate.compareTo(d)<=0){

            }


    }

So i should have my "row" which now should be painted red.

Can anyone provide a Method which simply gets a row, and then sets the background of this certain row red?

EDIT:

Now my code looks like this:

for(int row = 0;row < dbApplicantsTableModel.getRowCount();row++) {


            String sd = "";
            sd = (String) dbApplicantsTableModel.getValueAt(row, 5);
            try {
                acdate = df.parse(sd);
            } catch (ParseException ex) {
                Logger.getLogger(EditApplicationJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (acdate.compareTo(d)<=0){
                dbApplicantsTableModel.setRowColour(row, Color.RED);
            }

But ist doesnt set any backgrounds red!

Sadly i need a reputation of 10 to post images -.-

1 Answers1

0

i want to mark every row red where the "ActionPoint" equals today oder is "smaller" than today.

Check out Table Row Rendering for an easy approach to solve this problem. The approach overrides the prepareRenderer(...) method of the JTable, so you can add you logic in one place with a simple "if statement".

camickr
  • 321,443
  • 19
  • 166
  • 288