How do you create a "hidden" menu?

8

I have a Toolbar, and I want to make a settings bar in it. However, in order not to occupy space, I would like to hide / minimize it, something like the teamviewer chat.

My ToolBar (it's inside a main screen)

Container contentPane = this.getContentPane();
contentPane.add(toolBar, BorderLayout.SOUTH);  
toolBar.setFloatable(false);// não deixa ToolBar mudar de lugar.
JLabel label = new JLabel("  Configurações   ");
toolBar.add(label);                  
toolBar.add(botaoConfig);   
botaoConfig.addActionListener(this);
botaoConfig.setPreferredSize(new Dimension(30, 30));

I'm going to put an illustration to help you with your understanding:

Well-summedexample:

packagetestes;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.Dimension;importstaticjava.awt.Frame.MAXIMIZED_BOTH;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JDesktopPane;importjavax.swing.JFrame;importstaticjavax.swing.JFrame.EXIT_ON_CLOSE;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JToolBar;publicclassTelaSistemaextendsJFrameimplementsActionListener{publicJDesktopPanejdp=newJDesktopPane();publicJMenuBarjmb=newJMenuBar();publicJMenujmCadastros=newJMenu("Cadastros");
    public JMenuItem jmiEstado = new JMenuItem("Estado");
    JToolBar toolBar = new JToolBar();
    JButton botaoConfig = new JButton("Config");

    public TelaSistema() {

        Container contentPane = this.getContentPane();
        contentPane.add(toolBar, BorderLayout.EAST);
        toolBar.setFloatable(false);// não deixa ToolBar mudar de lugar.
        JLabel label = new JLabel("  Configurações   ");
        toolBar.add(label);
        toolBar.add(botaoConfig);
        botaoConfig.addActionListener(this);
        botaoConfig.setPreferredSize(new Dimension(30, 30));
        toolBar.setBackground(new Color(230, 230, 230));

        // setSize(800, 600);
        setExtendedState(MAXIMIZED_BOTH);
        setTitle("Sistema");
        getContentPane().add(jdp);
        setJMenuBar(jmb);
        jmb.add(jmCadastros);
        adicionaJMenuItem(jmCadastros, jmiEstado);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void adicionaJMenuItem(JMenu menu, JMenuItem item) {
        menu.add(item);
        item.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {

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

}
    
asked by anonymous 30.10.2016 / 20:13

1 answer

9

The closest thing I could do to simulate this toolbar was to use the zero of the JToolBar width every time the button was clicked.

To better control when to collapse and when to expand, it replaces Jbutton with JToggleButton , since it has 2 states (selected and not selected), just change the width of the toolbar as the status of this component.

To use, just instantiate the class below as a JPanel , passing its JToolBar as a parameter, and then add on your screen:

    JPanel barPanel = new ToolBarPanel(toolBar);
    contentPane.add(barPanel, BorderLayout.EAST);

The class ToolBarPanel follows:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
*
* @author diego.felipe
*/

public class ToolBarPanel extends JPanel implements ItemListener {

    private final JComponent barComponent;
    private final int prefwitdh;
    private final int MINIMUM_WIDTH = 0;
    private JToggleButton botaoConfig = new JToggleButton(">>");

    public ToolBarPanel(JComponent toolBar) {
        this.barComponent = toolBar;
        this.prefwitdh = barComponent.getPreferredSize().width;
        this.setLayout(new BorderLayout());

        botaoConfig.addItemListener(this);
        botaoConfig.setPreferredSize(new Dimension(30, 30));
        botaoConfig.setMargin(new Insets(0, 0, 0, 0));
        botaoConfig.setFocusable(false);

        this.add(botaoConfig, BorderLayout.WEST);
        this.add(toolBar, BorderLayout.CENTER);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {

            barComponent.setPreferredSize(new Dimension(MINIMUM_WIDTH, 0));
            ((AbstractButton) e.getSource()).setText("<<");

        } else if (e.getStateChange() == ItemEvent.DESELECTED) {

            barComponent.setPreferredSize(new Dimension(prefwitdh, 0));
            ((AbstractButton) e.getSource()).setText(">>");

        }
        revalidate();
    }
}

See working in your code:

As it is, it reduces the toolbar to width 0, but if you want even minimized to display a minimum size greater than zero, simply change the MINIMUM_WIDTH attribute to the desired size.

    
03.11.2016 / 18:40