How to get horizontal scroll in JTable

I have 2 issue that i need to ask, 1st how to get the horizontal scroll for my JTable, currently it shrink all the columns which is not readable at all. I have 20 columns to show in a Table.

2nd, how to adjust my table height width maximum to my screen size.

my current code is

Container cp = getContentPane();

            ConnectionDB con = new ConnectionDB();
            tableModel = con.getRecord(DEFAULT_QUERY);

    table = new JTable(tableModel);
    table.setPreferredScrollableViewportSize(new Dimension(900, 500));

    table.setFont(new Font("Tahoma", Font.PLAIN, 12));
    scrollPane = new JScrollPane(table);

    centerPanel.add(scrollPane, BorderLayout.CENTER);
    centerPanel.setBorder(BorderFactory.createTitledBorder("Books:"));
    cp.add("Center", centerPanel);

Kindly help me in it, Thanks in Advance.

Answers


To prevent that all columns are resized to the ScrollPane size you can disable the auto resize:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

Adding your ScrollPane to the center of the BorderLayout should set the maximum size to the screen size, because normally the JFrame can't become bigger.

To set the size of the ScrollPanes ViewPort to the screen size you can use the awt Toolkit.

table.setPreferredScrollableViewportSize(Toolkit.getDefaultToolkit().getScreenSize());

Need Your Help

How to place two labels around an image (dynamically); right and bottom?

c# css asp.net layout

I'm generating a visual list of search results, and I want to format it as such:

Java Set local file Hyperlink in existing pdf using itext

java pdf hyperlink itext

I am trying to provide a hyper link in a existing PDF, which when clicked will open the file. How can this be done?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.