I know there is a lot of similar question posted earlier, but none of them helps me.
I have created a JFrame
, with several JPanel
-s, here included some with several JTable
-s inside. But I cant make it fit the JFrame
. I have tried pack()
; that doesn´t work, I have tried to add JScrollPane
, which does not solve the problem either. I'm new to working with GUI so the layout options are somewhat difficult to figure out, maybe this could be the problem?
Here is the code:
public class Frame extends JFrame implements ActionListener{
private JPanel mainScreen, menu1, menu2, menu3;
private JButton defTeamsButton, userTeamsButton, menu1Button1, menu2Button1, menu1backButton, menu2backButton,menu3backButton;
private JTable table1, table2, table3;
private JScrollPane scrollPane, scrollPane2, scrollPane3;
private JScrollBar bar;
private JTextField jtfDay;
private JLabel jlblDay;
public Frame(){
setLayout(new GridLayout(2,0));
mainScreen = new JPanel();
defTeamsButton = new JButton("Default/ preset teams");
defTeamsButton.setLocation(0, 20);
defTeamsButton.setSize(250, 30);
userTeamsButton = new JButton("Add my own teams");
userTeamsButton.setLocation(0, 60);
userTeamsButton.setSize(250, 30);
menu1 = new JPanel();
menu1Button1 = new JButton("Display fixture table");
menu1Button1.setLocation(0, 20);
menu1Button1.setSize(250, 30);
menu1backButton = new JButton("Back");
menu2Button1 = new JButton("Display standings table");
menu2Button1.setLocation(0, 40);
menu2Button1.setSize(250, 30);
menu2 = new JPanel();
table1 = new JTable(new tables());
scrollPane = new JScrollPane(table1);
scrollPane.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
"Sunday 1",
TitledBorder.CENTER,
TitledBorder.TOP));
table2 = new JTable(new tables2());
table2.setSize(300, 200);
scrollPane2 = new JScrollPane(table2);
scrollPane2.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
"Wednesday 1",
TitledBorder.CENTER,
TitledBorder.TOP));
menu2backButton = new JButton("Back");
menu3 = new JPanel();
table3 = new JTable(new tables3());
scrollPane3 = new JScrollPane(table3);
menu3backButton = new JButton("Back");
mainScreen.add(defTeamsButton);
mainScreen.add(userTeamsButton);
menu1.add(menu1Button1);
menu1.add(menu2Button1);
menu1.add(menu1backButton);
menu2.add(menu2backButton);
menu2.add(scrollPane);
menu2.add(scrollPane2);
menu3.add(scrollPane3);
menu3.add(menu3backButton);
add(mainScreen);
add(mainScreen);
//register listener with buttons
defTeamsButton.addActionListener(this);
userTeamsButton.addActionListener(this);
menu1Button1.addActionListener(this);
menu2Button1.addActionListener(this);
menu1backButton.addActionListener(this);
menu2backButton.addActionListener(this);
menu3backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource() == defTeamsButton){
//define action for defTeamsButton
getContentPane().removeAll();
getContentPane().add(menu1);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == userTeamsButton)
{
//ask user to input teams, to do..
String Team1= "";
String Team2= "";
String Team3= "";
String Team4= "";
Team1 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
Team2 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
Team3 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
Team4 = JOptionPane.showInputDialog(
null, "Please enter your First Team: ");
JOptionPane.showMessageDialog (null," your teams are" + Team1 + Team2
+ Team3 + Team4);
}
else if(e.getSource() == menu1Button1){
getContentPane().removeAll();
getContentPane().add(menu2);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == menu2Button1)
{
getContentPane().removeAll();
getContentPane().add(menu3);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == menu1backButton)
{
getContentPane().removeAll();
getContentPane().add(mainScreen);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else if(e.getSource() == menu2backButton)
{
getContentPane().removeAll();
getContentPane().add(menu1);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
}
public class tables extends AbstractTableModel {
/**
*
*/
private String[] columnNames = {"Home",
"Away",};
private String[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class tables2 extends AbstractTableModel {
/**
*
*/
private String[] columnNames = {"Home",
"Away",
};
private String[][] data = {
{"Team A", "Team B"},
{"Team C", "Team D"},
{"Team E", "Team F"},
{"Team G", "Team H"},
{"Team I", "Team H"}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public class tables3 extends AbstractTableModel {
/**
*
*/
private String[] columnNames = {"Teams",
"Goals",
"GoalDifference",
"Score"
};
private Object[][] data = {
{"Kathy", new Integer(5), new Integer(5), new Integer(5)},
{"John", new Integer(5), new Integer(4), new Integer(5)},
{"Sue", new Integer(5), new Integer(3), new Integer(5)},
{"Jane", new Integer(5), new Integer(2), new Integer(5)},
{"Joe", new Integer(5), new Integer(1), new Integer(5)}
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
public static void main(String args[]){
JFrame frame = new Frame();
frame.setTitle("Choose an option");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}