I want to render different swing components in the same JTable column. For example, I want to have few different comboboxes, jlabels and jcheckboxes in the same column. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html provides information how to render only one type of combobox per column, however it is not enough.
How can I setup table cell renderer so that it would achieve this functionality?
If you need to use different editors/renderers in the same column, you can follow the approach described in this answer and override JTable#getCellEditor() based on the cell (column and row intersection). JTable#getCellRenderer() can be overriden if needed as well.
In Concepts: Editors and Renderers is described the strategy followed by tables to get a renderer/editor so you can take advantage of it to solve your problem.
Related
I'm currently using a JFrame to hold a JTabbedPane that contains multiple tables. In my class that extends JFrame and implements TableModelListener, I have an onChanged() method that takes a TableModelEvent as an argument. I can successfully obtain data from the event on the table that the event was fired from, but I can't determine which table it was.
From what I understand, this is not the way to do what I intend to do. I believe that I may need to write a custom TableModelListener or JTable and implement the onChanged() method there.
What do I need to do to determine which JTable was changed in the JTabbedPane? I'll need to find the table and the row that was modified.
TableModelListener and TableModelEvent won't provide information about the JTable that the model is associated with, as the model may be shared by multiple tables, in theory.
Getting the row is matter of getting the row from the event, which comes from the firstRow and lastRow properties. Once you can establish which table the model belongs to you, you can determine the view row by using JTable#convertRowIndexToView
To find the JTable you have, at least, two basic solutions
You Could...
Ask each table, stored in each JTabbedPane for their model and compare it with the model that generated the table model event
You Could...
Maintain some kind of look up between the TableModel and the JTable or JTabbedPane, depending on what it's you are ultimately after
This could be achieved by using Map of some kind, keyed to the TableModel
I believe that I may need to write a custom TableModelListener...
Check out the Table Cell Listener.
It is very similar to a TableModelListener, but you do need to specify the JTable when creating the TableCellListener, so you do have access to the table when a value is changed.
I want to be able to do two things:
set colors of rows based on index, so first row is red, second blue,
third green
be able to set color of columns also based on something, be it index
or their names etc, whatever is possible.
I do not need to detect selection change or anything. Could someone tell me how to do that? What methods would help etc? In case the title wasn't read, this is regarding DefaultTableModel in JTables.
set colors of rows based on index,
Table Row Rendering might give you some ideas.
be able to set color of columns also based on something
You can provide a custom render for any column. Then you can add you logic to color the column based on something. Read the JTable API and follow the link to the Swing tutorial on How to Use Table and you will find a section on creating a custom renderer.
I would like to "grey out" particular rows of a JTable so that they may not be selected by any means. The other rows should still be selectable. How do I accomplish this?
You can either override JTable.changeSelection() to deselect the offending row whenever it's selected, or provide your table with a custom ListSelectionModel where you override setSelectionInterval(), addSelectionInterval(), etc. to prevent the row from being selected in the first place.
You will want to create a custom TableCellRenderer, one that will display "disabled" information greyed out. Read the Swing Table Tutorial for more on how to create these renderers, especially the section, Concepts: Editors and Renderers.
Create a temporary TableModel which has only the rows that you want to select. After the selection made and when you want to revert, change back to original TableModel
I am trying to make a properties frame just like the one in netBeans (or Visual Studio). My problem is that I don't know exactly how to design it. First I thought I'll make it with JTable (2 columns, multiple rows) but then I realised that on the second column I will have different types of values (booleans, String, color choosers, etc.), but I think that JTable allows only 1 type of data to be placed in a column.
I would like someone to tell me "JTable allows multiple data types on the same column" and show me how to do it, or tell me a different approach to the problem.
You can perfectly tell a JTable to have a column that contains Object, this way you will be able to put whatever ou want in.
BUT.
You'll then have to implement a very good TableCellRenderer/TableCellEditor pair in order to display whatever the cell contains.
Another option would be to use a Grid or GridBag layout inside of a JScrollPane, then dynamically populate the cells of the grid with different editors depending on the data type of the property.
If you can use external libraries, the JGoodies FormLayout is really suited to create such dialogs. Just take a look at the screenshots in their demo.
There is also a rather good PDF available containing with some examples and explanations.
I am developing a software for which I need a basic Java Swing UI. I am using Netbeans which enables me to drag and drop UI component. However there is a final result table that I need to display using code.
In my UI using trhe IDE I created a JTabbedPane, inside which I added an empty JTable (no rows nor columns) called finalOutputTable. Now I want at runtime to fill this table, let's say with columns: x, y and rows: row1, row2 & row3.
How can I do that by coding?
You need to make a custom TableModel that will allow you to support this functionality. JTable is just there to display the table on the GUI. The TableModel has all the business logic of what the data is and how each cell should act. To do this you will have to step away from the GUI-Builder and write actual code.
Tutorial: Creating a Table Model
inside which i added an empty JTable (no rows nor columns) called "finalOutputTable".
You need to add the data to the TableModel. Then you use:
table.setModel(...);
to display the data. You may also need to revalidate() the panel containing the table if you didn't reserve space for the table when you created the form.
How can i do that by coding?
See the Laying Out Components Within a Container lesson of the Java Tutorial.
You can also use a TableModel witch does all that hard work for you.
https://github.com/MarkyVasconcelos/Towel/wiki/ObjectTableModel