Results 1 to 5 of 5
- 03-30-2012, 06:01 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
JTable validate already existing JTable data and set Background colour issue
Hello everyone
I wander whether you guys can help me as I goggled it all day and still cannot find any solution for my problem.
I have app that when started it will extract some info from file and put it to 2 JTables (see class below)
- Source
- Destination
The problem I have got now is that I have got another method called validate info which scan through both and validate media e.g.
Source 1 is same as Destination 1
Source 4 is same as Destination 2
What I need to do is to set red background color for the matching rows, but I am unable to do that
I have searched google for different example and I cannot find anything
Would anyone be able to help me with that ??
Java Code:package UI; import javax.swing.BorderFactory; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.util.Vector; import javax.swing.JPanel; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; /** * * @author ;) * @version 0.1 * @since 02.02.2012 * */ public class mainPanelUI { private JPanel contentJPanel; private JPanel contentTableHolder_JPanel; private JPanel buttonControl_JPanel; private JTable mainTable; public mainPanelUI.customTableModel customTableModel; public mainPanelUI.customTableRowColorRenderer customTableRowColorRendererOne; private JButton chooseJButton; public mainPanelUI() { customTableModel = new mainPanelUI.customTableModel(); contentJPanel = new JPanel(new BorderLayout()); contentTableHolder_JPanel = new JPanel(new BorderLayout()); buttonControl_JPanel = new JPanel(new BorderLayout()); chooseJButton = new JButton("..."); chooseJButton.setEnabled(true); customTableRowColorRendererOne = new mainPanelUI.customTableRowColorRenderer(); mainTable = new JTable(customTableModel); mainTable.setDefaultRenderer(Object.class, customTableRowColorRendererOne); mainTable.setRowHeight(mainTable.getRowHeight() + 5); mainTable.setShowHorizontalLines(false); mainTable.setShowVerticalLines(false); Object[] values = { false, "", "", "", "", "" }; customTableModel = (customTableModel) mainTable.getModel(); customTableModel.insertData(values); initColumnSizes(mainTable); contentTableHolder_JPanel.add(mainTable.getTableHeader(), BorderLayout.NORTH); contentTableHolder_JPanel.add(mainTable, BorderLayout.CENTER); buttonControl_JPanel.add(chooseJButton, BorderLayout.EAST); contentJPanel.add(contentTableHolder_JPanel, BorderLayout.WEST); contentJPanel.add(buttonControl_JPanel, BorderLayout.EAST); } /** * This method returns content JPanel object * @return JPanel */ public JPanel getContentJPanelObject() { return contentJPanel; } /** * This method returns chose JButton object * @return JButton */ public JButton getChooseJButtonObject() { return this.chooseJButton; } /** * This method sets JPanel title * @param jPanelBorderTitle */ public void setMainContentPanelBorderTitle(String jPanelBorderTitle) { contentJPanel.setBorder(BorderFactory.createTitledBorder(jPanelBorderTitle)); contentJPanel.revalidate(); } public mainPanelUI.customTableModel getCustomTableModel() { return customTableModel; } public void insertNewRow(String fileTitle, String fileExists, String filePath, String dllFileVersion, String dllProductVer) { Object[] newObject = { false, fileTitle, fileExists, filePath, dllFileVersion, dllProductVer }; customTableModel.insertData(newObject); contentJPanel.revalidate(); } public void removeALLRows() { customTableModel.removeALL(); Object[] newObject = { false, "", "", "", "", "" }; customTableModel.insertData(newObject); contentJPanel.revalidate(); mainUI.frame.pack(); } private void initColumnSizes(JTable table) { customTableModel model = (customTableModel) table.getModel(); TableColumn column = null; Component comp = null; int headerWidth = 0; int cellWidth = 0; Object[] longValues = model.longValues; TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); for (int i = 0; i < table.getColumnCount(); i++) { column = table.getColumnModel().getColumn(i); if (i == 2) { column.setPreferredWidth(50); } else if (i == 4) { column.setPreferredWidth(100); } else if (i == 5) { column.setPreferredWidth(100); } else { comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, longValues[i], false, false, 1, i); cellWidth = comp.getPreferredSize().width; column.setPreferredWidth(Math.max(headerWidth * 2, cellWidth * 2)); } } } public class customTableModel extends AbstractTableModel { private String[] columnNames = { "", "File Name ", "PDB File", "Path ", "File version", "Product version" }; private Vector data = new Vector(); public final Object[] longValues = { Boolean.TRUE, "", "", "", "", "" }; @Override public int getColumnCount() { return columnNames.length; } @Override public int getRowCount() { return data.size(); } @Override public Object getValueAt(int row, int col) { return ((Vector) data.get(row)).get(col); } @Override public String getColumnName(int col) { return columnNames[col]; } @Override public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } @Override public void setValueAt(Object value, int row, int col) { ((Vector) data.get(row)).setElementAt(value, col); fireTableCellUpdated(row, col); } @Override public boolean isCellEditable(int row, int col) { if (0 == col) { return true; } else { return false; } } /** * This method inserts single row to a jTable * @param values */ public void insertData(Object[] values) { data.add(new Vector()); for (int i = 0; i < values.length; i++) { ((Vector) data.get(data.size() - 1)).add(values[i]); } fireTableDataChanged(); } /** * This method removes all elements from object */ public void removeALL() { data.removeAllElements(); fireTableDataChanged(); mainUI.frame.pack(); } /** * This method removes one (specified) row from jTable * @param row */ public void removeRow(int row) { data.removeElementAt(row); fireTableDataChanged(); mainUI.frame.pack(); } } public class customTableRowColorRenderer extends DefaultTableCellRenderer { Color currColor = Color.white; public customTableRowColorRenderer() { setOpaque(true); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { super.setForeground(Color.BLACK); super.setBackground(Color.WHITE); } else { super.setForeground(Color.BLACK); super.setBackground(currColor); } setFont(table.getFont()); setValue(value); return this; } } }Last edited by JosAH; 03-30-2012 at 07:03 PM. Reason: changed [quote] ... [/quote] tags to [code] ... [/code] tags
- 03-30-2012, 07:07 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: JTable validate already existing JTable data and set Background colour issue
Use your custom renderer to set the background color of the appropriate cells. You will need to set a flag someone in your model for values which are invalid, then evaluate this flag in the renderer and set the color appropriately.
- 03-31-2012, 01:11 AM #3
Re: JTable validate already existing JTable data and set Background colour issue
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-02-2012, 06:15 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 19
- Rep Power
- 0
Re: JTable validate already existing JTable data and set Background colour issue
Hello doWhile
Thank you very much for you replay doWhile , unfortunately there is a small problem. I am gathering data about these things first then display them in the tables with (setForeground(Color.BLACK); & setBackground(Color.WHITE);). When all data is inserted to a tables I want to validate it based on inserted data and change background colour accordingly.
Is there a way to do that way ??
- 04-02-2012, 07:57 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: JTable validate already existing JTable data and set Background colour issue
a) Why do you need to validate after all the data is loaded? Validation should occur based upon the model, not loading into the view b) If you wish to change the presentation of the view through the renderer, then just call repaint() on the JTable
Similar Threads
-
Need guidance on how to change colour of a row in JTable.
By rhexis in forum New To JavaReplies: 8Last Post: 02-17-2012, 05:42 AM -
NetBeans GUI editor (drag/drop) and JTable data issue
By Ceryph in forum AWT / SwingReplies: 6Last Post: 08-16-2011, 09:17 AM -
Jtable colour cell problem
By fuzzdn in forum New To JavaReplies: 10Last Post: 07-30-2011, 05:30 PM -
Updating the existing JTable !
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-07-2010, 08:38 PM -
jtable cell background
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 02-22-2010, 04:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks