How can a scrollpane of jtable show whole data vertically without scrolling - java

I have a Jtable and it enclose in a default scrollpane. When my table grow up with its row, a vertical scrollpane appear. But i don't want it. How can i show whole row vertically without vertical scrollbar.

After loading the data into the TableModel you can set the preferred size of the viewport:
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );

Related

Names of my JTable columns not showing

I created a JFrame with Java Swing using a BorderLayout. Both east and west layout are fixed with some components, while BorderLayout at the center can change depending on what the user does. At the beginning there is a picture,
panelDinamycCenter = new JPanel ();
JScrollPane = new JScrollPane (panelDinamycCenter);
panelDinamycCenter.add (new JLabel ( "", new Imagelcon ( "......."), JLabel.CENTER));
jFrame.getContentPane (). add (JScrollPane, BorderLayout.CENTER);
Then a data entry screen, for which I used a GridBagLayout. In order to remove the picture and to insert the new screen I used this:
panelDinamycCenter.removeAll (),
then I add the various components. After
setVisible (true);
I have now created a JTable, to be included in panelDinamycCenter,
panelDinamycCenter.removeAll ();
String [] [] matrixValori = new String [arrayOggCreaJTableDeroghe.length] [arrayJTableNomeColumnDeroghe.length];
for (int i = 0; i <arrayOggCreaJTableDeroghe.length; i ++) {
matrixValori [i] = arrayOggCreaJTableDeroghe [i] .creaArrayString ();
}
DefaultTableModel model = new DefaultTableModel (matrixValori, arrayJTableNomeColumnDeroghe);
JTable JTable = new JTable (model);
jTable.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
panelDinamycCenter.add (JTable);
jFrame.setVisible (true);
All right, but I can not see the names of the columns. Why?
As shown in How to Use Tables: Adding a Table to a Container, "The scroll pane automatically places the table header at the top of the viewport." At a minimum, you need to replace the JScrollPane removed by removeAll():
JTable jTable = new JTable(model);
panelDinamycCenter.add(new JScrollPane(jTable));
Among alternatives, consider these:
Add the table header to PAGE_START, as suggested here.
Instead of removing and restoring, use CardLayout to switch between views.

Scroll bar covers last row of JList with VERTICAL_WRAP

I want to layout a JList on one line horizontally, and have it scroll only horizontally. I've found list.setLayoutOrientation(JList.VERTICAL_WRAP); which works well if there are few enough items. However, when the list needs to scroll, the scrollbar covers the last (and only) row of the list, so you can't see it at all. How can I prevent this?
My test code:
JList<String> list = new JList<>("TIVFBJPAVUOHCVINPNYLMSMNNDUSHVSWUYUSNZXTYTXJMJPTISAVVYHOPBFIAXSUUQYYPVGAKEEWOTRCBWQWRXQTYJLCTTHTXPMZWDLQRRUZJSVWDMLYNRUDZXRTEJWAZUOBQCWNCYEPVCPXVWOGVZPOEKPWZZFDGZZGXPBFZQQVKFIXCYFTHRPJJMOYISEUCUTJGZQI".split("[A-D]"));
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(1);
frame.add(new JScrollPane(list), BorderLayout.NORTH);
The result:
Force the scroll pane to include the horizontal scrollbar in its size calculation:
JList<String> list = new JList<String>( ... );
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(1);
JScrollPane scrollPane = new JScrollPane( list );
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add( scrollPane );

Scene2d ScrollPane not scrolling

I create a ScrollPane, with table inside. It works fine, but when I add this ScrollPane to layout Table (or VecrticalGroup) it stops working.
layoutTable = new Table();
groupTable = new Table();
SP = new ScrollPane(groupTable, skin);
SP.setWidth(1105);
SP.setHeight(300);
groupTable.top();
groupTable.left();
layoutTable.add(inputTable);
layoutTable.row();
layoutTable.add(SP);
layoutTable.row();
layoutTable.add(resultTable);
layoutTable.setFillParent(true);
stage.addActor(layoutTable);
Once you add it to the table, the table manages its size, so your earlier calls (SP.setWidth() and SP.setHeight()) are overwritten (probably with whatever is the prefSize of the scroll pane's contents, which is zero if it's an empty table). If you want to control the scroll pane's size, do it by adjusting its cell when you add it to the table:
layoutTable.add(SP).width(1105).height(300).fill();
The fill() call tells the table to resize the scroll pane to fit the cell dimensions.

Jtable in BoxLayout

I have a BoxLayout (in a panel of a BorderLayout) in which I've put a JTable and a button vertically. I would like the table to have a fixed width and that the height should resize itself up to filling the panel. At the moment, I'm fine with the displayed width but not that it's filling up the whole panel height. For example, if there are zero data in the table, I would like only the column names to be shown. I've tried things like setViewportView() and setPreferredSize(), but can't really make it work. Any suggestions? Is it in the layout or scrollpane?
String[] columnNames = { "Date", "Type"
};
Object[][] data = {
};
JTable myTable = new JTable(data, columnNames);
JButton okButton = new JButton("OK");
JPanel panWest = new JPanel();
panWest.setLayout(new BoxLayout(panWest, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(myTable);
panWest.add(scrollPane);
panWest.add(okButton);
EDIT
This is what ended up working:
Dimension dimension = new Dimension();
dimension.height = (myTable.getRowCount()*myTable.getRowHeight())+
myTable.getTableHeader().getPreferredSize().height;
dimension.width = myTable.getColumnModel().getTotalColumnWidth();
scrollPane.setMaximumSize(dimension);
I would like the table to have a fixed width and that the height
should resize itself up to filling the panel.
and
I have a BoxLayout (in a panel of a BorderLayout) in which I've put a
JTable and a button vertically.
there are three (mentioned only simple) ways
use built_in LayoutManager for JPanel - FlowLayout (FLowLayout.CENTER),
override FLowLayout.LEFT or RIGHT in the case that you want to aling,
JComponent laid by FlowLayout never will be resizable without programatically to change XxxSize and notified by revalidate & repaint,
set proper size for JScrollPane by override JTable.setPreferredScrollableViewportSize(new Dimension(int, int));,
JScrollPane accepts this Dimension as initial size, required is usage of JFrame.pack(), before JFrame.setVisible(true)
BoxLayout accepts min, max and preferred size, override all these three sizes for JScrollPane
(half solution, but most comfortable) change LayoutManager for JPanel to BorderLayout,
put JScrollPane with JTable to BorderLayout.EAST/WEST area,
then override JTable.setPreferredScrollableViewportSize(new Dimension(int, int));,
JScrollPane will occupy whole area for BorderLayout.EAST/WEST and will be resizeable only on heights coordinates
use MigLayout, here are a few posts about MigLayout and JScrollPane (with JTextArea, JTable)

JTable in JScrollPane forces scrolling when not required

I have a JTable in a JScrollPane. If the screen shows half of a row in the JTable, when clicking on the JTable, the JScrollPane scrolls automatically to show the entire row.
How do I stop that behavior, such that a click will not scroll the JScrollPane.
Thanks!
The behavior is enforced by calling scrollRectToVisible() from the table's UI delegate, typically derived from BasicTableUI. Short of supplying your own delegate, you can use setPreferredScrollableViewportSize() to make the height of enclosing Container an intergral multiple of the rowHeight, as shown below and in this example.
private static final int N_ROWS = 8;
private JTable table = new JTable();
...
Dimension d = new Dimension(
table.getPreferredScrollableViewportSize().width,
N_ROWS * table.getRowHeight());
table.setPreferredScrollableViewportSize(d);
add(new JScrollPane(table));

Categories

Resources