JTable with merged column headers - java

I'm working developing a system and I need to create a special table take a look:
Row headers and column titles are always static.
Is it possible to create that kind of table with Netbeans?

Please follow the link about how to add Nested Table Header.

Related

Create JTable header without using new JTable(data,columnNames)

I've read how to create a JTable on JScrollPane with data in code, but how can I create a empty table with column header without adding data to it?
I want to add data to it from a form - not from my code.
If you use Netbeans for Development, it will be quite easy to work with JTables.
Using Netbeans, simply select Table from the palate and put it wherever you wish it to display.
Then in properties, you can change column names, add new columns and delete older. Also there only you will get an option of rows to display, simply put that to zero (0).
Voila! Your table is ready with column names and No Rows.

How to auto-refresh a JTable bound to Mysql database after insert using NetBeans

I'm working on a project in NetBeans.
I have a Jtable which i bound to my database and there is a form that I use to insert data.
I want the records in the table to be refreshed with each insert.
How can I do that?
I want the records in the table to be refreshed with each insert.
Then your "insert" logic need to do two things:
insert the data into the database
insert the data into the JTable. This is done by using the addRow(...) method of the DefaultTableModel. You get the data from the form, create a Vector to contain the data for each column then you add the Vector to the model.
I should simply select the table and go to the navigator window. Under "Other Components" there is the list that was created after the binding and contains the table records (you should know its name). Right click on the list > Properties > check "observable".
Please try the following code:
if you want update the table on button click or on event of any component
you have to put the code in the event
the list generated should be Observable checked
code
udetailsList.clear();
udetailsList.addAll(udetailsQuery.getResultList());

How to create jCheckBox depending on number of rows present in database?

I want to create a frame in java with variable number of checkboxes which will depend on the number of rows present in my database.
Problems that I am facing are:-
1. How to count the number of rows in database?
2. How and where to store the data present in columns of database?
3. How to create checkboxes with that data present in column of database?
Can anyone help me.??
Looking forward for solution.
Thanks in advance :)
How to count the number of rows in table?
SELECT COUNT(*) FROM table_name;
How and where to store the data present in columns of database?
I would suggest you to use JTable and look the tutorial How to use Table
How to create checkboxes with that data present in column of
database?
Read about Renderer and Editor section to know more about registered cell renderers in JTable.

JTable model column

I have a JTable and a Model for that table.
Now I want to change the order of the columns and to hide or show some columns (e.g. like Windows Explorer in "Detail View" via Menu on rightclick).
My first problem here is, the getColumnName function. Do I have to keep track of, which column is at which place and then return the right columnName or is this already part of the model?
Same for the getValueAt function. Can I always return the value for the first column if I get columnIndex = 0, even if the user has dragged this column to the end of the table?
And nearly the same problem for adding/removing columns. If I do that, of course I have to fireTableStructureChanged, but do I also have to adapt e.g. the getColumnName function?
I haven't found a tutorial for that. All tutorials stop at "you can use a model".
I'd really like to see an example of such a dynamic model.
Thanks a lot.
You should use the getColumn(int) method of the model, and for accessing the model, you'll need to convert the row and column view indices with JTable's convertRowIndexToModel(int), convertColumnIndexToModel(int) and the equivalents for converting the model indices to view indices.
You need to understand the difference between the "View" and the "Model". When you reorder the columns in the JTable (view), this does not change the order of the data in the model.
If you want to access the first column that is displayed in the table you use:
table.getValueAt(row, 0);
if you want to access the first column in the model, then you use:
table.getModel().getValueAt(row, 0);
I want to hide or show some columns
See Table Column Manager.

Dynamically changing the column header text in JTable

I have a table with 3 columns which have the following values in the headers: 'No.', 'X [mm]', 'Y [mm]'. This table contain the coordinates of points in millimeters. I have a checkbox on checking of which the table should repopulate to show the coordinates in inches. Moreover, the column header values should be: 'No.', 'X [in]', 'Y [in]'.
In short I want to dynamically change the header text of the table.
In detail:
The table is a subclass of JTable. Moreover, a subclass of 'DefaultTableModel' has been set as the model for the table. I have provided the header values in the constructer of the datamodel subclass.
Any idea? My application is compatible with only jdk v1.4 so it would be good if the solution is compatible with the verion :)
You can update the TableColumnModel directly:
JTableHeader th = table.getTableHeader();
TableColumnModel tcm = th.getColumnModel();
TableColumn tc = tcm.getColumn(0);
tc.setHeaderValue( "???" );
th.repaint();
If you have column number use that code
jtable.getColumnModel().getColumn(5).setHeaderValue("newHeader");
I can't test here but familiar that this method '[DefaultTableModel.setColumnIdentifiers(...)][1]' should do what you want.
Basically, you run 'DefaultTableModel.getColumnCount()' to find out how many column (unless you already know). Then you run 'DefaultTableModel.getColumnName(int ColumnIndex)' to get the name of each, change it the way you want and put it in an array. After thatn, you set them back using 'DefaultTableModel.setColumnIdentifiers(...)'.
Hope this helps.

Categories

Resources