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

Components do not receive componentShown event when in a JScrollPane

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_01"
      Java(TM) SE Runtime Environment (build 1.6.0_01-b04)
      Java HotSpot(TM) Server VM (build 1.6.0_01-b04, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP SP2

      A DESCRIPTION OF THE PROBLEM :
      When I call addComponentListner to a component which is in a JScrollPane, the ComponentEvents are never received. If I add the component without a JScollPane, the event is fired. A component should receive events whether or not it is in a JScrollPane.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the program detailed in "executable test case". Click on tab labeled "1" and then on tab labeled "2".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Clicking on tab "1" writes "componentShown: taOne" to System.out.
      Clicking on tab "2" writes "componentShown: taTwo" to System.out.
      ACTUAL -
      Clicking on tab "1" writes "componentShown: taOne" to System.out.
      Clicking on tab "2" writes nothing.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No errors.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.ComponentAdapter;
      import java.awt.event.ComponentEvent;
      import java.awt.event.KeyEvent;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.Box;
      import javax.swing.BoxLayout;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTabbedPane;
      import javax.swing.JTextArea;

      public class Test extends JDialog
      {
        private JButton btnOK;

        public Test(JFrame parent) {
          super(parent, "Test", true);
          init();
        }

        private void init() {
          JPanel pnlMain = new JPanel(new BorderLayout(5, 5));

          JTextArea taOne = new JTextArea(5, 20);
          JTextArea taTwo = new JTextArea(5, 20);
          JTabbedPane tabPane = new JTabbedPane();
          tabPane.addTab("0", new JLabel("1"));
          tabPane.addTab("1", taOne);
          tabPane.addTab("2", new JScrollPane(taTwo));
          pnlMain.add(tabPane, BorderLayout.CENTER);
          pnlMain.add(getButtonPanel(), BorderLayout.SOUTH);

          taOne.addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
              System.out.println("componentShown: taOne");
            }
          });

          taTwo.addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
              System.out.println("componentShown: taTwo");
            }
          });

          this.setContentPane(pnlMain);
          this.pack();
          this.setLocationRelativeTo(this.getOwner());
          rootPane.setDefaultButton(btnOK);

          this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        }

        private JPanel getButtonPanel() {
          JPanel pnlButtons = new JPanel();
          pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.X_AXIS));

          btnOK = new JButton("OK");
          btnOK.setMnemonic(KeyEvent.VK_S);
          pnlButtons.add(Box.createHorizontalGlue());
          pnlButtons.add(btnOK);
          pnlButtons.add(Box.createHorizontalGlue());

          btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              Test.this.dispose();
            }
          });

          return pnlButtons;
        }

        public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.setFocusable(false);
          frame.setResizable(false);
          frame.setUndecorated(true);
          frame.setBounds(0, 0, 0, 0);
          final Test dialog = new Test(frame);
          frame.setTitle(dialog.getTitle());
          frame.addWindowFocusListener(new WindowAdapter() {
            public void windowGainedFocus(WindowEvent ev) {
              // since frame has no size, dialog should get focus instead of frame
              dialog.toFront();
              dialog.requestFocus();
            }
          });
          frame.setVisible(true);
          dialog.setVisible(true);
          frame.setVisible(false);
          System.exit(0);
        }
      }
      ---------- END SOURCE ----------

            shickeysunw Shannon Hickey (Inactive)
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: