Java Swing JTable sort - java

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.

Related

Options menu for one cell in java table

Welcome
In this table, I made the column mentioned cells editable to let the users make the needed value as they want ...
But they told me the values of this column can take just one of the values: 25,50,75,100, so, they asked me I make an options menu (like it is mention in the pic) allow them to choose directly the needed value and working fast.
So, how can I solve it, please!
(note: I'm working with java swing)
It's not the same question, but you can take a look on solution from below question:
How do I create a right click context menu in Java Swing?
you should create such listener, and add it to your Cell / Table.
you can take a look on below question to understand how to add listener to your Cell (it's not obvious :/)
JTable cell listener?

Get JTable to print tabular format

So I have a standard JTable and I want the user to be able to print it out. I used JTable.print() and that's fine for printing out an exact replica of the table, but I was hoping to have it more in a tabular format, with just the column names and then the data beneath them, no grids or anything. I thought this would be simple, but I have no clue what to do! Has anybody done this? If so, can someone provide me with code sample/example? Thank you.
You could disable the grid lines, for example...
JTable printTable = new JTable(table.getModel());
printTable.setSize(printTable.getPreferredSize());
JTableHeader tableHeader = printTable.getTableHeader();
tableHeader.setSize(tableHeader.getPreferredSize());
printTable.setShowHorizontalLines(false);
printTable.setShowVerticalLines(false);
printTable.print(JTable.PrintMode.FIT_WIDTH);
This uses a temporary, offscreen JTable to do the actually printing, so you will need to be sure to configure any required renderers, but the idea is sound.
This basic ensures that the JTable that is on the screen doesn't get updated with the changed, which could be kind of freaking to users.
It also allows you to change the TableHeader should you want to to ;)

Set number of options shown in JComboBox when used as JTable RowFilter

I'm trying to set the number of options shown in the JComboBox drop down list when it is used as a JTable RowFilter. Specifically, the filter on occasion can have many options and I'd like to show twice as many as the default (which appears to be 8). See this image:
Combox Box Example http://aalto.tv/test/combobox-image.png
As you can hopefully see, this ComboBox only shows 8 items and I would like to show more if there are more to be seen.
Having searched around the popular solution is to call "setMaximumRowCount" on the JComboBox, however this is having no effect.
Can any one point me in the right direction?
Many thanks for any and all help!
Cheers,
Alex
try the revalidate() (or repaint()) method after setting the rowcount;
if a setXX method does not generate an event to the component, then you have to manually reset it.
failing that, look at the source code of the setMaximumRowCount() method
JComboBox#setMaximumRowCount works for JTable / TableHeader and AutoComplete JComboBox in the JTable too

JComboBox in specific JTable cell

I've a problem that is driving me crazy.
I have a classic JTable, with several columns. I need that a particular column, instead of simple texts values in its cell, it must contain a ComboBox. I searched A LOT, all I found was examples that would implement the same JComboBox in each cell of the column, that it's not what I need: I need that each cell of the column has a combo box with different values.
Can anyone give me some practical example of how to do it, please?
Thanks.
PS: I'm using NetBeans.
The TableCellEditor.getTableCellEditorComponent() method takes a row as argument. Use the existing example as a guide, and use the row argument of this method to decide which values must be proposed by the combo box.

restore jTable focus and position after removing a row from table model

one question with the java jTable class. Actually I am not a Java programmer and just now using Java to design a GUI in Matlab. What I've done is:
A jTable is built into a Matlab GUI.
I used/called a RowFilter in jTable, which can make the jTable to show the filtering results.
Then from the results in this filtered view I used the removeRow method from table model to remove one or several selected rows.
The problem is that everytime if I remove a row, the table content refreshs itself as wanted, but the scroll bar jumps back to the beginning.
Does anyone know how to inhibit this jumping and keep the original view of jTable? Because this helps me not to have to scroll back to find the original position where I started the deleting.
Thank u for ur advice and help.
Invoke the table's scrollRectToVisible() method; pass it the Rectangle returned by getCellRect() for the desired row.

Categories

Resources