Bloquear JMenuBar do JFrame principal

5 respostas
wamarra

Olá pessoal,

Tenho uma tela de login que dependendo do privilégio do usuário tenho que bloquear alguns JMenuBar.
Alguém tem alguma :idea:

Vlw!

5 Respostas

T

Conforme o privilégio, você pode desabilitar JMenuItem ou JMenu (com setEnable (false)).

O melhor seria se você trabalhasse com Actions, porque aí você pode desabilitar a Action mesmo (com setEnable (false)).

Veja um exemplo disso em http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html

wamarra

thingol:
Conforme o privilégio, você pode desabilitar JMenuItem ou JMenu (com setEnable (false)).

O melhor seria se você trabalhasse com Actions, porque aí você pode desabilitar a Action mesmo (com setEnable (false)).

Veja um exemplo disso em http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html

Olá thingol,
Eu utilizo um evento de um JButton ao qual dentro desse evento tem um método que faz o que me falou, porem não dá certo, a não ser que crio outra instância da classe principal e através dela eu chamo cada item do menu.
Mas ai fica dois formularios aberto, proporcionando o usuário usar o Alt + Tab e acessar o formulario com todas as opções disponíveis.
O código é o seguinte:

private void verificarDepartamento(){ switch (comboLogDepartamento.getSelectedIndex()){ case 1: juridico.setEnabled(true); cobranca.setEnabled(false); departamentoPessoal.setEnabled(false); recepcao.setEnabled(false); cadastramento.setEnabled(false); contabilidade.setEnabled(false); break; case 2: juridico.setEnabled(false); cobranca.setEnabled(true); departamentoPessoal.setEnabled(false); recepcao.setEnabled(false); cadastramento.setEnabled(false); contabilidade.setEnabled(false); break; case 3: juridico.setEnabled(false); cobranca.setEnabled(false); departamentoPessoal.setEnabled(true); recepcao.setEnabled(false); cadastramento.setEnabled(false); contabilidade.setEnabled(false); break; case 4: juridico.setEnabled(false); cobranca.setEnabled(false); departamentoPessoal.setEnabled(false); recepcao.setEnabled(false); cadastramento.setEnabled(false); contabilidade.setEnabled(true); break; case 5: juridico.setEnabled(false); cobranca.setEnabled(false); departamentoPessoal.setEnabled(false); recepcao.setEnabled(true); cadastramento.setEnabled(false); contabilidade.setEnabled(false); break; case 6: juridico.setEnabled(true); cobranca.setEnabled(true); departamentoPessoal.setEnabled(true); recepcao.setEnabled(true); cadastramento.setEnabled(true); contabilidade.setEnabled(true); break; } }
O que funciona em termos é o seguite:

private void verificarDepartamento(){ TelaPrincipal tp = new TelaPrincipal(); tp.setVisible(true); switch (comboLogDepartamento.getSelectedIndex()){ case 1: tp.juridico.setEnabled(true); tp.cobranca.setEnabled(false); tp.departamentoPessoal.setEnabled(false); tp.recepcao.setEnabled(false); tp.cadastramento.setEnabled(false); tp.contabilidade.setEnabled(false); break; case 2: tp.juridico.setEnabled(false); tp.cobranca.setEnabled(true); tp.departamentoPessoal.setEnabled(false); tp.recepcao.setEnabled(false); tp.cadastramento.setEnabled(false); tp.contabilidade.setEnabled(false); break; case 3: tp.juridico.setEnabled(false); tp.cobranca.setEnabled(false); tp.departamentoPessoal.setEnabled(true); tp.recepcao.setEnabled(false); tp.cadastramento.setEnabled(false); tp.contabilidade.setEnabled(false); break; case 4: tp.juridico.setEnabled(false); tp.cobranca.setEnabled(false); tp.departamentoPessoal.setEnabled(false); tp.recepcao.setEnabled(false); tp.cadastramento.setEnabled(false); tp.contabilidade.setEnabled(true); break; case 5: tp.juridico.setEnabled(false); tp.cobranca.setEnabled(false); tp.departamentoPessoal.setEnabled(false); tp.recepcao.setEnabled(true); tp.cadastramento.setEnabled(false); tp.contabilidade.setEnabled(false); break; case 6: tp.juridico.setEnabled(true); tp.cobranca.setEnabled(true); tp.departamentoPessoal.setEnabled(true); tp.recepcao.setEnabled(true); tp.cadastramento.setEnabled(true); tp.contabilidade.setEnabled(true); break; } }
O que eu poderia fazer?
Vlw

T

