Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8075084

JOptionPane.showMessageDialog causes JScrollBar to move

    XMLWordPrintable

    Details

    • Subcomponent:
    • Understanding:
      Cause Known
    • OS:
      generic

      Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      I have a JScrollBar, and I would like to show the user an error message if they press the 'Up' or 'Down' button. So, I use the JOptionPane.showMessageDialog function in response to a adjustmentValueChanged event, after verifying that the scrollbar value has actually changed.

      The problem is that I see a message dialog show up over and over and over. The problem seems to be that the act of showing the message dialog causes the cursor on the scrollbar to move, which generates another adjustment event, which causes the message dialog to show up again, and so forth.

      I can find other ways to report the error condition to the user, but it would be nice to understand why showing a message dialog in response to an adjustment event causes the cursor on the scrollbar to move again.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the java source code and click on the 'Down' button.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The message dialog should pop up only once whenever you press the 'Up' or 'Down' arrow button.
      ACTUAL -
      The message dialog pops up over and over whenever you press the 'Up' or 'Down' arrow button.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Run this code:

      import javax.swing.JOptionPane;

      public class BuggyDialog extends javax.swing.JDialog {

          private int _curVal;

          public BuggyDialog(java.awt.Frame parent, boolean modal) {
              super(parent, modal);

              initComponents();
              setLocationRelativeTo(null);
              _curVal = _scrollBar.getValue();
          }
                                
          private void initComponents() {
              java.awt.GridBagConstraints gridBagConstraints;

              _scrollBar = new javax.swing.JScrollBar();

              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              getContentPane().setLayout(new java.awt.GridBagLayout());

              _scrollBar.setMinimumSize(new java.awt.Dimension(17, 200));
              _scrollBar.setPreferredSize(new java.awt.Dimension(17, 200));
              _scrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
                  public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                      _scrollBarAdjustmentValueChanged(evt);
                  }
              });
              gridBagConstraints = new java.awt.GridBagConstraints();
              gridBagConstraints.gridx = 0;
              gridBagConstraints.gridy = 0;
              gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
              getContentPane().add(_scrollBar, gridBagConstraints);

              pack();
          }

          private void _scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
              if (evt.getValueIsAdjusting() || evt.getValue() == _curVal) {
                  return;
              }
              _curVal = evt.getValue();
              JOptionPane.showMessageDialog(getParent(), "Testing.");
          }

          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      BuggyDialog dialog = new BuggyDialog(new javax.swing.JFrame(), true);
                      dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                          @Override
                          public void windowClosing(java.awt.event.WindowEvent e) {
                              System.exit(0);
                          }
                      });
                      dialog.setVisible(true);
                  }
              });
          }
                         
          private javax.swing.JScrollBar _scrollBar;
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated: