Horizontal Scrolling + JTable + Java

I checked stackoverflow questions regarding getting horizontal scroll on JTable to work. Setting AUTO_RESIZE_OFF makes me a horizontal scrollbar, but at the same time, the table does not use the full width of the scrollbar.

I read a little, and it turned out that since 1998 this has been working bug (will Oracle fix this?)

I saw some tips on riding techniques, etc., but no one worked for me. And does anyone have an answer? It will be much appreciated. Basically, I need a table for automatically resizing, but allowing a horizontal scrollbar before compressing any of the column names.

Thank.

+5
source share
4 answers

after talking with other answers - JXTable (in the SwingX project ) has an additional column layout property, which

  • fills the horizontal viewport (i.e. increases the width of the column) if their combined prefix is โ€‹โ€‹less than the current width, given autoResizeMode
  • saves the size of the columns in their pref and shows the horizontal scroll bar if their combined prefix is โ€‹โ€‹greater than the current width

There's a bit of internal tweaking, so (biased) would suggest using JXTable. Or look at its code and c & p - everything is allowed, everything is open source :-)

+6
source

Check out this link and try to solve this problem.

http://www.daniweb.com/software-development/java/threads/29263

JTable

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);
    }
}
+3

TABLEColumn Width, , runnable examples

+2

, , . swinglabs :

http://swinglabs.org/downloads.jsp

( ), , :

http://java.net/downloads/swingx/releases/1.6.2/

, , . .jar , JXTable (org.jdesktop.swingx.JXTable).

JXTable JTable (. : http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/JXTable.html). , JTable- .. .

JXTable table = new JXTable(yourTableModel)
table.setHorizontalScrollEnabled(true)

Swing , , JTable. . :

http://swinglabs.org/docs/components/JXTable/tutorial.jsp?step=0

, , . .

+2
source

All Articles