When i try to hide the rows in a jTable, the jTable ScrollPanel need to adjust its size to remaining rows and, other components in the Frame also adjust to the change. i.e., there shouldn't be any space between the components and the jTable after hiding the table-rows..
ex: in microsoft excel, when we delete a row, the remaining rows will move upwards to fill the deleted row..
Please help me.. Thank you..
Edited:
Used a model that extends AbstractTableModel. Used RowFilter to hide the rows.
Problem is even when the rows disapper, the JScrollpane(where the JTable resides) wont get adjusted to the remaining rows..
You can use setPreferredScrollableViewportSize() to resize the scroll pane to the desired multiple of getRowHeight(). You can use validate() "to cause a Container to lay out its subcomponents again" or pack() to cause the Window "to be sized to fit the preferred size and layouts of its subcomponents."
Are you trying to hide rows or delete rows? You mention hiding rows in JTable but deleting them in Excel.
Related
It's actually an question about this. How do I resize this now? I used this, I was able to resize my JPanel, but I can't find how to resize the table or scrollpane. I have 10 columns and the table just stays the same size.
Thanks in advance
I was able to resize my JPanel, but I can't find how to resize the table or scrollpane.
The problem is that the JPanel uses a FlowLayout. A FlowLayout will respect the preferred size of any component added to the panel. So in the example code the table is displayed at its default preferred size.
A couple of solutions:
There is no need for the panel. Just add the scrollpane directly to the frame. The table will grow/shrink as the frame size changes.
Change the layout manager of the panel to one that will allow components added to it to grow to fill the available space. Again the table will grow/shrink as required.
Override the getPreferredScrollableViewportSize(...) method of the JTable. This will override the default preferred size of the table. The table may grow/shrink depending of the layout manager used.
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.
I have a JList that is inside of a JscrollPane and I am trying to do some pagination on the data, in which as the user scrolls it loads the appropriate amount of data onto the screen. The problem is I need to figure out how much data I need to buffer out of my file and the only way to do that is to figure out how much data needs to be displayed on the screen. Does anyone have an idea of how to get the number or rows and columns that can be displayed out of my JList? Also is doing pagination with a JList and a JScrollPane a good idea or is there some other approach that would be better suited for displaying a ton of data in a scroll panel?
Thanks
You can use getFirstVisibleIndex and getLastVisibleIndex methods of JList to calculate number of items it displays when visible.
If size of viewport in scroll pane changes then the number of visible rows will also change. So if the component is resizable you may need to listen to the change in the component size.
Do getModel() on the JList to get the underlying ListModel, then call getSize() on it.
EDIT: oh, and if the model contents consist of a datastructure with multiple columns, call whatever suitable method exists on that for the size. You can get a row with getElementAt(int index). Might require some more work if the column count is variable.
I am using a jtable which have 8 rows by default. When I add a new row using a button click a vertical scrollbar comes into picture and covers up my data of last column in jtable which is right aligned. How can I overcome this?
Specify setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) on the JScrollPane. This will leave room for the scroll bar when (if) it becomes necessary.
Sounds like you need to give your table a little bit more space to account for the addition of a scroll bar. You will need to possibly mess with the preferredSize.
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.