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

[WinLaF]JComboBox trigger toString() almost 25 times on mouse over

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      IDE = Netbeans 12.5
      JDK = 17


      A DESCRIPTION OF THE PROBLEM :
      I have created a simple GUI, one JComboBox and JButton. JButton populate the JComboBox with objects of class testString. When mouse hover the JComboBox it fired toString() of testString class almost 25 time or more. Why this happening? How we can stop it?
      It should call toString() to render / refresh the String Item selected and displayed.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      ToSting() Called on Text ABC 1
      ToSting() Called on Text EDF 2
      ToSting() Called on Text ABC 3
      ToSting() Called on Text ABC 4

      * It would show "ToString() Called on Text ABC X" once on each move over
      ACTUAL -
      ToSting() Called on Text ABC 1
      ToSting() Called on Text EDF 2
      ToSting() Called on Text ABC 3
      ToSting() Called on Text ABC 4
      ToSting() Called on Text ABC 5
      ToSting() Called on Text ABC 6
      ToSting() Called on Text ABC 7
      ToSting() Called on Text ABC 8
      ToSting() Called on Text ABC 9
      ToSting() Called on Text ABC 10
      ToSting() Called on Text ABC 11
      ToSting() Called on Text ABC 12
      ToSting() Called on Text ABC 13
      ToSting() Called on Text ABC 14
      ToSting() Called on Text ABC 15
      ToSting() Called on Text ABC 16
      ToSting() Called on Text ABC 17
      ToSting() Called on Text ABC 18
      ToSting() Called on Text ABC 19
      ToSting() Called on Text ABC 20
      ToSting() Called on Text ABC 21
      ToSting() Called on Text ABC 22
      ToSting() Called on Text ABC 23
      ToSting() Called on Text ABC 24
      ToSting() Called on Text ABC 25
      ToSting() Called on Text ABC 26
      ToSting() Called on Text ABC 27

      ** Whereas it is triggering toString() almost 25 time on each move over.
      I think triggering the toString() 25 times is slowing down the software speed and lower the performance.

      ---------- BEGIN SOURCE ----------
      import javax.swing.DefaultComboBoxModel;

      public class _NewJFrame extends javax.swing.JFrame {

      public _NewJFrame() {
          initComponents();
      }

      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
          
          Object[] args = {new testString("ABC") , new testString("EDF") , "GIH", "IJK"};
          jComboBox1.setModel(new DefaultComboBoxModel(args ));
      }
      public static void main(String args[]) {
              

                  new _NewJFrame().setVisible(true);
                  
                   
      }

      private javax.swing.JButton jButton1;
      private javax.swing.JComboBox<String> jComboBox1;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JTextArea jTextArea1;
      }



      class testString {
      String text;
      static int i;
      testString(String text){ this.text = text; };


      public String toString(){
          System.out.println("ToSting() Called on Text " + text + " "+ ++i);

          return text;
      }


      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            tr Tejesh R
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: