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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…