Hi I am trying to move JTabbedPane down below top panel.I want it where the bottom panel begin.Thanks.

import java.awt.*;
    import javax.swing.*;

    public class GridBagLayoutTest

    {
        JPanel bottomPanel;
        JPanel topPanel;
        public GridBagLayoutTest()
        {
            JFrame frame = new JFrame("GridBag Layout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);

             JTabbedPane tabbed=new JTabbedPane();

            tabbed.add(bottomPanel,"One Way");
            tabbed.setBounds(12, 200, 20, 20);
            //tabbed.setTabPlacement(JTabbedPane.BOTTOM);

            Container container = frame.getContentPane();
            container.setLayout(new GridBagLayout());

            topPanel = new JPanel();

            topPanel.setBackground(Color.BLUE);
            topPanel.setPreferredSize(new Dimension(290,80));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;


            container.add(topPanel, gbc);

            bottomPanel = new JPanel();
            bottomPanel.setPreferredSize(new Dimension(120,270));
            bottomPanel.setBackground(Color.WHITE);




          bottomPanel.add(topPanel);


            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 1;

            gbc.weightx = 1.0;
            gbc.weighty = 1.0;

            gbc.anchor = GridBagConstraints.LAST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;



            container.add(bottomPanel, gbc);

            container.add(tabbed,new GridBagConstraints(0,0,1,1,1.0,1.0
        ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 4, 5, 4), -2, -1));

            frame.setSize(300, 400);
            frame.setVisible(true);
        }

        public static void main(String... args)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    new GridBagLayoutTest();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }

Recommended Answers

All 4 Replies

You seem to add it with a grid y of 1, the same as bottom panel. Use a grid y of 2 to place it below

Thanks I fix that.One more issue. I want to move bottom panel little more up.

import java.awt.*;
    import javax.swing.*;

    public class GridBagLayoutTest

    {
        JPanel bottomPanel;
        JPanel topPanel;
        public GridBagLayoutTest()
        {
            JFrame frame = new JFrame("GridBag Layout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);



            Container container = frame.getContentPane();
            container.setLayout(new GridBagLayout());

            topPanel = new JPanel();

            topPanel.setBackground(Color.BLUE);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;


            container.add(topPanel, gbc);

            bottomPanel = new JPanel();

            bottomPanel.setBackground(Color.WHITE);


            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 1;

            gbc.weightx = 1.0;
            gbc.weighty = 1.0;

            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;



            container.add(bottomPanel, gbc);
  JTabbedPane tabbed=new JTabbedPane();


            tabbed.add(bottomPanel,"One Way");

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 2;

            gbc.weightx = 1.0;
            gbc.weighty = 1.0;

            gbc.fill = GridBagConstraints.BOTH;
            container.add(tabbed,gbc);


            frame.setSize(300, 400);
            frame.setVisible(true);
        }

        public static void main(String... args)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    new GridBagLayoutTest();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }

I fix that. I just want to reduce the size of JTabbedPane. How can i do that.I mean the JTabbedPane box size.

I have not found a layout I couldn't achieve with GridBagLayout, but it's never easy. You may need to play with the weights and insets, and try setting the minimum, maximum, and/or preferred size for the tabbed pane itself. In the end it's a lot of trial and error.
You may get some good tips form Oracle's tutorial
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.