JTable with horizontal scrollbar
Solution 1
First, add your JTable
inside a JScrollPane
and set the policy for the existence of scrollbars:
new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Then, indicate that your JTable must not auto-resize the columns by setting the AUTO_RESIZE_OFF
mode:
myJTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Solution 2
Set the AutoResizeMode
to OFF
in the properties of the jTable
Solution 3
For reference, here's a minimal example of the accepted approach. Moreover,
You can adjust the size of individual columns as shown in Setting and Changing Column Widths, as well as here and here.
You can adjust the overall size of the enclosing scroll pane as shown in Implementing a Scrolling-Savvy Client, as well as here and here.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
/**
* @see https://stackoverflow.com/a/37318673/230513
*/
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableModel model = new AbstractTableModel() {
private static final int N = 32;
@Override
public int getRowCount() {
return N;
}
@Override
public int getColumnCount() {
return N;
}
@Override
public Object getValueAt(int rowIndex, int colIndex) {
return "R" + rowIndex + ":C" + colIndex;
}
};
JTable table = new JTable(model) {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
f.add(new JScrollPane(table));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}
Related videos on Youtube
Mr CooL
Updated on July 09, 2022Comments
-
Mr CooL almost 2 years
Is there any way to enable horizontal scroll-bar whenever necessary?
The situation was as such: I've a
JTable
, one of the cells, stored a long length of data. Hence, I need to have horizontal scroll-bar.Anyone has idea on this?
-
Mr CooL about 14 yearsIn Netbeans, the JScrollPane with JTable intial components have been set up in a method which is cannot be modified. Any idea to solve this? I tried putting in the method, however, the table disappeared once I put above code in a button handler for testing purpose.
-
Romain Linsolas about 14 years@MrCooL could you edit your original post to show your Java code?
-
Mr CooL about 14 yearsokay, it works finally with some manipulations in Netbeans... Thanks romaintaz! ;)
-
Petr Gladkikh over 10 yearsSingle-argument constructor of
JScrollPane
sets*_SCROLLBAR_AS_NEEDED
so there's no need to specify that. -
dadan almost 7 yearssave me a lott of times
-
WSS over 6 yearsI have used both lines given in this answer in my code, but I see xyz.... . That is truncation of original line in Jtable and no horizontal scrollbar.
-
M.E. over 4 years@MrCooL could you please share how to achieve this in Netbeans?