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

ScrollPane with scrollbar as needed can not be reset correctly

XMLWordPrintable

    • x86
    • windows_nt


      On Win32: Kestrel

      ScrollPane can not provide a proper scroll bar by using setSize(width, height).
      On solaris a proper scroll bar can be provieded.
      The behaviour between solaris and win32 is different.


      To reproduce;
      1. Compile and run the attached "DispTest.java"
      2. Open a file which size is wider and longer than the ScrollPane.

      The file can be loaded properly, bug the proper size of scroll bar
      is not applied, though ScrollPane is setSize() specified with the proper width
      and height.


      Is the understanding that
      applying setSize() to ScrollPane which has scroll bar with "as needed"
      provides a proper size of scroll bar(not changing the Frame size itself)
      right?
      From the behaviour on solaris, it seems right.



      ***bugtraq can't accept attaching the file (error occurs),
      So such a long source is pasted here.***


      ---DispTest.java----Start----

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;

      class DispTest extends Frame {
          ScrollPane sPane;
          MyCanvas cv;
          String fName ="Serif";
          int fSize = 12;
          int fStyle = Font.PLAIN;
          Dimension d;
          StringBuffer sb;

          public DispTest() {
              super("Graphics2D display test");
              MenuBar mbar = createMenuBar();
              setMenuBar(mbar);
              add(sPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED));
              sPane.add(cv = new MyCanvas());
       cv.font = new Font(fName, fStyle, fSize);
          }

          MenuBar createMenuBar() {
              Menu fontMenu;
              Menu fileMenu;

              MenuBar mbar = new MenuBar();
              fileMenu = new Menu("File");
              mbar.add(fileMenu);
              MenuItem openItem = new MenuItem("Open...");
              openItem.addActionListener( new ActionListener() {
                  public void actionPerformed( ActionEvent e ) {
                      openAction();
                  }
              });
              fileMenu.add(openItem);
              fileMenu.addSeparator();
              MenuItem exitItem = new MenuItem("Exit");
              exitItem.addActionListener( new ActionListener() {
                  public void actionPerformed( ActionEvent e ) {
                      System.exit(0);
                  }
              });
              fileMenu.add(exitItem);

              fontMenu = new Menu("Font");
              Menu nameMenu = new Menu("Name");
              MenuItem[] nameItem = { new MenuItem("Serif"),
                  new MenuItem("Sansserif"),
                  new MenuItem("Monospaced"),
                  new MenuItem("Dialog"),
                  new MenuItem("DialogInput")
              };
              for (int i = 0; i < 5; i++ ) {
                  nameMenu.add(nameItem[i]);
                  nameItem[i].addActionListener( new ActionListener() {
                      public void actionPerformed( ActionEvent e) {
                          fName = ((MenuItem)e.getSource()).getLabel();
                          fontChangeAction();
                      }
                  });
              }
              fontMenu.add(nameMenu);

              Menu sizeMenu = new Menu("Size");
              MenuItem sizeItem[] = { new MenuItem("10"),
                  new MenuItem("12"),
                  new MenuItem("14"),
                  new MenuItem("18"),
                  new MenuItem("24")
              };
              for (int i = 0; i < 5; i++ ) {
                  sizeMenu.add(sizeItem[i]);
                  sizeItem[i].addActionListener( new ActionListener() {
                      public void actionPerformed( ActionEvent e) {
                          String strItem = ((MenuItem)e.getSource()).getLabel();
                          fSize = Integer.parseInt(strItem);
                          fontChangeAction();
                      }
                  });
              }
              fontMenu.add(sizeMenu);

              Menu styleMenu = new Menu("Style");
              CheckboxMenuItem styleItem1 = new CheckboxMenuItem("Bold", false);
              CheckboxMenuItem styleItem2 = new CheckboxMenuItem("Italic", false);
              styleMenu.add(styleItem1);
              styleMenu.add(styleItem2);
              styleItem1.addItemListener( new ItemListener() {
                  public void itemStateChanged( ItemEvent e ) {
                      fStyle ^= Font.BOLD;
                      fontChangeAction();
                  }
              });
              styleItem2.addItemListener( new ItemListener() {
                  public void itemStateChanged( ItemEvent e ) {
                      fStyle ^= Font.ITALIC;
                      fontChangeAction();
                  }
              });

              fontMenu.add(styleMenu);
              mbar.add(fontMenu);
              return mbar;
          }

          void openAction() {
              FileDialog fDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
              fDialog.show();
              String selectFile;
              if ((selectFile = fDialog.getFile()) != null ) {
                  String fileName = fDialog.getDirectory() + selectFile;
                  File f = new File( fileName );
                  sb = new StringBuffer((int)f.length());
                  int nLine = 0;
                  try {
                      String strLine;
                      BufferedReader br = new BufferedReader( new FileReader(f));
                      strLine = br.readLine();
                      while( strLine != null ) {
                          sb.append(strLine);
                          sb.append("\n");
                          nLine++;
                          strLine = br.readLine();
                      }
                  } catch(Exception e) {
                      System.out.println( e.toString() );
                  }
                  cv.setLines(nLine);
                  cv.isFirst = true;
                  //cv.setSize(400, 300);
                  sPane.setScrollPosition(0,0);
                  cv.repaint();
              }
          }

          void fontChangeAction() {
              cv.font = new Font( fName, fStyle, fSize);
              cv.isFirst = true;
             // cv.setSize(400, 300);
              sPane.setScrollPosition(0, 0);
              cv.repaint();
          }

          class MyCanvas extends Canvas {
              public boolean isFirst = false;
              int nLine;
                      Font font;


              public void setLines( int n ) {
                  nLine = n;
              }

              public MyCanvas() {
                  super();
                  setSize(400, 300);
              }

              public void paint( Graphics g ) {
                  if ( sb == null ) return;
                  String str = sb.toString();
                  g.setFont(font);
                  FontMetrics fm = g.getFontMetrics();
                  int lineHeight = fm.getHeight() * 6 / 5;
                  int x = 0;
                  int y = 0;
                  int maxWidth = getSize().width;
                  int maxHeight = lineHeight * (nLine+1);
                  int index = 0;
                  int nextIndex= 0;

                  setSize( maxWidth, maxHeight);
                  long l0 = System.currentTimeMillis();
                  while( nextIndex !=-1 ) {
                      boolean bResize = false;
                      nextIndex = str.indexOf('\n', index);
                      String lineStr;
                      if (nextIndex==-1) {
                          lineStr = str.substring(index);
                      } else {
                          lineStr = str.substring(index, nextIndex);
                      }
                      index = nextIndex +1;
                      int lineWidth = fm.stringWidth(lineStr);
                      if (lineWidth > maxWidth) {
                          maxWidth = lineWidth;
                          bResize = true;
                      }
                      y += lineHeight;

                      if (bResize) {
                          setSize( maxWidth, maxHeight);
                          bResize = false;
                      }
                      g.drawString( lineStr, x, y );
                  }
                  long l1 = System.currentTimeMillis() - l0;
                  if (isFirst) {
                      System.out.println("drawing done in " +l1+" msec.");
                      isFirst = false;
                  }
               }
          }

          public static void main(String[] args) {
              DispTest obj = new DispTest();
              obj.addWindowListener( new WindowAdapter() {
                  public void windowClosing( WindowEvent e ) { System.exit(0);}
              });
              obj.pack();
              obj.setSize(400, 300);
              obj.show();
          }
      }

      ---DispTest.java---End------

            uwesunw Uwe Uwe (Inactive)
            eishidasunw Etsuko Ishida (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: