0

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

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    CardLayout/JTabbedPane wiht pack after JPanel switched – mKorbel Jan 05 '14 at 21:45
  • 1
    Try posting a SSCCE (www.sscce.org) – Andrew Gies Jan 05 '14 at 21:45
  • JPanel use a FlowLayout by default, try using a BorderLayout instead... – MadProgrammer Jan 05 '14 at 21:51
  • Andrew G :the problem is each of the Panels in the code, so dont know how to simplify... – user3163498 Jan 05 '14 at 21:58
  • mrKobel, can you elaborate? – user3163498 Jan 05 '14 at 21:58
  • i tried the BorderLayout : setLayout(new BorderLayout(4,0)); and adding ex: menu2.add(scrollPane, BorderLayout.LINE_START); menu2.add(scrollPane2, BorderLayout.LINE_END); but it does not make any difference. – user3163498 Jan 05 '14 at 22:19
  • `defTeamsButton.setLocation(0, 20); defTeamsButton.setSize(250, 30);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 06 '14 at 04:24
  • BTW - `printAll(getGraphics());` looks suspiciously wrong, but it depends on what `pintAll` (which is missing) does. I second the call of @AndrewG for an SSCCE. That is over 250 LOC that would not compile even when imports are added. – Andrew Thompson Jan 06 '14 at 04:28

1 Answers1

1

In your actionPerformed method add

this.pack();

at the end of the method.

This will make it all appear but you then may want to consider the use of Layout managers within your panels to adjust the appearance i.e. the layout.

Java Devil
  • 10,629
  • 7
  • 33
  • 48