Make header text bold when its column is selected in JTable - java

is possible to turn header text bold when I select a cell of its column? How to do it?
Thanks
Leandro

Set custom renderer into table header. If current column is selected column set font to bold.
JTable table = new JTable()
table.getTableHeader().setDefaultRenderer(new MyRenderer());
class MyRenderer implements TableCellRenderer {
//todo implement
}

It is partially possible. What you have to do is write your model is such way that on table's cell selection your model executes fireTableStructureChanges method( assuming you use AbstractTableModel as a base). This will repaint the whole table including column header. All you have to do is keep the state of which column is selected.
In the beginning I said "partially" possible. That is because calling fireTableStructureChanges will revalidate the whole table and you will lose your current column model state - column widths and sequence.
To make your text bold you can use HTML - something like <html><b>your text</b></html>, but it has to change dynamically based on your model's internal state
UPDATE: Also column table header text can be set directly but model change or tableStructureChanged event will make the table to reread from the model.

Related

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.)

FEST: Retrieving a cell value when cell is under a JTable with CellRenderPane

I have code like this:
//(...)
JTableFixture myTreeTable = frame.table(matcher);
If I try to obtain JCellFixtures or values or contents, everything is null. I am only obtaining the number of rows or columns. JTable internally uses a CellRendererPanel which I guess I have to fetch. But how? JTable has no ContainerFixture to make .panel(). Is there any way to get the cell values in these circumstances? How it is usually done when there is a cell renderer panel?
This is the hierarchy for the FEST debugger:
gui.treetable.myTreeTable[name=null, rowCount=33, columnCount=2, enabled=true, visible=true, showing=true]
javax.swing.CellRendererPane[,0,0,0x0,hidden]
If you table has a custom cell renderer you can provide your your own cell reader. Check out Custom Cell Renderers article. It demonstrates how to extend BasicJTableCellReader and how to use the reader in JTableFixture.

how to add a jcheckbox when the table model initially is not defined.

how to add a jcheckbox when the table model initially is not defined.
I have populated the jtable with data from database, but now I need to add a jcheckbox used for selection of desired record.
I can add jcheckbox to jtable if the datatable rows are perdefined with boolean class. But as I am using a database to read from I am bit confused.
any help is welcome.
JTable relies on its TableModel to determine what to display. Although only existing rows can be shown, you can insert an uncommitted row in the model for editing. What and when to commit are entirely up to your application.
As discussed in How to Use Tables: Concepts: Editors and Renderers, the default renderer and editor for a column of type Boolean is a JCheckBox. Here's a simple example that conditions getColumnClass() accordingly. Here's a more complex example that defines a composite type, Value, as well as a custom renderer and editor.

JTable how to hide column in view but keep it in model for use it in background

I have a JTable and am passing a data array into the TableModel. I wish to retain all columns in the data model as I need them all for background data processing, but I wish to show only some of the columns in the JTable.
How do I achieve this -- to remove a data column from the view (the visualized JTable) but not from the model (the TableModel)?
JTable has
public void removeColumn(TableColumn aColumn)
Override getColumnCount of the model to return less columns that there is. Then put your hidden columns as last columns of the model.

Creating a table with an initial element selected

I want to create a table which displays in each row a set of data, in addition to a radio button for possible selection.
This can be achieved easily by using a simple table model class which extends the DefaultTableModel and a cell renderer and editor class which implements the TableCellRenderer and TableCellEditor interfaces respectively.
What I really couldn't manage to do is to make the table to display initially one of the elements (rows) initially selected. It seems straightforward but it isn't...does anybody have a clue about it?
Depends on your exact requirement.
table.changeSelection(2, 0, false, false);
will select the row and make the cell have focus in the table
Use the setSelectionInterval() method of the table's ListSelectionModel:
table.getSelectionModel().setSelectionInterval(4, 5);

Categories

Resources