简体   繁体   English

Java JTable列标题不显示-JScrollPane?

[英]Java JTable Column headers not showing - JScrollPane?

New to GUIs in Java. Java的GUI的新功能。 I have been looking through stackoverflow and still don't understand how JTables work. 我一直在研究stackoverflow,但仍然不了解JTables是如何工作的。 I can get data in perfectly fine but I can't figure out why the column headers are not showing up and also how to re-size/position the table to another spot in the frame. 我可以很好地获取数据,但是我无法弄清为什么没有显示列标题,以及如何将表格的大小/位置调整到框架中的另一个位置。 I keep reading about using JScrollPane which I'm probably using incorrectly.. Thanks for any help. 我一直在阅读有关使用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);

The problem is not the table or the jscrollpane but how you put them in the panel: 问题不是表或jscrollpane,而是如何将它们放在面板中:

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

First of all the scheduleTable is (visually) inside the scrollPane, so you only need to put the scrollPane, not the table again. 首先,scheduleTable(在视觉上)位于scrollPane内部,因此您只需要放置scrollPane,而不是再次放置该表。 So we want: 所以我们想要:

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

this works nicely: 这很好地工作:

在此处输入图片说明

And when I say it works nicely I mean it works nicely... by chance. 当我说效果很好时,我的意思是它效果很好...偶然。 The real layout of the elements is discovered when you make the window bigger: 当您增大窗口时,将发现元素的实际布局:

在此处输入图片说明

The problem is in this code again: 问题再次出现在此代码中:

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

This surely adds the components (label and scrollpane) to the panel, however the obvious question is "Where in the panel adds them?". 这肯定会将组件(标签和滚动窗格)添加到面板中,但是显而易见的问题是“面板中的何处添加了它们?”。

Then answer is that the panel has a LayoutManager that decides the position of its components. 然后答案是该面板具有一个LayoutManager来确定其组件的位置。 There are several of them, you have to pick one, learn how it works and use it, look at this visual guide . 其中有几种,您必须选择其中一种,了解其工作原理并使用它,然后查看此视觉指南

Because you didn't specify a layout, the panel is using the default layout for panels FlowLayout , that puts the components one after the other, side by side. 因为您未指定布局,所以panel使用的是面板FlowLayout的默认布局,该布局将组件并排放置。

Pick a layout look at the tutorial and then use it. 选择一个布局,看一下教程,然后使用它。 For example I could use a BorderLayout like this: 例如,我可以这样使用BorderLayout:

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

Gives: 给出:

在此处输入图片说明

Full code: 完整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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