Pregunta

New to GUIs in Java. I have been looking through stackoverflow and still don't understand how JTables work. 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.

        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);
¿Fue útil?

Solución

The problem is not the table or the jscrollpane but how you put them in the panel:

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. So we want:

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

this works nicely:

enter image description here

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:

enter image description here

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. 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.

Pick a layout look at the tutorial and then use it. For example I could use a BorderLayout like this:

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

Gives:

enter image description here

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

Otros consejos

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);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top