Display values of a selected row from a dynamically varying JTable - java

I have to develop a front end in Swing.I have a JFrame in Netbeans with 3 panes in it.
JScrollPane 1:it contains a JTable
JScrollPane 2: It should display the value of the fields of the selected row
ListPane : which contains the list of tables from which a user chooses a table to be displayed.
Now since the content of JTable varies(the no of rows and columns also vary) dynamically based on the table chosen by user I can't drag and drop the TextBoxes in the 3rd scroll pane to display the selected row's values.It would be helpful if anybody can suggest a way to do it or any pointers to problems that deal with similar issue

Add a ListSelectionListener to your JList. When a particular table is selected from the list, use setModel() to change the TableModel of your JTable to one that is correct for the chosen table. A related example using setModel() is shown here.

Related

CheckBox Selection in JTable

I want, in a JTable, has a column that when it is clicked, a JPanel to appear with the names of all the columns, and it can select (with JCheckBox) which we want to continue in the JTable. It would be a column with "#".
Just create such a column in your data model implementation, with a custom artificial data object. Then register an editor (setDefaultEditor) in the jtable to show a checkbox or open a dialog with a checkbox. based on the user selection you may then alter your model (add or remove columns) and fire an according changed event.
Not exactly what you asked for but maybe you can use the Table Column Manager.
The Table Column Manager manages which columns are visible in the table. You invoke the Table Column Manager by right clicking on any column in the column header. You are presented with a popup menu that uses check boxes to hide/show columns.

Changing Content of JTable Cells Individually

So Suppose I create a JTable with a specific number of columns and rows. Is it possible to add a JPanel to a specific cell after the JTable is already initialized? The JTable would be empty at first and then I would change a cell at row X and column Y to instead contain a JPanel which would contain an amount of ImageIcons. I don't have any code to put here really, I just need to know what I would have to use to accomplish this and how. Thank you.
To change the data in the table you use:
table.setValueAt(...);
This will update the data in the TableModel.
However, adding a JPanel to the table is not the way a table is designed to be used. Typically you add data to the model. Then you use a renderer to display the data in a cell.
Start by reading the section from the Swing tutorial on Using Custom Renderers for more information and examples.

how to drop Jlist item on JTable 's Particular row & column in java?

I have a list of items in JList what I want is to drag one item at a time from JList and dropped on particular column the item should be added only that column.
AFAIK is possible to use DND in JTable only for Rows not Columns, because JTable is vertically designed
example about DND support for JTable

Clearing a JScrollPane but can't then 'attach' tables

I am creating an application where I have 3 Panes with buttons which dynamically create buttons in the next pane depending on the selection, clicking the last button shows a table of data brought up via an SQL query:
[buttonPane1][buttonPane2][buttonPane3][table]
If a user has clicked a button on all 3 panes and then wants to change their choice on buttonpanel1, it will bring up the choices in buttonpanel2 and using
buttonPanel3.removeAll();
buttonPanel3.repaint();
I can clear the third button panel, my problem is how to clear the table. I want to remove it from the Table ScrollPanel however if I try
tableScrollPanel.removeAll();
it just means that the table never shows.
How can I remove any current table but allow a table to be 'reattached' I am doing this to create and 'attach' the table
jTableTemp.setModel(new DefaultTableModel(
tableContent, tableTitles));
tableScrollPanel.setViewportView(jTableTemp);
Thanks Very Much
Try setting the table model to a DefaultTableModel with empty data and the original headers, then repaint. As long as you have a JScollPane wrapped around the JTable, the headers should show up, assuming this is what you want.
Other wise, you can set the viewport to a new instance of a JTable with new headers.

Add text and icon (with MouseListener) to JTable column

I want to implement following functionality but I am confused if it's possible in Java. If yes, than how? Please help:
I want to create a JTable kind of table where 1st row of table contains column names and an icon in each column i.e. in each cell of 1st row. Clicking on that icon should lead to removal of that column from table (possible using MouseListener??).
I have found many solution where I can add button to a cell in JTable but none which describes adding both text and icon (with MouseListener) to a cell. Please see if you can help and thanks a lot for reading.
You can create a custom TableCellRenderer that extends JLabel. This JLabel can be created with an icon (JLabel can display icons, to the right or left of the text). You will want the getTableCellRendererComponent to test wether the row being rendered is the first or not, and if so, set the icon, otherwise do not.
For the removal action, you can add a MouseListener on the table, and when processing the mouseClicked method, you can find the cell that was clicked in by testing the rowAtPoint and columnAtPoint by creating a Point from the mouseEvent.getX() and mouseEvent.getY(). If you determine the first row with the icon was clicked, you can remove the column from the column model.
If by 1st row, you actually mean the table header, you can create the same renderer for the JTableHeader, and set the MouseListener on that component.
Well, I don't understand your question.
I want to create a JTable kind of
table where 1st row of table contains
column names and an icon
Do you mean the Table Header, like the way sorting works by displaying the column name and the sort direction?
If so then you use a custom renderer for the table header and add a MouseListener to the header to determine which column was clicked. You should be able to customize the Default Table Header Renderer to do what you want.
Or do you mean the first row of data in the table. If so then you still need to use a custom renderer but this time you add the MouseListener to the table not the table header.
In both cases you can use the TableColumnModel.removeColumn() method to remove the column from the view of the table.

Categories