Que estranho. Aqui tem um exemplo do que eu estava mencionando. (O exemplo é grande mas é por causa do Visual Editor do Eclipse, o “osso” mesmo é o evento “public void itemStateChanged(java.awt.event.ItemEvent e)”.

import java.awt.Rectangle;
import java.awt.event.ItemEvent;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/*
 * TesteJFrame
 *
 * Atualizações:
 * 01 - 29/11/2007 - Criação.
 */

/**
 */
public class TesteJFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel jContentPane = null;

    private JMenuBar jJMenuBar = null;

    private JMenu jMenu1 = null;

    private JMenu jMenu2 = null;

    private JMenu jMenu3 = null;

    private JMenuItem jMenuItem11 = null;

    private JMenuItem jMenuItem12 = null;

    private JMenuItem jMenuItem21 = null;

    private JMenuItem jMenuItem22 = null;

    private JMenuItem jMenuItem31 = null;

    private JComboBox jComboBox = null;

    /**
     * This method initializes jJMenuBar	
     * 	
     * @return javax.swing.JMenuBar	
     */
    private JMenuBar getJJMenuBar() {
        if (jJMenuBar == null) {
            jJMenuBar = new JMenuBar();
            jJMenuBar.add(getJMenu1());
            jJMenuBar.add(getJMenu2());
            jJMenuBar.add(getJMenu3());
        }
        return jJMenuBar;
    }

    /**
     * This method initializes jMenu1	
     * 	
     * @return javax.swing.JMenu	
     */
    private JMenu getJMenu1() {
        if (jMenu1 == null) {
            jMenu1 = new JMenu();
            jMenu1.setText("Diretor");
            jMenu1.add(getJMenuItem11());
            jMenu1.add(getJMenuItem12());
        }
        return jMenu1;
    }

    /**
     * This method initializes jMenu2	
     * 	
     * @return javax.swing.JMenu	
     */
    private JMenu getJMenu2() {
        if (jMenu2 == null) {
            jMenu2 = new JMenu();
            jMenu2.setText("Gerente");
            jMenu2.add(getJMenuItem21());
            jMenu2.add(getJMenuItem22());
        }
        return jMenu2;
    }

    /**
     * This method initializes jMenu3	
     * 	
     * @return javax.swing.JMenu	
     */
    private JMenu getJMenu3() {
        if (jMenu3 == null) {
            jMenu3 = new JMenu();
            jMenu3.setText("Operador");
            jMenu3.add(getJMenuItem31());
        }
        return jMenu3;
    }

    /**
     * This method initializes jMenuItem11	
     * 	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getJMenuItem11() {
        if (jMenuItem11 == null) {
            jMenuItem11 = new JMenuItem();
            jMenuItem11.setText("Opção 1");
        }
        return jMenuItem11;
    }

    /**
     * This method initializes jMenuItem12	
     * 	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getJMenuItem12() {
        if (jMenuItem12 == null) {
            jMenuItem12 = new JMenuItem();
            jMenuItem12.setText("Opção 2");
        }
        return jMenuItem12;
    }

    /**
     * This method initializes jMenuItem21	
     * 	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getJMenuItem21() {
        if (jMenuItem21 == null) {
            jMenuItem21 = new JMenuItem();
            jMenuItem21.setText("Opção 3");
        }
        return jMenuItem21;
    }

    /**
     * This method initializes jMenuItem22	
     * 	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getJMenuItem22() {
        if (jMenuItem22 == null) {
            jMenuItem22 = new JMenuItem();
            jMenuItem22.setText("Opção 4");
        }
        return jMenuItem22;
    }

    /**
     * This method initializes jMenuItem31	
     * 	
     * @return javax.swing.JMenuItem	
     */
    private JMenuItem getJMenuItem31() {
        if (jMenuItem31 == null) {
            jMenuItem31 = new JMenuItem();
            jMenuItem31.setText("Opção 5");
        }
        return jMenuItem31;
    }

    /**
     * This method initializes jComboBox	
     * 	
     * @return javax.swing.JComboBox	
     */
    private JComboBox getJComboBox() {
        if (jComboBox == null) {
            jComboBox = new JComboBox();
            jComboBox.setBounds(new Rectangle(18, 26, 111, 28));
            jComboBox.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) { 
                        if (e.getItem().equals ("Diretor")) {
                            jMenu1.setEnabled (true);
                            jMenu2.setEnabled (true);
                            jMenu3.setEnabled (true);
                        } else if (e.getItem().equals ("Gerente")) {
                            jMenu1.setEnabled (false);
                            jMenu2.setEnabled (true);
                            jMenu3.setEnabled (true);
                        } else if (e.getItem().equals ("Operador")) {
                            jMenu1.setEnabled (false);
                            jMenu2.setEnabled (false);
                            jMenu3.setEnabled (true);
                            
                        }
                    }
                }
            });
            jComboBox.addItem("Diretor");
            jComboBox.addItem("Gerente");
            jComboBox.addItem("Operador");
        }
        return jComboBox;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TesteJFrame thisClass = new TesteJFrame();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    /**
     * This is the default constructor
     */
    public TesteJFrame() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setJMenuBar(getJJMenuBar());
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getJComboBox(), null);
        }
        return jContentPane;
    }

}  //  @jve:decl-index=0:visual-constraint="53,33"
wamarra

Cara, consegui! :stuck_out_tongue:
Alterei o método main.
Fiz o seguinte:

public static void main(String args[]) { Splash.getInstance().openSplash(); Botoes b = new Botoes(); b.initApplication(); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { TelaPrincipal senha = new TelaPrincipal(); senha.setVisible(true); senha.solicitarSenha(); senha.setVisible(false); } }); }
Dentro do método solicitarSenha() é que tá o evento onde contém o método verificarDepartamento() e a nova instância da Classe Principal tornando-a visível novamente com as opções acessível apenas para os usuários daquele departamento.
Não sei se é o certo, mas foi a única forma que achei para não duplicar o formulário.
Valeu a força,
Abraços

wamarra

Bom, só pra corrigir o que falei hehehe…
Dentro do método solicitarSenha() é que chamo o JFrame onde tá o evento do JButton contendo o método verificarDepartamento()
T+

Criado 29 de novembro de 2007
Ultima resposta 30 de nov. de 2007
Respostas 5
Participantes 2