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

Different behavior of JCheckBox when click button or push keys

XMLWordPrintable

    • generic, x86
    • generic, windows_xp



      Name: dk106046 Date: 06/07/2004

      Operating System(s) : Windows2000, Windows XP

      Full JDK version(s) (from java -version) : 131,141,142,1.5(tiger version)

      Problem details
      ---------------
      On running the test cases provided,
      Customer is developing a Swing application using a WSAD 5.1.
      In the following cases, behavior of JCheckBox in AbstractTableModel
      differs:
                                                                             
      Case 1: click a button to call ActionListener
      Case 2: push key combination to call ActionListener
                                                                             
      In the case2, the state of focus and checkbox will remain.
      Since the row of AbstractTableModel (it contains JCheckBox) is dropped
      and remade in the ActionListener, the states should be
      reset.
                 
      When a button on the window was clicked, statuses of a focus and a
      checkbox were cleared correctly. But when Alt+T (it is set as mnemonic
      of JButton) was pushed, these statuses remained. In both of the cases,
      ActionListener is called similarly, no difference found in the program
      code.

      Steps for recreation
      --------------------
        
      Working case:
      1. Compile CheckBoxModel java file
      2. Compile and Run java CheckBoxTable.java
      3. Check some check box
      4. Click "Test" button
      5. The checked check boxes were cleared normally
                                                                              
      Failing case:
      1. Run java CheckBoxTable.java
      2. Check some check box
      3. Push Alt+T key (It is mnemonic of "Test" button)
      4. The check box, which was checked last, was not cleared of status.

        
      CheckBoxModel class:

      import javax.swing.*;
      import javax.swing.table.DefaultTableModel;

      /*
       * created: 2004/04/27
       *
       */

      /**
       * @author hiroke
       *
       */
      class CheckBoxModel extends DefaultTableModel {
      public Object getValueAt(int row , int column) {
      Object ret = super.getValueAt(row , column);
      if (ret instanceof JCheckBox) {
      return new Boolean( ( (JCheckBox)ret ).isSelected() );
      }
      return ret;
      }
      public Class getColumnClass (int column) {
      if (column == 0) {
      return Boolean.class;
      }
      else {
      return super.getColumnClass(column);
      }
      }

      }



      CheckBoxTable class:
       
      import javax.swing.JFrame;

      /*
       * created: 2004/04/27
       *
       */

      /**
       * @author hiroke
       *
       */
      public class CheckBoxTable extends JFrame {

      private javax.swing.JPanel jContentPane = null;
      private javax.swing.JButton jButton = null;
      private javax.swing.JTable jTable = null;
      private javax.swing.JScrollPane jScrollPane = null;

      private CheckBoxModel cbm = null;
      Object [][] rows = {
      {new Boolean(false) , "1" },
      {new Boolean(false) , "2" },
      {new Boolean(false) , "3" }
      };

      public static void main(String[] args) {
      try{
      CheckBoxTable cbt = new CheckBoxTable();
      cbt.show();
      } catch(Throwable t){
      t.printStackTrace();
      System.exit(-1);
      }
      }
      /**
      * This is the default constructor
      */
      public CheckBoxTable() {
      super();
      initialize();
      }
      /**
      * This method initializes this
      *
      * @return void
      */
      private void initialize() {
      this.setSize(300, 200);
      this.setContentPane(getJContentPane());
      this.setVisible(true);
      this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) {
      System.out.println("windowClosing()"); // TODO Auto-generated stub windowClosing()
      System.exit(-1);
      }
      });
      }
      /**
      * This method initializes jContentPane
      *
      * @return javax.swing.JPanel
      */
      private javax.swing.JPanel getJContentPane() {
      if (jContentPane == null) {
      jContentPane = new javax.swing.JPanel();
      java.awt.GridBagConstraints consGridBagConstraints11 = new java.awt.GridBagConstraints();
      java.awt.GridBagConstraints consGridBagConstraints10 = new java.awt.GridBagConstraints();
      consGridBagConstraints11.fill = java.awt.GridBagConstraints.BOTH;
      consGridBagConstraints11.weighty = 1.0;
      consGridBagConstraints11.weightx = 1.0;
      consGridBagConstraints11.gridy = 0;
      consGridBagConstraints11.gridx = 0;
      consGridBagConstraints10.gridy = 0;
      consGridBagConstraints10.gridx = 2;
      jContentPane.setLayout(new java.awt.GridBagLayout());
      jContentPane.add(getJButton(), consGridBagConstraints10);
      jContentPane.add(getJScrollPane(), consGridBagConstraints11);
      }
      return jContentPane;
      }
      /**
      * This method initializes jButton
      *
      * @return javax.swing.JButton
      */
      private javax.swing.JButton getJButton() {
      if(jButton == null) {
      jButton = new javax.swing.JButton();
      jButton.setText("Test");
      jButton.setMnemonic(java.awt.event.KeyEvent.VK_T);
      jButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent e) {
      System.out.println("\nactionPerformed()"); // TODO Auto-generated stub actionPerformed()
      int currentRow = cbm.getRowCount();
      System.out.println("before initial :: ");
      for (int i = 0 ; i < currentRow ; i++) {
      System.out.println("<Line " + (i+1) + "> getValueAt() ==> " + cbm.getValueAt(0, 0));
      cbm.removeRow(0);
      }
      System.out.println("after initial :: ");
      for (int i = 0 ; i < rows.length ; i++) {
      cbm.addRow(rows[i]);
      System.out.println("<Line " + (i+1) + "> getValueAt() ==> " + cbm.getValueAt(i, 0));
      }
      jTable.setRowSelectionInterval(0,0);

      repaint();
      }
      });
      }
      return jButton;
      }
      /**
      * This method initializes jTable
      *
      * @return javax.swing.JTable
      */
      private javax.swing.JTable getJTable() {
      if(jTable == null) {
      initialTableModel();
      jTable = new javax.swing.JTable();
      jTable.setModel(cbm);
      }
      return jTable;
      }
      /**
      * This method initializes jScrollPane
      *
      * @return javax.swing.JScrollPane
      */
      private javax.swing.JScrollPane getJScrollPane() {
      if(jScrollPane == null) {
      jScrollPane = new javax.swing.JScrollPane();
      jScrollPane.setViewportView(getJTable());
      }
      return jScrollPane;
      }

      private void initialTableModel() {
      cbm = new CheckBoxModel();
      cbm.addColumn("this is a JCheckBox");
      cbm.addColumn("this is a String");

      for (int i = 0 ; i < rows.length ; i++) {
      cbm.addRow(rows[i]);
      }
      }
      }


      Findings
      --------
      It looks like the cell having the focus is not getting refreshed.
      Another interesting observation is that whenever we place a breakpoint at JTable::editCellAt and press Alt+T, it is working correct. But if we try without the breakpoint it is not working. So, this may be a timing issue.
      We believe the repaint method is not creating the panel properly. Even if the value of the checkbox gets false internally
      it's not shown externally.The same behaviour is observed for SUn 131,141,142 and 1.5 tiger versions also.

      ======================================================================

            Unassigned Unassigned
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: