5

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Mark
  • 479
  • 1
  • 7
  • 11

4 Answers4

6

after ranting against the other answers - JXTable (in the SwingX project) has an additional column layout property which

  • fills the horizontal viewport (that is increases the column width) if their combined pref is less than the current width, respecting the autoResizeMode
  • keeps the column sizes at their pref and shows the horizontal scrollbar if their combined pref is greater than the current width

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 :-)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • Thanks this worked. If you're interested in some guidance about acquiring the libraries etc. Please see below for a post. – Mark Sep 13 '11 at 17:31
3

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);
    }
}
Community
  • 1
  • 1
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
2

you have to setTableColumn's Width properly, more about here or here, runnable examples here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

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.

Mark
  • 479
  • 1
  • 7
  • 11
  • thanks for taking all this trouble - SwingX is still hurt by the migration to the new java.net plus a brittle infrastructure on a (Oracle!) server which hosts the CI and swinglabs.org – kleopatra Sep 14 '11 at 07:15
  • Lol, its no problem. I'm really happy to help. Way too often I go to forums etc and people are really vague as to exactly how they solved the problem. I try to break that cycle by being as detailed and as complete as possible. But I understand some problems take much more than a paragraph or two to solve and that may deter someone from writing a proper description :) – Mark Sep 16 '11 at 14:25