Java JTable列标题未显示 - JScrollPane?

时间:2014-03-17 19:05:19

标签: java swing jtable jpanel jscrollpane

Java中的GUI新手。我一直在查看stackoverflow,但仍然不了解JTable的工作原理。我可以完全正常地获取数据,但我无法弄清楚为什么列标题没有显示,以及如何将表重新调整大小/位置到帧中的另一个位置。我一直在阅读有关使用JScrollPane的内容,我可能会错误地使用它。感谢您的帮助。

        String[] columnNames = {"Date","Field", "Home Team","Visitor Team", "Score"};
        JFrame guiFrame = new JFrame();
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Soccer Schedule");
        guiFrame.setSize(500,500);
        guiFrame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setSize(450, 450);
        JLabel titleLabel = new JLabel("Real Tired");
        String[][] data = createTableContents(scheduleCsv);
        JTable scheduleTable = new JTable(data,columnNames);
        JScrollPane scrollPane = new JScrollPane(scheduleTable);
        panel.add(scrollPane);
        panel.add(titleLabel);
        panel.add(scheduleTable);

        guiFrame.add(panel);
        guiFrame.setVisible(true);

2 个答案:

答案 0 :(得分:3)

问题不在于表格或jscrollpane,而在于如何将它们放在面板中:

panel.add(scrollPane);
panel.add(titleLabel);
panel.add(scheduleTable);

首先,scheduleTable在(视觉上)在scrollPane中,因此您只需要放置scrollPane,而不是再次放置表。所以我们想:

panel.add(titleLabel); //first put the title
panel.add(scrollPane);

这很好用:

enter image description here

当我说它运作良好时,我的意思是它很好地运作......偶然。当您使窗口变大时,会发现元素的真实布局:

enter image description here

问题在于此代码:

panel.add(titleLabel); //first put the title
panel.add(scrollPane);

这肯定会将组件(标签和滚动窗格)添加到面板中,但显而易见的问题是“面板中的位置是否添加它们?”。

然后回答是面板有一个LayoutManager来决定其组件的位置。有几个,你必须选择一个,了解它的工作原理并使用它,看看这个visual guide

由于您没有指定布局,panel正在使用面板FlowLayout的默认布局,它将组件一个接一个地并排放置。

选择教程的布局,然后使用它。例如,我可以像这样使用BorderLayout:

JPanel panel = new JPanel(new BorderLayout());
...
panel.add(titleLabel, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);

给出:

enter image description here

完整代码:

String[] columnNames = {"Date","Field", "Home Team","Visitor Team", "Score"};
JFrame guiFrame = new JFrame(); //no layout specified, frames use BorderLayout by default
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Soccer Schedule");
guiFrame.setSize(500,500);
guiFrame.setLocationRelativeTo(null);

JPanel panel = new JPanel(new BorderLayout());
panel.setSize(450, 450);
JLabel titleLabel = new JLabel("Real Tired");

//String[][] data = createTableContents(scheduleCsv);

//my test data
String[][] data = new String[1][5];
for (int i = 0; i < 5; ++i)
{
    data[0][i] = "cell " + i;
}

JTable scheduleTable = new JTable(data,columnNames);
JScrollPane scrollPane = new JScrollPane(scheduleTable);
panel.add(titleLabel, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);

guiFrame.add(panel); //this is actually  guiFrame.add(panel, BorderLayout.CENTER);
guiFrame.setVisible(true);

答案 1 :(得分:0)

You only need to add scrollpane to panel and not scheduleTable as scrollpane already  contain scheduleTable. see below code. Hope it helps.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class Test extends JFrame {

    public static void main(String args[]){
        String[] columnNames = {"Date","Field", "Home Team","Visitor Team", "Score"};
        JFrame guiFrame = new JFrame();
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Soccer Schedule");
        guiFrame.setSize(500,500);
        guiFrame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setSize(450, 450);
        JLabel titleLabel = new JLabel("Real Tired");
        String[][] data = new String[0][0];
        JTable scheduleTable = new JTable(data,columnNames);
        JScrollPane scrollPane = new JScrollPane(scheduleTable);
        panel.add(scrollPane);
        panel.add(titleLabel);

        guiFrame.add(panel);
        guiFrame.setVisible(true);
    }
}