How to disable component in Jframe

Asked

Viewed 149 times

0

I have two Jframes the first has the name "System" and another "Access" when completing the authentication in "Access" wanted the component called "mnUsuario" which is described below was disabled so that I can have a control over the privileges of each user in the program.

Jframe: System

public class Sistema extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JMenu mnUsuario;

public void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {

            // Look And Feel
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }

            try {
                Sistema frame = new Sistema();
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Sistema() {

    setTitle("Sistema - IBMO");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 600);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu mnArquivo = new JMenu("Arquivo");
    menuBar.add(mnArquivo);

    mnUsuario = new JMenu("Usuário");
    mnArquivo.add(mnUsuario);

    JMenuItem mntmAdicionar_1 = new JMenuItem("Adicionar");
    mntmAdicionar_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            GerenciarUsuario gUsuario = new GerenciarUsuario();
            GerenciarUsuario.main(null);
            gUsuario.setLocationRelativeTo(null);
        }
    });
    mnUsuario.add(mntmAdicionar_1);

    JMenuItem mntmModificar_1 = new JMenuItem("Modificar");
    mnUsuario.add(mntmModificar_1);

    JMenuItem mntmPesquisar_1 = new JMenuItem("Pesquisar");
    mnUsuario.add(mntmPesquisar_1);

    JMenu mnMembro = new JMenu("Membro");
    mnArquivo.add(mnMembro);

    JMenuItem mntmAdicionar = new JMenuItem("Adicionar");
    mnMembro.add(mntmAdicionar);

    JMenuItem mntmModificar = new JMenuItem("Modificar");
    mnMembro.add(mntmModificar);

    JMenuItem mntmPesquisar = new JMenuItem("Pesquisar");
    mnMembro.add(mntmPesquisar);

    JMenuItem mntmSair = new JMenuItem("Sair");
    mntmSair.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            int opcao = JOptionPane.showConfirmDialog(null, "Você realmente deseja sair do Sistema?", "Informação", JOptionPane.OK_OPTION);

            // Validar Confirmação
            if(opcao == 0) {
                System.exit(0);
            } else {
                // Continuar
            }

        }
    });
    mnArquivo.add(mntmSair);

// Menu Ajuda
    JMenu mnAjuda = new JMenu("Ajuda");
    menuBar.add(mnAjuda);

    // Ajuda -> Sobre
    JMenuItem mntmSobre = new JMenuItem("Sobre");
    mnAjuda.add(mntmSobre);

// JPanel
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}
public JMenu mnUsuario() {
    return mnUsuario;
}
}

Jframe: Log In

if(grupo.equals("suporte")) {
                            JOptionPane.showMessageDialog(null, "Você pertece ao Grupo: " + grupo, "Informação", JOptionPane.INFORMATION_MESSAGE);
                            new Sistema().main(null);
                            dispose();
                        } else if(grupo.equals("lider")) {
                            JOptionPane.showMessageDialog(null, "Você pertece ao Grupo: " + grupo, "Informação", JOptionPane.INFORMATION_MESSAGE);
                            new Sistema().main(null);
                            dispose();
                        } else if(grupo.equals("auxiliar")) {
                            JOptionPane.showMessageDialog(null, "Você pertece ao Grupo: " + grupo, "Informação", JOptionPane.INFORMATION_MESSAGE);
                            new Sistema().main(null);
                            dispose();
                        } else if(grupo.equals("supervisor")) {
                            JOptionPane.showMessageDialog(null, "Você pertece ao Grupo: " + grupo, "Informação", JOptionPane.INFORMATION_MESSAGE);
                            new Sistema().main(null);
                            dispose();

Jframe: System

    public class Sistema extends JFrame {

// Variáveis
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

// JFrame
    public void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                // Look And Feel
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();
                } catch (InstantiationException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                } catch (UnsupportedLookAndFeelException e1) {
                    e1.printStackTrace();
                }

            // Frame
                try {
                    Sistema frame = new Sistema();
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

// Iniciar
    public Sistema() {

    // Sistema
        setTitle("Sistema - IBMO");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);

    // Menubar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        // Arquivo
        JMenu mnArquivo = new JMenu("Arquivo");
        menuBar.add(mnArquivo);

        JMenu mnUsuario = new JMenu("Usuário");
        mnArquivo.add(mnUsuario);

        JMenuItem mntmAdicionar_1 = new JMenuItem("Adicionar");
        mntmAdicionar_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GerenciarUsuario gUsuario = new GerenciarUsuario();
                GerenciarUsuario.main(null);
                gUsuario.setLocationRelativeTo(null);
            }
        });
        mnUsuario.add(mntmAdicionar_1);

        JMenuItem mntmModificar_1 = new JMenuItem("Modificar");
        mnUsuario.add(mntmModificar_1);

        JMenuItem mntmPesquisar_1 = new JMenuItem("Pesquisar");
        mnUsuario.add(mntmPesquisar_1);

        // Arquivo -> Membro
        JMenu mnMembro = new JMenu("Membro");
        mnArquivo.add(mnMembro);

        // Arquivo -> Membro (Adicionar)
        JMenuItem mntmAdicionar = new JMenuItem("Adicionar");
        mnMembro.add(mntmAdicionar);

        // Arquivo -> Membro (Modificar)
        JMenuItem mntmModificar = new JMenuItem("Modificar");
        mnMembro.add(mntmModificar);

        // Arquivo -> Membro (Pesquisar)
        JMenuItem mntmPesquisar = new JMenuItem("Pesquisar");
        mnMembro.add(mntmPesquisar);

        // Arquivo -> Sair
        JMenuItem mntmSair = new JMenuItem("Sair");
        mntmSair.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                // Confirmação para Sair
                int opcao = JOptionPane.showConfirmDialog(null, "Você realmente deseja sair do Sistema?", "Informação", JOptionPane.OK_OPTION);

                // Validar Confirmação
                if(opcao == 0) {
                    System.exit(0);
                } else {
                    // Continuar
                }

            }
        });
        mnArquivo.add(mntmSair);

    // Menu Ajuda
        JMenu mnAjuda = new JMenu("Ajuda");
        menuBar.add(mnAjuda);

        // Ajuda -> Sobre
        JMenuItem mntmSobre = new JMenuItem("Sobre");
        mnAjuda.add(mntmSobre);

    // JPanel
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }
}
  • 1

    mnUsuario.setEnable(false); have tried?

  • Yes, already. mnUsuario.setVisible(false) also.

  • 1

    Using Jdialog this task may be easier, because this way, you can make jdialog return a boolean informing if the user is authenticated.

  • How can I help Jdialog?

  • 1

    Which Jframe is called first: system or access?

  • First is Access and Access calls the System.

  • 1

    Can you add your Jframe code access? If it is the main one, it makes more sense the main and all that error handling for L&F to be in Access.

  • Was added.

  • In this case, it was Jframe Acessar .

  • Excuse the ignorance. How so champion?

  • 1

    Junior, in question you say you have two jframes, one named System and the other named Access. The Sistema you had already posted, the Acessar you just posted the validation.

Show 6 more comments
No answers

Browser other questions tagged

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