Glitch with Jpopupmenu in Java Swing

Asked

Viewed 88 times

2

I created a small application to test a Jpopupmenu along with a Jtextfield, the problem I found is that when I click too fast, several times in a row selecting the items one time or another it gives a "Glitch"/bug on the screen, it makes the graphics components all weird. It also happens if you do it slowly, selecting some items, making appear and disappear Popup. Remembering that the problem happens randomly, not even having a special action to make it happen.

Code: (Some parts generated by Netbeans)

package blackscreendeveloper.loginteste.view;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class Login extends javax.swing.JFrame {

    public Login() {

        initComponents();

        JPopupMenu menu = new JPopupMenu();

        JMenuItem item1 = new JMenuItem("Item 1");
        JMenuItem item2 = new JMenuItem("Item 2");
        JMenuItem item3 = new JMenuItem("Item 3");

        menu.add(item1);
        menu.add(item2);
        menu.add(item3);

        txtLogin.setComponentPopupMenu(menu);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txtLogin = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField txtLogin;
    // End of variables declaration                   
}

Glitch Image: (It stayed that way in this case, but it can change) inserir a descrição da imagem aqui

  • I tested your code here and this problem did not occur in any way.

  • What would be then? Problem on the machine? Have you tested in the form I described? By clicking quickly?

  • Yes, the problem did not occur.

  • Any idea what it could be?

  • Another incoherent thing in the question is the fact that the code has the Nimbus LAF configured but its print is not with the Nimbus LAF.

  • The print posted is the way I ran it. Could LAF be the cause? Which one did you use?

  • I do not know, as it is not possible to reproduce the bug, I have no way to suggest anything because I do not know the origin. But I suspect it has nothing to do with java, otherwise the problem would occur to me too.

  • I will test on the home computer and I will return, to check if the problem occurs on another computer too.

  • It can be the ide too. Try to generate the jar and simulate to see if it occurs. If yes, it can be version of jdk with problems, or even the aero feature of your windows(by print you seem to be using windows 7)

  • I tested on Windows 10 right now. Why is ok... weird. I’m using Nimbus also on windows 10 so discarded be LAF. I will test on another PC with Windows 7 tomorrow and come back for an opinion. I thank you so far.

  • So you’ve just found that there’s no problem with java or swing API :)

  • I will test on Windows 7 again tomorrow to see the problem. But it’s probably really the PC I was on.

Show 7 more comments

1 answer

0


I tested on another PC with Windows 7 and another with Windows 10 and by my surprise nothing occurred, it worked perfectly. I suppose it could be some driver, program or something that’s causing the problem to occur. If someone has the same problem, I suggest testing on another computer, if it continues to occur, it is valid a possible driver reinstallation or computer formatting. I appreciate the help.

Browser other questions tagged

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