I am trying to implement a Jtable which includes three check-box tables like this:
Can you tell me how to set a single selection group of checkboxes which only allows 1 selected check-box in a single row at any time?
I know of nothing out-of-the-box for doing this. I´d have a TableModelListener check these columns every time a change is made and call setValueAt on the checkboxes as needed.
Related
Here i have an 1-10 row is listed in jtable i want to delete/hide the 5th row before it listed in jtable.
i set the rowheight but it affected the cellselection.Is there any way to hide/delete the row without affected the normal flow code?
If i remove the row it will throws ArrayIndexoutofBoundException.
in my project executed means one gui open in that gui listed the some string. In here we can add the more string via Add Button on popup Button
Here what i need is i have to hide the particular string. That string is placed on 1st row.
i need to hide the string from end user.
now u hope understand.
You can use the JTable row filtering support in order to hide certain rows without deleting them from the model. Also see this: How can I filter rows in a JTable?
You can eliminate rows in the table by calling the removeRow() method. If you want to just hide it instead of elimintaing it you need to customize the JTable's model to meet your specs on what to display.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
using DefaultTableModel with JTable you should be able to use model.removeRow(int row) function to remove A row from JTable. There is no way to hide a row based on index as much as i know. However, If you need to hide and re-show mechanism you need to save the row prior to delete it and Save the removedRow in a ArrayList to re-use them.. Something as follows:
List<Vector>deletedRows = new ArrayList<>();
Vector removingRow = (Vector) model.getDataVector().get(5);
deletedRows.add(removingRow);
model.removeRow(5);
There are many rows on above showing jtable. I want to filter record by short code. If I type short code like 1234 it should be display only short code 1234 associated row on jtable.
Thanks
You're going to have write the code...Start by checking out how to use tables, in particular sorting and filtering
The basic requirement would be to attach an ActionListener to both the field and button (you can do this from the form editor if you wish).
Within the actionPerformed event handler method, you need to create a RowFilter and apply it to the tables RowSorter.
A table can be configured to automatically create a row sorter by setting the autoCreateRowSorter property to true.
It's all explained nicely in the linked tutorials...
And another example
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 have a jTable with columns 'Job_no' and 'Status'with values such as:
Job_no Status
1 Active
2 Pending
3 Pending
I would like it so that if a user clicks on a Status say in this case the first 'Pending'(where Job_no = 2) an inputDialog pops up allowing the user to change the status of the cell clicked-how can I do this? Bear in mind you will also have to retrieve the Job_no(that corresponds to that status) somehow, and that, though I'm OK with JOptionPane's, I'm new to JTables. I'm using JDBC(mySQL) and have a table 'Jobs' which amongst other things, has column Job_no and status.
Thanks for your help.
You don't do that by using a mouse listener and a popup, you just make the cells editable, and perhaps set a custom TableCellEditor. Take a look at the Java Tutorial for more details.
1) add a MouseListener to the JTable
2) Read the JTable API for the methods that will convert a mouse point to a row/column
3) Now that you know the row/column you can use the getValueAt(...) method to query the data in the Job_no column
4) Then you can change the status of the selected cell using the setValueAt(...) method.
So you break the problem down one step at a time.
The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?
Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.