how to make tabbedPane concealable?

1

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

What I've 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:

    
asked by anonymous 15.04.2017 / 02:06

1 answer

3

As shown in the illustration, you just need to switch the TabbedPane panel visibility to the status of the checkbox, so when it is selected, isSelected will return true, displaying the component and vice versa.

The code looks 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();  
        });

    }
}

That results in:

Iwanttowarnyouthatyouhaveseveralissueswiththiscode,butthatitwouldescapethescopeofthequestiontomakechangesandexplainthem.OneisyoustartthescreenwithoutbeinginsidetheEventDispatchThread,as swing is not Thread- Safe , and the entire GUI needs to start within this single thread to avoid problems. In this answer shows you how to start the application within this Thread.

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

  

Avoid using absolute layout unless you are in dire need and know the consequences , because absolute layout makes it difficult to maintain the screen and make your application look different depending on the monitor and resolution being executed.

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

The Visual Guide to Layout Managers

More Swing: layout managers, more components and details

Knowing Java GUI Layout Managers

    
15.04.2017 / 18:59