I have a custom table with dynamic width and height for each column and row. I also planning to use JTextField as TableCellRenderer.
Should I set the table row height and column width inside the render or inside the custom table?Or there is another place that I can place this?
You can use JTable#setRowHeight(int, int) to set the height of individual rows and you will need to use the ColumnModel and obtain a reference to the TableColumn in order to change it's size.
Remember though, the size of a column may be affected by the autoResizeMode
Related
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.
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.
Is it possible to set width of a JTable column within its cell renderer? I have tried following code but it seems to do nothing.
//in main class where table defined;
table.getColumnModel().getColumn(0).setCellRenderer(renderer);
//in renderer class;
setSize(10, getPreferredSize().height);
1) its possible to set JTable Column's width this(very simple) way
TableColumnModel tcm = myTable.getColumnModel();
tcm.getColumn(0).setPreferredWidth(int);
2) I suggesting don't do that inside TableRenderer,
because I don't see reason for repainting that every time when is Renderer fired
and from your code that you posted here not really clear for me what / how / where / why
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
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.