Search code examples
javajbuttonjtabbedpane

Jbutton positioning inside a tabbed pane


I am currently working on making a digital school planner and want to use multiple JButtons inside a JTabbedPane. But I am currently having a problem where even one button takes up the whole pane and would like a solution to this.

I have created a test program that mirrors how i am coding my main program:

 import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class tabTest extends JFrame {
    private static final long serialVersionUID = 5101892517858668104L;
    private JFrame frame;
    private int WIDTH = 450;
    private int HEIGHT = 600;

    private JTabbedPane tab;

    public tabTest() {

        frame = new JFrame();
        // frame.setUndecorated(true);
        /****************************************************
         * Set up frame
         *****************************************************/
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocation(50, 50);
        frame.getContentPane().setBackground(Color.GRAY);
        frame.getContentPane().setLayout(null);

        /******************************************************
         * Set up Tabbed pane and buttons
         ******************************************************/
        tab = new JTabbedPane();
        tab.setBounds(20, 50, 400, 500);
        tab.setBackground(Color.white);
        tab.setFocusable(false);

        JButton tabButton1 = new JButton("test");

        tab.addTab("Week 1", tabButton1);
        tab.addTab("Week 2", null);
        tab.addTab("Week 3", null);
        tab.addTab("Week 4", null);
        frame.getContentPane().add(tab);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        new tabTest();

    }
}

i have tried using BorderLayout.POSITION and it chucked up error after error any alternate solution would be great :)


Solution

  • Your tabbedPane can only hold one "object". If you add a button, your pane is full. However you can add a container with more room to the pane. For example a JPanel. The JPanel can now hold as many "objects" as you want and can use it's own LayoutManager.

    JPanel moreButtons = new JPanel();
    moreButtons.add(new JButton("test1"));
    moreButtons.add(new JButton("test2"));
    moreButtons.add(new JButton("test3"));
    moreButtons.add(new JButton("test4"));
    
    tab.addTab("Week 1", moreButtons); 
    

    Also you crete and initialize your whole GUI inside the constructor. It might be cleaner to define a second methode like initializeGUI() to seperate the creation of the object and the initilazation of your GUI. Also you should seperate the GUI-thread and the Main-thread, by using invokeLater

    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        Gui g = new Gui();
        g.initalizeGUI();
        Gui.setVisible(true);
      }
      });
    }
    

    There is no need to extend JFrame and to create a seperate JFrame inside your JFrame. Remove the extends JFrame or the creation of your JFrame inside the class. You can access the JFrame mehtodes from inside your extended class.

    public class MyFrame extends JFrame {
    
    public MyFrame () {
        /**
         * Set up frame 
         */
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLocation(50, 50);
        getContentPane().setBackground(Color.GRAY);
        getContentPane().setLayout(null);
    }
    }