First of all thanks for your support. I'm creating an application with MySQL database to store User details (Names, Age..). Just want to send the Name value to a JTable cell clicked by the user.
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
Is this possible any how?
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
It is unclear how do you get this String and what have you tried but you can use setValueAt(...) method to set this String as the selected cell's value. In order to get the selected cell row and column indexes you should use getSelectedRow() and getSelectedColumn() methods, respectively. Finally to detect user's "click" you have to attach a MouseListener to the table or a ListSelectionListener to the table's ListSelectionModel to listen for selection changes.
See the API:
JTable#setValueAt(Object value, int rowIndex, int columnIndex
JTable#getSelectedRow()
JTable#getSelectedColumn()
See also:
How to Use Tables tutorial.
Related
I want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.
I have JTable object, filled with data provided from my implementation of AbstractTableModel. I have mouse event listener and when I click on some cell I get its row and column position. With this values I call getValueAt(int row, int column) method from TableModel and I get my data. The problem is the data in the table can be sorted when I click on column name. When again I try to call getValueAt() I get the old value
Example:
col1|col2
val11|val21
val12|val22
When I click on col1
values are:
col1|col2
val12|val22
val11|val21
The I call getValueAt(0,0) and still get val11, and I should get val12. How can I fix this ? Thanks!
The row index in the view (that you probably get from your mouse listener - if you posted the code as requested, we would know instead of having to guess) is not the same as the row index in the model, once the table is sorted. To convert from the view index to the model index, use convertRowIndexToModel().
Note that the same care must be taken for columns, as the user can reorder them by drag and dropping them.
I've manage to do it. table.getValueAt() should be called direct to the table object. My previous attempt was table.getModel().getValueAt().
so I have a application in which I have a JTable filled with values related to a process list on a computer (so it has things like process name, PID, memory offset, etc). As part of this I want to collect the process name and PID when a user clicks on a row for a certain process--but how do I do this? If I call "table.getSelectedRows()" or "table.getSelectedColumns()" with a row selected I just get one element representing the clicked field's column or row index. Thanks for any help.
You can get the data for each cell in the row by calling table.getValueAt(row, column) once for each column, with 'row' being the selected row index and the 'column' being the column's zero-based index.
Note that this can be somewhat problematic, however, because the user can re-order the columns, and this method references the column in display-order.
The better way to do this is to reference the JTable's TableModel via table.getModel(), and then using the TableModel's getValueAt() method to get the columns in model order (which does not change when the columns are re-ordered in the view).
I want to the check the selected check boxes in a table cell after pressing a button and the that selected rows. (The table date will be filled by a DB - Mysql)
Please explain me how to do that with examples.
The table's TableModel should have a column keeping booleans. getColumnClass() for the column should return Boolean.class. Then checkbox will be shown by DefaultTableCellRenderer. To get value just use table.getModel().getValueAt(...)
Your checkboxes should have an ItemListener.
In the itemStateChanged() method, you invoke the isChecked() method on the checkbox object and it will return a BOOL value on its check state.
I'm developing a Java Swing application which contains a JTable. By default, while launching the application for the first time, the call to the method jtable.getSelectedRow() or jtable.getSelectedColumn() returns -1, which means that no row selected at that moment. After user clicked any row or column, the call to the method jtable.getSelectedRow() or jtable.getSelectedColumn() returns the appropriate values of selected rows & columns. What I actually need is that I want to set the selected row or column to -1 i.e. "no row or column selected state". How can I do this?
The JTable method clearSelection will do what you want -- clear the selection of all the table's selected rows and columns. The JTable API is the place to look for methods such as these.