Java JTable -- Highlight a certain entry when an external variable changes - java

I have a JTable such that:
ID #: Name:
0 Entry 0
1 Entry 1
2 Entry 2
When a variable:
private int HighlightEntryID = 2;
is set to an ID #, the JTable should then be able to highlight the corresponding entry.
I have created a Custom Cell Renderer and with the following code:
TableColumn column = CISTable.getColumnModel().getColumn(0);
column.setCellRenderer(new CustomCellRenderer());
I render cells based on the ID # column. My questions are:
It only highlights a specific (row, column) entry. I want to highlight the whole row. How do I highlight other columns in the same row?
When the variable: HighlightEntryID is changed, the JTable doesn't automatically reflect the change. I must refresh the table manually. How do I refresh it automatically?

It only highlights a specific (row, column) entry. I want to highlight the whole row. How do I highlight other columns in the same row?
I like to use Table Row Rendering for this.
When the variable: HighlightEntryID is changed, the JTable doesn't automatically reflect the change. I must refresh the table manually. How do I refresh it automatically?
Invoke the following to force repainting of the entire table:
table.repaint();

You can use setDefaultCellRenderer(Object.class, new CustomCellRenderer()) method of JTable..
And also you should put highlightEntryID variable to your table class..

Related

Change background color of rows of one table when row of another table is clicked

I have two JTable components, I want that if i click on any row of first table then, the elements having same ID in table 2 also, changes their background color.
I searched these while R&D
Java Code Examples for javax.swing.JTable.prepareRenderer()
Change background color of one cell in JTable
Add a ListSelectionListener to the first table. It will generate an event whenever the selection of a row is changed
When the event is generated you clear the selection of the second table and then iterate through all the rows of the table finding rows with the same id. Then you can invoke the changeSelection(...) method on that table.

JTable: changing column based on outside input

I have a custom table class which extends JTable. I want a column to be visible/invisible based on a checkbox. I have an actionlistener on the checkbox that changes the max, min, and preferred width of the column in action.
When the checkbox is NOT selected, then I set the min/max/preferred width to 0. When it is selected, I set the min/max/preferred width to a normal size.
My issue is, when I click to select the checkbox, the table does not change. I do not see the column. However, when I resize the application or any of table columns, the table gets updated and my new column is now visible properly.
I tried calling "table.repaint()", and that did not do anything. My ideal approach now would be to just figure out what gets called during an application/column resize that causes the update, and just call it from my listener.
I have an actionlistener on the checkbox that changes the max, min, and preferred width of the column in action.
Don't play with the column size. The column will still be part of the table and will confuse the user as they tab from column to column since the cursor will disappear on the minimized column.
Instead you want to remove the column from the view of the table. So you need to remove a TableColumn from the TableColumnModel of the table.
Check out the Table Column Manager. This allows you to hide/show columns as required.

Select only column header and no row in an SWT table after click on column header

I have an Eclipse SWT table. It contains multiple rows (with row headers) and multiple columns (with column headers). On click on a column header I want to deselect all rows and highlight only the column's header (or all cells of this column). Is that possible?
I already registered a listener for the column header's selection and am able to set the selection to the given column, but then it also always selects the first row automatically. I tried with SWT.FULL_SELECTION and SWT.SINGLE as the style of my table, but it only changes the way how the row is highlighted, but I want only to highlight the column's header (or optionally all cells of this column) and no rows.
Is it possible? Did anyone have the same problem?
I ran into the same problem and I'm afraid you can't do that in SWT.
I solved by imitating the selection of a column; I colored the cells as if they were selected, and dropped the "real" selection.
You should extend your own TableViewer and override the getSelection() method to reflect that you now also have this imitated kind of selection.
(And from a UX perspective, you should make sure that a copy command (i.e. Ctrl+C) behaves as users expect it.)

How to hide a particlar column in DefaultTableModel from displaying it in table?

I am using Java Swingx framework. I have 4 columns in my DefaultTableModel object. I wish to display only 3 of the columns. But, I need all four for computing.
Actual data model
S.No. | ID | GDC ID | Decsription
What I want to display in table
S.No.| GDC ID | Decsription
Is it possible to hide or omit only one column from rendering? Please guide me.
by default minimum size is 10 pixels widht,
you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)
No need to adjust your model, or to try to make that column very small. JTable has built-in functionality for this: the removeColumn method. As stated in the javadoc of that method
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
Also note the existence of the following methods:
JTable#convertColumnIndexToModel
JTable#convertColumnIndexToView
Since the column order and column count in the view (the JTable) might be different from the one in the model you need those methods to switch between view and model
You can hide it by setting its width to 0.
_table.getColumn("ID").setPreferredWidth(0);
_table.getColumn("ID").setMinWidth(0);
_table.getColumn("ID").setWidth(0);
_table.getColumn("ID").setMaxWidth(0);
try this to remove a single column:
myTableModel = new DefaultTableModel();
myTableModel.setColumnIdentifiers(new Object[]{"S.No.", "ID", "GDC ID", "Decsription"});
JTable myTable = new JTable(myTableModel);
// remember to save the references
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
TableColumn myTableColumn2 = guiLoteryNumbersTable.getColumnModel().getColumn(2);
TableColumn myTableColumn3 = guiLoteryNumbersTable.getColumnModel().getColumn(3);
myTable.getColumnModel().removeColumn(myTableColumn1);
Then to show the column again keeping the order:
// 1- remove all the existent columns
myTable.getColumnModel().removeColumn(myTableColumn0);
myTable.getColumnModel().removeColumn(myTableColumn2);
myTable.getColumnModel().removeColumn(myTableColumn3);
// 2- add all the columns again in the right order
myTable.getColumnModel().addColumn(myTableColumn0);
myTable.getColumnModel().addColumn(myTableColumn1);
myTable.getColumnModel().addColumn(myTableColumn2);
myTable.getColumnModel().addColumn(myTableColumn3);
Sorry, but that's the best way I know.
You manipulate the getValueAt , getColumnCount to achieve this.
So for example the getColumnCount you give it as 3
and on getValueAt - to skip if the column index is above the skipped column
JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.
NOTE: Ignore this and see link by trashgod below.

Moving JTable rows downwards when new row is added to the table

I have to maintain list of events generated by the user in a JTable.The JTable consists of 2 columns Time and event and 5 rows. When 1st event is generated it's data must be placed in 1st row of JTable, when 2nd event is generated the data in 1st row must be moved to 2nd row and the data of 2nd event must be place in the 1st row of the JTabel and so on up to 5th row. There is no limit to the events(n events can be generated). But i have to display only 5 events in JTable and older once will be stored in text file. I have tried some logics but they are failed to work.
This is my problem. Can any one give me an idea how to move rows downwards...
Regards...
Upendra.S
You simply add your new row at index 0 in your TableModel. If your TableModel extends from DefaultTableModel, you can even use the available insertRow method.
For the requirement to only show 5 rows, you simple remove the extra row from your TableModel.
Consult the JTable tutorial for more information
have look at JTable tutorial
part TableModel
method from DefaultTableModel insertRow()

Categories

Resources