-1

I'm creating a program interface where the users selects something into a menu and then it will display several tabs. Inside some tabs there are choices. The problem comes when the user select something from the menu, the tab becomes visible, and the user click menu again, thats what is going on: menu bugged

The code is extense now, so I'm going to stick to the components creation.

Menu item creation:

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

JMenuItem arqAC = new JMenuItem("Aviso de Cobran\u00E7a");
menuArquivo.add(arqAC);
arqAC.addActionListener(menuItemListener);
arqAC.setActionCommand("AC");

Adding the tabPane to the contentPane:

contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

tabs = new ArquivoTabs();
JTabbedPane tabbedPane = tabs.getTabs();
tabbedPane.setBounds(0, 0, 1061, 600);
tabbedPane.setSelectedIndex(index);
contentPane.add(tabbedPane);
tabbedPane.setTitleAt(index, title);
revalidate();

ContentPane:

public ArquivoTabs() {

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    add(tabbedPane);

    ACTab act = new ACTab(tabbedPane);
    tabbedPane.addTab("AC", null, act, "Aviso de Cobran\u00E7a");
}

One tab creation:

public ACTab(JTabbedPane tp) {
    tabbedPane = tp;
    aut = Authority.getAuthority();             
    ButtonListener buttonListener = new ButtonListener();

    setLayout(null);

    tfAC = new JTextField();
    tfAC.setBounds(10, 25, 251, 20);
    add(tfAC);
    tfAC.setColumns(10);

    bpAC = new JButton("Procurar");
    bpAC.setBounds(271, 24, 75, 23);
    add(bpAC);
    bpAC.addActionListener(buttonListener);
    bpAC.setActionCommand("bpAC");

    JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
    separator.setBounds(10, 68, 336, 9);
    add(separator,
              BorderLayout.LINE_START);

    btnGerarAC = new JButton("GERAR");
    btnGerarAC.setBounds(271, 346, 75, 23);
    add(btnGerarAC);
    btnGerarAC.addActionListener(buttonListener);
    btnGerarAC.setActionCommand("btnGerarAC");


    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 113, 336, 106);
    add(scrollPane);

    tfDescDeb = new JTextArea();
    scrollPane.setViewportView(tfDescDeb);
    tfDescDeb.setFont(new Font("Arial", Font.PLAIN, 14));
    tfDescDeb.setColumns(10);
    tfDescDeb.setLineWrap(true);
    tfDescDeb.setWrapStyleWord(true);
    tfDescDeb.setEditable(false);

    motTypes = DescDeb.getMotTypes();

    choice = new Choice();
    choice.setBounds(10, 87, 336, 20);
    add(choice);
    choice.add("");
    for(DescDeb mt : motTypes){
        choice.add(mt.getTitle());
    }
    choice.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent ie)
        {
            for(DescDeb mt : motTypes){
                if(choice.getSelectedItem().equals(mt.getTitle())){
                    tfDescDeb.setEditable(false);
                    tfDescDeb.setText(mt.getText());
                }
            }
        }
    });


    JLabel lblDescDeb = new JLabel("Descri\u00E7\u00E3o dos d\u00E9bitos:");
    lblDescDeb.setBounds(10, 73, 110, 14);
    add(lblDescDeb);

    JLabel lblAutRole = new JLabel("Cargo da Autoridade:");
    lblAutRole.setBounds(10, 258, 110, 14);
    add(lblAutRole);

    txtAutRole = new JTextField();
    txtAutRole.setEditable(false);
    txtAutRole.setColumns(10);
    txtAutRole.setBounds(134, 258, 212, 20);
    txtAutRole.setText(aut.getRole());
    add(txtAutRole);

    JLabel lblAutMat = new JLabel("Matr\u00EDcula da Autoridade:");
    lblAutMat.setBounds(10, 286, 118, 14);
    add(lblAutMat);

    txtAutMat = new JTextField();
    txtAutMat.setEditable(false);
    txtAutMat.setColumns(10);
    txtAutMat.setBounds(134, 286, 212, 20);
    txtAutMat.setText(aut.getRegistration());
    add(txtAutMat);

    JLabel lblAutName = new JLabel("Nome da Autoridade:");
    lblAutName.setBounds(10, 230, 102, 14);
    add(lblAutName);

    txtAutName = new JTextField();
    txtAutName.setEditable(false);
    txtAutName.setBounds(134, 230, 212, 20);
    txtAutName.setText(aut.getName());
    add(txtAutName);

    JButton btnEditarAutoridade = new JButton("Editar Autoridade");
    btnEditarAutoridade.setBounds(226, 317, 119, 23);
    btnEditarAutoridade.addActionListener(buttonListener);
    btnEditarAutoridade.setActionCommand("edtAut");
    add(btnEditarAutoridade);

    JLabel lblArquivoDeEntrada = new JLabel("Arquivo de Entrada:");
    lblArquivoDeEntrada.setBounds(10, 11, 110, 14);
    add(lblArquivoDeEntrada);
}

I have to pass JTabbedPane tp to use it with unrelated stuff.

If more parts of the code are needed, let me know.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Hint: try coming up with a http://stackoverflow.com/help/mcve .... this is one of the occasions where I just would like to download your source, compile it and run it myself. Which is not possible when you just give snippets out of a larger content; and doing so always comes at the risk of not sharing that code that gives trouble. – GhostCat Dec 13 '16 at 13:02
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 13 '16 at 13:48
  • 1
    @GhostCat Great suggestion! Tip: `[mcve]` in a comment auto-expands to [mcve]. – Andrew Thompson Dec 13 '16 at 13:49
  • 1
    @AndrewThompson And I kept wondering how people do that, but was afraid to ask. You made my day ... plus 5 for that ;-) – GhostCat Dec 13 '16 at 13:50
  • @GhostCat *"You made my day ..."* I only offer that advice to people who I see as being useful enough to justify the time taken in doing so. Thanks for being one of them. :) – Andrew Thompson Dec 13 '16 at 13:54
  • Try to increase the menu's z-index with [setComponentZOrder](http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Container.html#setComponentZOrder%28java.awt.Component,%20int%29). – Yasin Uygun Dec 13 '16 at 13:15
  • @GhostCat I couldn't do that cus a [mcve] would not have the essence of the code, I had an especific problem for that code, in this case the mixing of Swing and AWT components as our friend noticed below) I hope you get my point. – Felipe Hogrefe Bento Dec 13 '16 at 19:33
  • @AndrewThompson Yes, I see that the "absolute Layout" might present problems for different resolutions. But this program will run into specifc machines and for them the absolute will work, but its a good point to implement in a further version, thanks for the tip. – Felipe Hogrefe Bento Dec 13 '16 at 19:33

1 Answers1

2

You are mixing Swing components (JMenu, JTabbedPane) with an AWT component (Choice). This leads to various problems including the overlap of the Choice component over the JMenu.

Use JComboBox instead of Choice

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34