How do you create a "hidden" menu?

Asked

Viewed 146 times

8

I have a Toolbar, and I want to make a settings bar on it. However, in order not to take up space, I would like it to be possible to hide/minimize it, something similar to teamviewer’s chat.

My Toolbar (this 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’ll put an illustration to help with the enthusement:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Well summarised example:

package testes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

public class TelaSistema extends JFrame implements ActionListener {

    public JDesktopPane jdp = new JDesktopPane();
    public JMenuBar jmb = new JMenuBar();
    public JMenu jmCadastros = new JMenu("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();
    }

}
  • You can add a [mcve] of your screen with the bar?

  • Hello I believe your question is the same as that: http://stackoverflow.com/questions/31601900/javafx-how-to-create-slide-in-animation-effect-for-a-pane-inside-transparent

  • @Phrxn this question is about javafx, the solution there will hardly fit for this,

1 answer

9


The closest I could do to simulate this bar was using the zero width feature JToolBar every time the button was clicked.

To better control when to contract and expand, it replaces the Jbutton by a JToggleButton, as it has 2 states(selected and not selected), simply change the width of the Toolbar depending on the state of this component.

To use, simply instantiate the class below as a JPanel, passing his JToolBar as parameter, and then add to your screen:

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

Follows the class ToolBarPanel:

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 how it works in your code:

inserir a descrição da imagem aqui

The way it is, it reduces Toolbar to width 0, but if you want a minimum size larger than zero to be displayed, just change the attribute MINIMUM_WIDTH to the desired size.

  • That’s right! Thank you very much.

Browser other questions tagged

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