Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel doesnt allow positioning component

So I have created 2 JFrames

Person

Work Detail

But then I realized 2 JFrames is not a good idea.So I added JTabbedPane and then tried adding as different tabs in form of 'JPanel. But I cant position the components the way I did in Jframe and it looks like this.

Main Menu

But I want it to be like this

Expected Main Menu

How can I do this?

like image 647
2FaceMan Avatar asked Nov 23 '25 11:11

2FaceMan


2 Answers

Without a working example, it appears you've left the default layout for the JPanel (which is a FlowLayout).

Start by creating a custom panel for each of your views, it will make it easier to manage and the apply them to the JTabbedPane.

For your immeditate layout issue, I'd recommend GridBagLayout for its power and flexibility...

LayoutLayout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JTabbedPane tp = new JTabbedPane();
                tp.add("Person", new JPanel());
                tp.add("Work Detail", new TestPane());

                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField name;
        private JTextField serial;
        private JTextField id;
        private JTextField email;

        private JComboBox level;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.insets = new Insets(2, 2, 2, 2);

            add(new JLabel("Name"), gbc);
            gbc.gridx += 2;
            add(new JLabel("Sr No"), gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(new JLabel("ID"), gbc);
            gbc.gridx += 2;
            add(new JLabel("Email ID"), gbc);

            name = new JTextField(10);
            serial = new JTextField(10);
            id = new JTextField(10);
            email = new JTextField(10);

            level = new JComboBox(new String[]{"Manager", "Eng Lead", "Eng Designer", "Jr Designer"});

            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(name, gbc);
            gbc.gridx += 2;
            add(serial, gbc);
            gbc.gridx = 1;
            gbc.gridy++;
            add(id, gbc);
            gbc.gridx += 2;
            add(email, gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(level, gbc);

            gbc.gridy++;
            gbc.anchor = GridBagConstraints.CENTER;
            add(new JButton("Save"), gbc);
        }

    }

}
like image 87
MadProgrammer Avatar answered Nov 25 '25 00:11

MadProgrammer


This is one I used recently. Just use the layout you want (MigLayout was my choice here).

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

JPanel pnlOne = new JPanel();
tabbedPane.addTab("Tab One Name", null, pnlOne, null);
pnlOne.setLayout(new MigLayout("", "[644.00,grow][50px:n,grow][]","[grow][]"));

JPanel pnlTwo = new JPanel();
tabbedPane.addTab("Tab Two Name", null, pnlTwo, null);
pnlTwo.setLayout(new MigLayout("", "[grow][]", "[grow][]"));
like image 41
dly Avatar answered Nov 25 '25 00:11

dly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!