简体   繁体   中英

JScrollPane inside JPanel not showing


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.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM