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

JTextArea scrolls to end of text when displayed via JButton/JMenuItem

    XMLWordPrintable

Details

    Description



      Name: skT45625 Date: 06/07/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


      Scenario:
      We have some text which is too large to be displayed in a JTextArea. The
      JTextArea is within a JScrollPane which is within a JDialog. If the JDialog is
      displayed without user interaction the start of the text is correctly shown in
      the JTextArea. If the same JDialog is displayed by clicking a JButton or
      selecting a JMenuItem the end of the text is displayed instead.

      The example code below demonstrates this:
      1. Run ScrollBugDemo - a frame appears with a JDialog below it.
      2. Select 'Dialog' from the 'Show' menu - another JDialog appears.
      3. Click the 'Show Dialog' button - a third JDialog appears.
      4. Compare the contents of the three dialogs - you should get the following
      results:

      Using
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-001, native threads, symcjit)
      All JDialogs are identical - the start of the text is displayed.

      Using
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
      The 'Dialog displayed via UI init code' correctly displays the start of the text
      but the 'Dialog displayed via JMenuItem' and 'Dialog displayed via JButton' have
      scrolled to the bottom and erroneously display the end of the text.

      Example code: ScrollBugDemo.java

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public class ScrollBugDemo
      {
        public ScrollBugDemo()
        {
          ScrollBugFrame frame = new ScrollBugFrame();
          frame.setVisible(true);
        }

        public static void main(String[] args)
        {
          new ScrollBugDemo();
        }
      }

      class ScrollBugFrame extends JFrame
      {
        BorderLayout borderLayout1 = new BorderLayout();
        JMenuBar jMenuBar1 = new JMenuBar();
        JMenu jMenu1 = new JMenu();
        JMenuItem jMenuItem1 = new JMenuItem();
        JButton jButton1 = new JButton();

        public ScrollBugFrame()
        {
          enableEvents(AWTEvent.WINDOW_EVENT_MASK);
          try
          {
            jbInit();
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
        }

        private void jbInit() throws Exception
        {
          this.getContentPane().setLayout(borderLayout1);
          this.setSize(new Dimension(200, 100));
          this.setTitle("Scroll Bug Demo");
          jMenu1.setText("Show");
          jMenuItem1.setText("Dialog");
          jMenuItem1.addActionListener(new java.awt.event.ActionListener()
          {
            public void actionPerformed(ActionEvent e)
            {
              jMenuItem1_actionPerformed(e);
            }
          });
          jButton1.setText("Show Dialog");
          jButton1.addActionListener(new java.awt.event.ActionListener()
          {

            public void actionPerformed(ActionEvent e)
            {
              jButton1_actionPerformed(e);
            }
          });
          jMenuBar1.add(jMenu1);
          jMenu1.add(jMenuItem1);
          this.getContentPane().add(jButton1, BorderLayout.CENTER);
          this.getContentPane().add(jMenuBar1, BorderLayout.NORTH);
          displayDialog("Dialog displayed via UI init code");
        }

        protected void processWindowEvent(WindowEvent e)
        {
          super.processWindowEvent(e);
          if(e.getID() == WindowEvent.WINDOW_CLOSING)
          {
            System.exit(0);
          }
        }

        private void displayDialog(String title)
        {
          String message =
          "1. Here's a load of text that doesn't fit into the JTextArea\n" +
          "2. Here's a load of text that doesn't fit into the JTextArea\n" +
          "3. Here's a load of text that doesn't fit into the JTextArea\n" +
          "4. Here's a load of text that doesn't fit into the JTextArea\n" +
          "5. Here's a load of text that doesn't fit into the JTextArea\n" +
          "6. Here's a load of text that doesn't fit into the JTextArea\n" +
          "7. Here's a load of text that doesn't fit into the JTextArea\n" +
          "8. Here's a load of text that doesn't fit into the JTextArea\n" +
          "9. Here's a load of text that doesn't fit into the JTextArea\n" +
          "10. Here's a load of text that doesn't fit into the JTextArea\n" +
          "11. Here's a load of text that doesn't fit into the JTextArea\n" +
          "12. Here's a load of text that doesn't fit into the JTextArea\n" +
          "13. Here's a load of text that doesn't fit into the JTextArea\n" +
          "14. Here's a load of text that doesn't fit into the JTextArea\n" +
          "15. Here's a load of text that doesn't fit into the JTextArea\n" +
          "16. Here's a load of text that doesn't fit into the JTextArea\n" +
          "17. Here's a load of text that doesn't fit into the JTextArea\n" +
          "18. Here's a load of text that doesn't fit into the JTextArea\n" +
          "19. Here's a load of text that doesn't fit into the JTextArea\n";
          JDialog myDialog = new JDialog();
          JTextArea jTextArea_MESSAGE = new JTextArea();
          JScrollPane jScrollPane_MESSAGE = new JScrollPane();
          jScrollPane_MESSAGE.getViewport().add(jTextArea_MESSAGE);
          jTextArea_MESSAGE.setEditable(false);
          jTextArea_MESSAGE.setLineWrap(true);
          jTextArea_MESSAGE.setWrapStyleWord(true);
          jTextArea_MESSAGE.setText(message);
          myDialog.getContentPane().add(jScrollPane_MESSAGE);
          myDialog.setSize(new Dimension(500, 200));
          myDialog.setLocation(0,100);
          myDialog.setModal(false);
          myDialog.setTitle(title);
          myDialog.setVisible(true);
        }

        void jMenuItem1_actionPerformed(ActionEvent e)
        {
          displayDialog("Dialog displayed via JMenuItem");
        }

        void jButton1_actionPerformed(ActionEvent e)
        {
          displayDialog("Dialog displayed via JButton");
        }
      }
      (Review ID: 105852)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              apikalev Andrey Pikalev
              skondamasunw Suresh Kondamareddy (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: