Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - When i add a JScrollPane to my JTable, the JTable is not visible

When i use only the JTable it works, it is visible, Only JTable but when i add the JScrollPane then it is invisible. With JScrollPane

    package Menu;
import Objects.Background;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class HighScore extends JFrame {
    public HighScore() {
        setLayout(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(250, 100, 1050, 650);
        this.setLayout(null);
        Background background = new Background();
        background.setBounds(0, 0, 1050, 650);
        add(background);
        background.setLayout(null);
        JButton back = new JButton("BACK");
        back.setBounds(800, 550, 100, 30);
        background.add(back);

        String[][] info = new String[100][2];
        String[] nevek=new String[100];
        int[] pontok = new int[100];
        int i=0;
        // read the highscores
        BufferedReader objReader = null;
        try {
            String strCurrentLine;

            objReader = new BufferedReader(new FileReader("highscores.txt"));

            while ((strCurrentLine = objReader.readLine()) != null) {
                String[] parts = strCurrentLine.split("\,");
                String nev = parts[0];
                String pont = parts[1];
                nevek[i]=nev;
                pontok[i]= Integer.parseInt(pont);
                i++;
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {
                if (objReader != null)
                    objReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }//---bubble sort for the highscores
        for (int j=0; j<i; j++)
        {
            for (int k=0; k<i; k++)
                if(pontok[j]>pontok[k]) {
                    String seged2=nevek[j];
                    int seged=pontok[j];
                    pontok[j]=pontok[k];
                    nevek[j]=nevek[k];
                    pontok[k]=seged;
                    nevek[k]=seged2;
                }
        }
        //--matrix for the JTable
        for (int j=0; j<i; j++)
        {
            info[j][0]=nevek[j];
            info[j][1]= String.valueOf(pontok[j]);
        }
        //initialize Jtable and JScrollPane
        String[] columnNames={"Name","Score"};
        JTable scores = new JTable(info,columnNames);
        scores.setBounds(325,100,325,190);
        JScrollPane jScrollPane = new JScrollPane(scores);
        background.add(jScrollPane);

        back.addActionListener(e -> {
            new MainMenu();
            dispose();
        });
        setVisible(true);
    }
}

Thats the full code of my class. I am trying to have a JTable with a JScrollPane, i tried setVisible at JTable and JScrollPane too. I think the problem might be that i add the JScrollPane to a JPanel not to a JFrame, but i want that score table on my panel. In my JPanel (background), i draw an image for the background, and that's all.

Anny ideas?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't see the table because you are using null layouts incorrectly.

Don't use null layouts!!!

Don't use setBounds(...)!!!

Swing was designed to be used with layout managers. Learn how to use layout managers.

Your basic code should be:

Background background = new Background().
background.setLayout( new BorderLayout() );

JButton button = new JButton(...);
background.add(button, BorderLayout.PAGE_START);

JTable table = new JTable(...);
background.add(new JScrollPane( table ), BorderLayout.CENTER);

add(background);

Now the button and scroll pane will be added to the background panel and the background panel added to the frame.

Read the section from the Swing tutorial on Layout Managers for more information and working examples of layout managers.

The tutorial also has a section on How to Use Tables. You can download the working demo code from there as well to practice using layout managers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...