How to position Jtabbedpane in the lower half of the screen?

Asked

Viewed 145 times

1

I would like to have my canvas divided into at least two parts, at the top I will add some fields, and at the bottom, a JTabbedPane with a few tabs. The problem is that I don’t know how to do this, I can only make Jtabbedpane occupying the entire screen.

Follow an image to illustrate:

inserir a descrição da imagem aqui

Canvas:

package tabbedpane;

import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Tela extends JFrame {

    JTabbedPane tabbedPane = new JTabbedPane();

    public Tela() {
        setTitle("Apenas um EXEMPLO");
        tabbedPane.addTab("Página 1", painel1());
        tabbedPane.addTab("Página 2", painel2());
        add(tabbedPane);
        setResizable(false);
        setVisible(true);
        setSize(600, 500);
        setLocationRelativeTo(null);
        setMinimumSize(new Dimension(getWidth(), getHeight()));
         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {
        Tela tela  = new Tela();
    }

    public JComponent painel1() {
        JPanel painel1 = new JPanel();       
        JLabel label1;     
        label1 = new JLabel("Página 1");
        painel1.add(label1);
        return painel1; // retorna painel.
    }

    public JComponent painel2() {
        JPanel painel2 = new JPanel();       
        JLabel label2;
        label2 = new JLabel("Página 2");
        painel2.add(label2);
        return painel2;
    }
}

1 answer

0


It is possible to make only two changes:

  • add(tabbedPane, BorderLayout.SOUTH); - took advantage of the Layoutmanager pattern of JFrame, and indicated to him that position the component in the south direction inside the Frame.

  • tabbedPane.setPreferredSize(new Dimension(getWidth(), 300)); - I also set a preferred size so that the Layoutmanager know which dimension of the component.

With the amendments, it stays this way:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Tela extends JFrame {

    JTabbedPane tabbedPane = new JTabbedPane();

    public Tela() {
        setTitle("Apenas um EXEMPLO");
        tabbedPane.addTab("Página 1", painel1());
        tabbedPane.addTab("Página 2", painel2());
        tabbedPane.setPreferredSize(new Dimension(getWidth(), 300));

        /* -- exemplificacao de como adicionar o painel superior -- */
        JPanel painelSuperior = new JPanel();
        painelSuperior.add(new JButton("teste"));
        add(painelSuperior, BorderLayout.NORTH);

        add(tabbedPane, BorderLayout.SOUTH);
        setResizable(false);
        setVisible(true);
        setSize(600, 500);
        setLocationRelativeTo(null);
        setMinimumSize(new Dimension(getWidth(), getHeight()));
         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {
        Tela tela  = new Tela();
    }

    public JComponent painel1() {
        JPanel painel1 = new JPanel();       
        JLabel label1;     
        label1 = new JLabel("Página 1");
        painel1.add(label1);
        return painel1; // retorna painel.
    }

    public JComponent painel2() {
        JPanel painel2 = new JPanel();       
        JLabel label2;
        label2 = new JLabel("Página 2");
        painel2.add(label2);
        return painel2;
    }
}

Output when executing this code, already with an example top panel:

inserir a descrição da imagem aqui

  • Wow, thank you so much !

  • 1

    @Joaopedro :)

  • 1

    @Gustavosantos has no secret, just follow the reasoning of the changes I highlighted, perform this code and see the same reasoning applied to your doubt(has additional comments in the code).

  • @Gustavosantos the ideal is always to distribute components through containers, facilitates repositioning something, as in your case. There is no way to suggest anything without seeing and simulating the problem, maybe it is the case to create a question even with an example that is reproducible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.