Unique JMenu for every column in JTable - java

I'm trying to add a JMenu component for every column in my JTable. The tricky bit is that I'd like the menu to be unique for each column e.g. right clicking on a column header displays a menu which is unique for the column. Anyone has an idea of how to achieve such behavior?

add a MouseListener to the table header
use the columnAtPoint(...) method of JTableHeader to get the column that was clicked
display your menu for the specific column

The tools MouseListener, MouseEvent.isPopupTrigger() and MouseEvent.getPoint() give you all the information you need for the user actions.
Likewise JTable.getTableHeader()/getColumnModel(), JTableHeader/TableColumn give you all the information about the columns and header.
You just need to wire it up.

Related

Editing JList in a JTable

I have a JTable whose every cell contains a JList. What I want to do is to make that table editable so that every item on the JList can be edited either using a JTextfield or Choosing an item from another List whenever the user right clicks on that list item in a particular table cell. I also want 2 of my table column need to be set uneditable. Here is a picture of my JTable. I want the every cell and every JList item on that cell to be editable leaving the 'Batch' and 'Break' column to be uneditable.
P.S. I don't want any spoon fed code. I just want an idea how it can be done. And I'll be really grateful if you can reference me some link on the web where I can read to learn how this type of problems can be solved. Thank you..!
enter image description here

Editable cell and growable Jtable Implementation

I have a JTable with Customized CellRenderer and CellEditor, Intially the table is loaded with
a list of values Say with 12 rows and 5 colums, I have a JTextField at the top of the table in which I applied KeyListener and made the Textfield to display like a JComboBox with a list of values as soon as first 3 characters typed in that field, eg. Typing 'met' will display all the medicine names starting with "met", now what I want to do is I have to Implement that Textfield into the Jtable's last row's 2nd column Say 13th row in the situation I mentioned above. and after selecting any 1 medicine from the list of displayed value the JTable could Add a row dynamically and insert a new row with that search textfield, Please Suggest me an Idea and Code for this, also guide me how to apply cellrenderer and celleditor for a particular cell(Cell which contains the dynamic search textfield)...
Thanks a lot in Advance :)
Kindly let us assume jTable2 be your JTable variable name and TextField be your JTextField variable name. Then use the following code with the keylisterner of the text field to get what is wanted:-
javax.swing.table.DefaultTableModel dft= (javax.swing.table.DefaultTableModel)
jTable2.getModel();
jTable2.setModel(dft);
dft.addRow(new Object[1]);
jTable2.setValueAt(TextField.getText(),jTable2.getRowCount()-1,1);

Master detail with a JTable

I have a swing layout with some labels and a jTable.
On the left side, I have the jTable, and on the right side I have the labels.
I want to set the page up as a master / detail page, where the jTable is the master, while the labels act as details.
I have found a way to get the index from the jTable - my question. What kind of listener can I use to tell if the user selects a row in the table?
You need to get the JTable's selection model (using getSelectionModel()) and add a ListSelectionListener to this model.
Make sure to use convertRowIndexToModel to convert the index of the selected row to an index in the table model: if the table is sorted or filtered, the view and model indices won't be the same.

Drop down content in JTable

I want to show all the categories in the database in each line of a JTable i have done that. Now when i click on the each row of JTable i need to show the list of products under it (considered as child of the category) as a drop down not as a drop down list which closes automatically when focus is lost, but it should stay there until i click on minimize icon in the same row. It is just like expanding content. so i can expand all categories individually. The working is similar to JTree but i need it in JTable so that i can show more information on each row in a proper format. Any ideas ?
You need TreeTable functionality. I bet this article will help you.
You will need to have the table model add/remove the subcategory rows when the button on the category is clicked.
You should look at NetBeans Outline. Here's an example that shows how it looks.

Java Swing JTable sort

I have a JTable with column headers. When I click on a column header the data gets sorted. This is the default sort behavior.
The thing is that I need to remember the last column the user clicked to sort. Anyone knows which listener I need to implement in order to catch the column name that user clicked for sorting on the JTable?
The code is already implemented and I'm new to Swing. I just need to add that extra functionality. So any clues will be helpful.
Thanks in advance.
You can add a MouseListener to the JtableHeader. Then you just use the columnAtPoint(...) method to determine when a column is clicked.
Use Swing-X components, there is a JXTable which is more powerful than JTable.

Categories

Resources