I'm very new at using JPanels/Tabs and so have run into a bunch of issues with my code. I've created a table from SQL that I want to be able to be viewed inside of a tab. So I have a class first of all that includes a login and then takes you to a window with three tabs: public class UserInterface { UserInterface() {
JFrame frame = new JFrame(); //create a frame
frame.setTitle("Bet Worthiness"); //sets title of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //system exit when user closes window
frame.setResizable(false); //prevents user from resizing frame
JTextField text = new JTextField(10); //creates a text box
frame.getContentPane().setBackground(new Color(255, 243, 181)); //background colour
//Create panels
JPanel accountTab = new JPanel();
JPanel bettingTab = new JPanel();
JPanel leaderboardTab = new JPanel();
JTabbedPane tabs = new JTabbedPane(); //Create the tab container
tabs.setBounds(40, 20, 1400, 700); //Set tab container position
//Account Details label
JLabel accountLabel = new JLabel();
accountLabel.setText("Account Details");
accountLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
accountTab.add(accountLabel);
//Betting label
JLabel bettingLabel = new JLabel();
bettingLabel.setText("Place Bets");
bettingLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
bettingTab.add(bettingLabel);
//Leaderboard label
JLabel leaderboardLabel = new JLabel();
leaderboardLabel.setText("Leaderboard");
leaderboardLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
leaderboardTab.add(leaderboardLabel);
//Associate each panel with the corresponding tab
tabs.add("Account", accountTab);
tabs.add("Betting", bettingTab);
tabs.add("Leaderboard", leaderboardTab);
frame.add(tabs); //Add tabs to the frame
frame.setSize(1500, 800);
frame.setLayout(null);
frame.setVisible(true);
}
}
I have then created a table on SQL in a separate class that I want to be embedded in the 'leaderboard' tab:
public class Leaderboard extends JFrame {
public void displayLeaderboard() {
try
{
String url = "jdbc:mysql://localhost:3306/bettingappdb";
String user = "root";
String password = "lucyb";
Connection con = DriverManager.getConnection(url, user, password);
String query = "SELECT * FROM users";
Statement stm = con.createStatement();
ResultSet res = stm.executeQuery(query);
String columns[] = { "Username", "Success Rate" };
String data[][] = new String[20][3];
int i = 0;
while (res.next()) {
String nom = res.getString("Username");
int successRate = res.getInt("SuccessRate");
data[i][0] = nom;
data[i][1] = successRate + "";
i++;
}
DefaultTableModel model = new DefaultTableModel(data, columns);
JTable table = new JTable(model);
table.setShowGrid(true);
table.setShowVerticalLines(true);
JScrollPane pane = new JScrollPane(table);
JFrame f = new JFrame("Leaderboard");
JPanel panel = new JPanel();
panel.add(pane);
f.add(panel);
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
} catch(SQLException e) {
e.printStackTrace();
}
}
}
Is there any way to put this table within the tab? Thank you :)