I checked the questions on stackoverflow concerning getting horizontal scrolling on a JTable to work. Setting AUTO_RESIZE_OFF does get me the horizontal scroll bar but at the same time the table does not utilize the full width of the scroll pane.
I did some reading and it turns out this has been a running bug since 1998 (Will Oracle fix this?)
I saw some suggestions about over riding methods etc but none worked for me. So does anyone have an answer? It would be much appreciated. Basically I need the table to auto resize but enable the horizontal scroll bar before any of the column names get compressed.
Thanks.
after ranting against the other answers - JXTable (in the SwingX project ) has an additional column layout property which
There's a bit of internal tweaking needed, so (biased me) would suggest to use the JXTable. Or have a look at its code and c&p - all allowed, all open source :-)
Check this link and try to solve this problem
http://www.daniweb.com/software-development/java/threads/29263
JTable with horizontal scrollbar
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class ScrollableJTable {
public static void main(String[] args) {
new ScrollableJTable();
}
public ScrollableJTable() {
JFrame frame = new JFrame("Creating a Scrollable JTable!");
JPanel panel = new JPanel();
String data[][] = {
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"},
{"001", "vinod", "Bihar", "India", "Biology", "65", "First"},
{"002", "Raju", "ABC", "Kanada", "Geography", "58", "second"},
{"003", "Aman", "Delhi", "India", "computer", "98", "Dictontion"},
{"004", "Ranjan", "Bangloor", "India", "chemestry", "90", "Dictontion"}
};
String col[] = {"Roll", "Name", "State", "country", "Math", "Marks", "Grade"};
JTable table = new JTable(data, col);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(panel);
frame.pack();
// frame.setSize(500, 200);
// frame.setUndecorated(true);
// frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Well here are some links, I hope they don't break. The swinglabs download page is here:
http://swinglabs.org/downloads.jsp
but at the time of me visiting the site these downloads were broken (this may be fixed by now), after a little looking around I found bare bones access on their server here:
http://java.net/downloads/swingx/releases/1.6.2/
And that's pretty much all the trouble I had, just locating the libraries. It was only a matter of extracting and adding the .jar files to my project and then importing the JXTable (org.jdesktop.swingx.JXTable).
The JXTable extends the regular JTable (see documentation: http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/JXTable.html ) So all of the regular JTable methods etc are still available.
After importing the library it was simply a matter of telling the table to horizontally scroll
JXTable table = new JXTable(yourTableModel)
table.setHorizontalScrollEnabled(true)
Swing labs provides a lovely tutorial explaining all the enhancements they have made on the JTable. See the tutorial here:
http://swinglabs.org/docs/components/JXTable/tutorial.jsp?step=0
Hope this helps, really helped me. Thanks to Kleopatra for pointing out the right path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.