Refreshing background color for a row in jtable?

For example, import java.awt. *; import java.awt.event. ActionEvent; import java.awt.event.

ActionListener; import java.util. Random; import java.util. Vector; import javax.swing.

*; import javax.swing.event. ListSelectionEvent; import javax.swing.event. ListSelectionListener; import javax.swing.event.

TableModelEvent; import javax.swing.event. TableModelListener; import javax.swing.table. AbstractTableModel; import javax.swing.table.

TableCellRenderer; public class Forum implements ListSelectionListener { private JFrame frame = new JFrame("Frame"); private JPanel fatherCenter = new JPanel(); private JScrollPane tableScroll = new JScrollPane(); private myTableModel tableModel; private JTable dialogTable; private JButton blueButton; private ListSelectionModel lsDialog; private Color clr; private Color clr1; private void addComponentsToPane(Container pane) { tableModel = new myTableModel(); dialogTable = new JTable(tableModel) { private static final long serialVersionUID = 1L; @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component comp = super. PrepareRenderer(renderer, row, column); JComponent jc = (JComponent) comp;//for Custom JComponent if (!isRowSelected(row)) { int modelRow = convertRowIndexToModel(row); boolean type = (Boolean) getModel(). GetValueAt(modelRow, 2); boolean type1 = (Boolean) getModel().

GetValueAt(modelRow, 3); comp. SetForeground(Color. Black); if ((type) && (!type1)) { comp.

SetBackground(clr1); } else if ((!type) && (type1)) { comp. SetBackground(Color. Orange); } else if ((!type) || (!type1)) { comp.

SetBackground(Color. Red); } else { comp. SetBackground(row % 2 == 0?GetBackground() : getBackground().darker()); } dialogTable.

ConvertRowIndexToView(0); } else { comp. SetForeground(Color. Blue); } if (!isCellEditable(row, column)) { comp.

SetForeground(Color. Red); comp. SetBackground(Color.

Magenta); } return comp; } }; tableScroll = new JScrollPane(dialogTable, ScrollPaneConstants. VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants. HORIZONTAL_SCROLLBAR_AS_NEEDED); tableScroll.

SetBorder(null); dialogTable.getTableHeader(). SetReorderingAllowed(false); dialogTable. SetSelectionMode(ListSelectionModel.

SINGLE_SELECTION); lsDialog = dialogTable. GetSelectionModel(); dialogTable. PutClientProperty("terminateEditOnFocusLost", Boolean.

TRUE); dialogTable. SetRowHeight(20); dialogTable. SetRowMargin(2); ListSelectionModel rowSelMod = dialogTable.

GetSelectionModel(); //ListSelectionModel colSelMod = dialogTable.getColumnModel(). GetSelectionModel(); rowSelMod. AddListSelectionListener(this); //colSelMod.

AddListSelectionListener(this); blueButton = new JButton(" Blue BackGround "); blueButton. SetPreferredSize(new Dimension(100, 30)); blueButton. AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (clr == Color.

White) { clr = Color. Orange; clr1 = Color. Yellow; } else { clr = Color.

White; clr1 = Color. Black; } System.out. Println(clr); dialogTable.

SetBackground(clr); dialogTable.repaint(); } }); fatherCenter = new JPanel(); fatherCenter. SetLayout(new BorderLayout(10, 10)); fatherCenter. Add(tableScroll, BorderLayout.

CENTER); fatherCenter. Add(blueButton, BorderLayout. SOUTH); pane.

Add(fatherCenter); } private void addData() { Runnable doRun1 = new Runnable() { @Override public void run() { tableModel.resetTable(); Vector tbl = new Vector(); Vector tbl1 = new Vector(); Random rnd = new Random(); tbl. Add("Integer"); tbl. Add("Double"); tbl.

Add("Boolean"); tbl. Add("Boolean"); tbl. Add("String"); tableModel.

SetColumnNames(tbl); for (int row = 0; row data; private Vector colNames; private boolean _columnsVisible = {true, true, true, true, true}; myTableModel() { this. ColNames = new Vector(); this. Data = new Vector(); } myTableModel(Vector colnames) { this.

ColNames = colnames; this. Data = new Vector(); } public void resetTable() { this.colNames. RemoveAllElements(); this.data.

RemoveAllElements(); } public void setColumnNames(Vector colNames) { this. ColNames = colNames; this. FireTableStructureChanged(); } public void addRow(Vector data) { this.data.

Add(data); //this. FireTableDataChanged(); //this. FireTableStructureChanged(); this.

FireTableRowsInserted(data.size() - 1, data.size() - 1); } public void removeRowAt(int row) { this.data. RemoveElementAt(row); //this. FireTableDataChanged(); this.

FireTableRowsDeleted(row - 1, data.size() - 1); } @Override public int getColumnCount() { return this.colNames.size(); } @Override public Class getColumnClass(int colNum) { switch (colNum) { case 0: return Integer. Class; case 1: return Double. Class; case 2: return Boolean.

Class; case 3: return Boolean. Class; default: return String. Class; } } @Override public boolean isCellEditable(int row, int colNum) { switch (colNum) { case 2: return false; default: return true; } } @Override public String getColumnName(int colNum) { return this.colNames.

Get(colNum); } @Override public int getRowCount() { return this.data.size(); } @Override public Object getValueAt(int row, int col) { Vector value = this.data. Get(row); return value. Get(col); } @Override public void setValueAt(Object newVal, int row, int col) { Vector aRow = data.

ElementAt(row); aRow. Remove(col); aRow. InsertElementAt(newVal, col); fireTableCellUpdated(row, col); } public void setColumnVisible(int index, boolean visible) { this.

_columnsVisibleindex = visible; this. FireTableStructureChanged(); } } }.

1 Now I see repaint(). – trashgod Aug 1 at 16:40 1 The custom TableModel code is not the best as the addRow, removeRowAt methods fire the wrong event. You should be using fireTableRowInserted and fireTableRowsDeleted respectively.

This is the most efficient as only affected rows in the table will be repainted. The fireTableStructureChanged is expensive as it will recreate the TableColumnModel along with all the TableColumns. This can be a problem if you have added any custom renderers or editor to the table as you would lose them.

– camickr Aug 1 at 18:58 @camickr thanks for excelent and valuable commment, bump ... -> that's maybe my problem with implementation for prepareEditor(), but that's another story.

It may sound wired myself answering to one of my questions. But I found another quck fix to my bug. But am not really sure about the reason.

I call a epaint just before the validate and everything works fine: deployTable.repaint(); deployTable.revalidate(); I think I have not really clear the difference within repaint and revalidate. Can anyone explain it to me?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions