0


I have the following scenario: a JFrame which contains several JPanels that are selected using a CardLayout. One JPanel contains a pretty large JTable which I have wrapped in a JScrollPane and added the JScrollPane to the main JPanel:

package com.javaswingcomponents.demo;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class MyPanel extends JPanel {

    private DefaultTableModel model = new DefaultTableModel();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.setVisible(true);
                    frame.setBounds(100, 100, 450, 535);
                    frame.getContentPane().add(new MyPanel());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MyPanel() {

        JTable table = new JTable(model);

        JScrollPane scrollPaneParte = new JScrollPane(table);
        scrollPaneParte.setPreferredSize(new Dimension(600, 600));

        scrollPaneParte.setBounds(10, 92, 867, 92);
        add(scrollPaneParte);
        scrollPaneParte.setViewportView(table);

        addColumns(model);


    }

    public  void addColumns(DefaultTableModel model) {
        for (int ii=0;ii<10;ii++)
        model.addColumn("Field"+ii);  
    }
}

I initially thought the problem was the XY (null) layout used, but even changing to BorderLayout nothing happens, that is, the ScrollBar is not displayed and only a part of the JTable is shown.

Any idea how to solve this issue?

EDIT: I have included a full working example which reproduces the issue.

user2824073
  • 2,407
  • 10
  • 39
  • 73
  • 2
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) *See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.)* – Andrew Thompson Oct 23 '14 at 09:43
  • Ok,, thanks. I have included the core part of the class so that it can be verified. – user2824073 Oct 23 '14 at 10:05

1 Answers1

2

This variant works reliably. Note that the default layout of a panel is flow layout. If the GUI becomes too small for the components in the flow layout, they will be truncated as in your example. I'd tend to change the layout to grid layout, which fixes the problem, in one sense. But in this example, we follow best practices for setting sizes (and locations) and enforce the minimum size.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class MyPanel extends JPanel {

    private DefaultTableModel model = new DefaultTableModel();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setLocationByPlatform(true);

                    frame.getContentPane().add(new MyPanel());

                    frame.pack();
                    frame.setMinimumSize(frame.getSize());
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MyPanel() {
        addColumns(model);
        JTable table = new JTable(model);
        JScrollPane scrollPaneParte = new JScrollPane(table);
        add(scrollPaneParte);
    }

    public  void addColumns(DefaultTableModel model) {
        for (int ii=0;ii<10;ii++)
        model.addColumn("Field"+ii);  
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for the code variant. I can see that it enforces the Frame size to a minimum size so in many cases that's a good solution. On the other hand, the ScrollBar still does not appear and that's a problem for me, as each column contains large cell data so it will endup with a very large Frame width, I think. – user2824073 Oct 23 '14 at 15:36