Resizing the column of jxtable automatically - java

I am using JXtable in my project.When I double click some column,it will be expanded or contracted to the highest character cell in that column. Is there any way of creating JXtable so that all its columns shall be resized to the size of its highest character cell.

I'm not sure I understand correctly, but to programmatically resize all columns according to their contents you can use JXTable.packAll();.
packAll() Javadoc:
public void packAll()
Resizes all columns to fit their content.
By default this method is bound to the pack all columns Action and registered in the table's ActionMap.
This will make the column with the "broadest" content the biggest, the column with the second "broadest" content the second biggest and so on. I am using it in my projects and it works quite well.
Resizing can also be achieved by clicking on the according menu item from the table controls.

You can use the Table Column Adjuster which will automatically adjust the width of a column any time the data is changed.

Related

How to scroll horizontally within a single column of a resizeable JTable?

I am making a dialog for the purpose of selecting multiple file paths. My dialog consists of two panels. One for buttons such as "Add" and "Remove", and a second panel containing a JTable wrapped in a scrollPane. The table has only one column. The cells of the table are not editable directly. When a user selects a file using a JFileChooser, the full path of that file will be added to the table. Although my dialog is resizeable, I still need a horizontal scroll behavior in the event that the file path is longer than the user's screen is wide.
I have researched the combination of resizeable table and horizontal scroll bar. That is similar, but not my issue. The typical scroll behavior is that the columns are scrolled, not the contents of the columns. I need the contents of a single column to scroll horizontally.
doesn't matter whether you scroll a multiple or only a single column: the basic issue is to get the horizontal scrollBar to start with :-)
There are two screws to tweak:
- enable horizontal scrolling by setting the table's resizeMode: default is to always fit the table's size to the size of the scrollPane, that is no scrolling
- resize the column width to fit its content
In a core JTable that maps into pseudo-code like
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// on receiving a TableModelEvent which might increase the column width
// calculate new width by measuring pref of the renderer
int newWidth = ...
// set it as pref of the column
table.getColumnModel().getColumn(0).setPreferredWidth(newWidth);
The catch is that without resizeMode, you are always responsible to sizing the column: it its width is less than the scrollPane, there's an empty region at its trailing side.
JXTable (part of SwingX project), supports an addition sizing mode which fills the available horizontal space as long as the table's prefWidts is less than parent width and shows a horizontal scrollBar if needed
table.setHorizontalScrollEnabled(true);
// on receiving a TableModelEvent which might increase the column width
// tell the table to re-evaluate
table.packColumn(0);
I selected kleopatra's answer as correct because it addresses my specific question regarding table manipulation. I am adding this answer because I ended up solving my root problem in a different manner.
I chose to use a JList to represent my file paths instead of a single column table. The only real reason that I had wanted to use the JTable was because of the appearance of the lined rows that a table has, and because of my unfamiliarity with JList. I discovered how to edit the appearance of the JList by extending the DefaultListCellRenderer. Because I now knew about editing the appearance, the JList's natural resizing and scroll behavior made it a much more natural fit to my needs.

JComboBox sizing issue in a table with Multi line Cells

I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable

How do I control column width of the Vaadin Table?

I have a large table to display in one of our Vaadin application. The table has 110 columns with large text header for each column.
On display the table looks awful as all the column header goes off the screen and the header separator loses the alignment with the data record column separator.
I have set the table width to 100% to display the table scrollbar. I tried to set columnExpandRatio as well as column width but no success.
Vaadin table by default sets the text header width as the column width. Is there any way i can set the header/column width to fit my data rather the header?
Finally as a solution I assigned no width value to initial columns (like first 30 columns will span naturally) and later columns with fixed width size with setColumnWidth() and it worked.
Please use table column name hide which s very useful for the viewer can choose what are the column he is able to view ...
http://demo.vaadin.com/sampler/#TableRowHeaders

Allowing users to resize columns of JTable

Im creating a table which is dynamically changing its width. And I add columns by time as well. And I have bounded this table to a scrollpane. Here I have set the auto resizing to false in the JTable (else it will fill the whole area from the beginning).
And I'm adding images to cells using cellrenderer as well. Now I need to allow users to resize this table columns.
Any idea?
I don't understand the question, by default the user is allowed to resize the columns by dragging the header of the column.
However if you want this to happen automatically then you can try using the Table Column Adjuster, which can automatically adjust the columns to fit the size of the data.
Well first of all - just change TableModel. You can leave column resizing on and for example set maximum width of cell by using setMaxWidth
Or just pick correct mode that fits your requirements. You can find them here.

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