How to do tabbedPane concealable?

Asked

Viewed 94 times

1

I’m having trouble trying to make a tab (tabbedPane) that is displayed according to the status of a Checkbox ("selected/unchecked"). I do not know if it is possible, since it will end up changing the screen size at runtime.

what I have done so far (Simple example):

package teste02;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class ExemploTela extends JFrame {

    public ExemploTela() {
        add(monta());
        setSize(500, 500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public JComponent monta() {
        JPanel painel = new JPanel();
        painel.setLayout(null);
        JTextField text = new JTextField();
        JLabel label = new JLabel();
        JCheckBox check = new JCheckBox("Exibe/Oculta");
        painel.add(label);
        label.setBounds(95, 90, 100, 25);
        label.setText("Exemplo:");
        painel.add(text);
        text.setBounds(155, 90, 200, 25);
        painel.add(check);
        check.setBounds(155, 150, 200, 25);
        painel.setBounds(1, 1, 500, 300);

        check.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (check.isSelected()) {
                    add(montaAba());
                } else {

                }
            }
        });
        return painel;
    }

    public JComponent montaAba() {
        JPanel aba = new JPanel();
        JTabbedPane tabbedPane = new JTabbedPane();
        JLabel label = new JLabel("Testando aba");
        tabbedPane.setPreferredSize(new Dimension(300, 100));
        add(tabbedPane, BorderLayout.BEFORE_LINE_BEGINS);
        tabbedPane.add(label);
        aba.add(tabbedPane);
        return aba;
    }

    public static void main(String[] args) {
        ExemploTela x = new ExemploTela();
    }
}

To Illustrate:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    I even found the error (several, by the way) but I didn’t answer because I don’t see much sense in what you want to do. Can you better explain the purpose of this?

  • @diegofm is that when registering, depending on the product the user will have to register "extra things" (example products with expiration date), more, in the case of a clothes registration, do not need to have this lot of field to more, then I wanted to leave hidden, It’s only gonna show up if he clicks on that checkbox. This example in the products, I thought of other utilities in other screens as well. If you can give me a help, I thank you already !

  • But why create a tabbedpane and put inside a panel then? Why not do it right on the panel or right on the tabbedpanel? I still don’t understand...

  • @diegofm because I would like to do it this way, I didn’t want to leave it visible, more if there’s no way to do it, I’ll try to think of something else..

  • 1

    I’m not understanding your idea right, it might even be a different suggestion, but for that, I need to understand what you’re trying to do. If it’s not too much to ask, could you illustrate what you intend to do? Because this code has a lot of problems, the tabbed ta overlapping everything and is being added to two places, outside others.

  • @diegofm see if this "understandable" now.

  • Now yes, I understand, because your code does something very different

Show 2 more comments

1 answer

3


According to the illustration, just toggle the visibility of the Tabbedpane panel according to the status of the checkbox, so when it has selected, the isSelected return true, displaying the component and vice versa.

The code went like this:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class ExemploTela extends JFrame {

    JComponent comp;

    public ExemploTela() {
        add(monta());
        comp = montaAba();
        comp.setVisible(false);
        add(comp, BorderLayout.SOUTH);
        setSize(500, 500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public JComponent monta() {
        JPanel painel = new JPanel();
        painel.setLayout(null);
        JTextField text = new JTextField();
        JLabel label = new JLabel();
        JCheckBox check = new JCheckBox("Exibe/Oculta");
        painel.add(label);
        label.setBounds(95, 90, 100, 25);
        label.setText("Exemplo:");
        painel.add(text);
        text.setBounds(155, 90, 200, 25);
        painel.add(check);
        check.setBounds(155, 150, 200, 25);
        painel.setBounds(1, 1, 500, 300);

        check.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                    comp.setVisible(check.isSelected());
            }
        });
        return painel;
    }

    public JComponent montaAba() {
        JPanel aba = new JPanel();
        JTabbedPane tabbedPane = new JTabbedPane();
        JLabel label = new JLabel("Testando aba");
        tabbedPane.setPreferredSize(new Dimension(300, 100));
        tabbedPane.add(label);
        aba.add(tabbedPane);
        return aba;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() ->{
            ExemploTela x = new ExemploTela();  
        });

    }
}

Which results in:

inserir a descrição da imagem aqui


I want to warn you that there are several problems with this code, but that it would be outside the scope of the question to make changes and explain them. One of them is you start the screen without being inside the Event Dispatch Thread, for swing is not Thread-Safe, and the entire GUI needs to start within this single Thread to avoid problems. In this answer shows how to start the application within this Thread.

Another problem is you position with absolute layout inside the panel built by the method monta().

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

Always prefer to use the various options of Layout Managers, in the links below, has a lot of content about:

A Visual Guide to Layout Managers

More Swing: layout managers, more components and details

Getting to know Java GUI Layout Managers

  • Thanks for your help. And I will read about what you indicated, I did not know that the display may look different depending on the monitor :( I will give a deeper search.

Browser other questions tagged

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