JScrollPane Scrollbar issues

Discussion in 'Java' started by SeanSBW@gmail.com, Aug 30, 2006.

  1. Guest

    Sorry if this has been posted. I went through 3 pages of topics
    looking for it, plus numerous google searches to no avail. Does anyone
    know why in Java 1.5 the Horizontal scrollbar in a JScrollPane
    initializes to the middle?

    I've tried using:

    JScrollPane pane
    // more initialization, but the last call is as follows.
    pane.getHorizontalScrollBar().setValue(0);

    Any thoughts? I thought it might be where the visible rectangle is
    set, but I am not sure. I was also wondering why too.

    Thanks,

    Sean
    , Aug 30, 2006
    #1
    1. Advertising

  2. schrieb:
    > Sorry if this has been posted. I went through 3 pages of topics
    > looking for it, plus numerous google searches to no avail. Does anyone
    > know why in Java 1.5 the Horizontal scrollbar in a JScrollPane
    > initializes to the middle?


    It doesn't

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

    public class Test {

    public static final void main( String args[] ) {
    JTable table = new JTable(5,5);
    table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    JScrollPane scrollPane = new JScrollPane( table );
    scrollPane.setPreferredSize( new Dimension(300,200) );

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    frame.setContentPane( scrollPane );
    frame.pack();
    frame.setVisible(true);
    }
    }

    Bye
    Michael
    Michael Rauscher, Aug 30, 2006
    #2
    1. Advertising

  3. Guest

    Mike,

    Thanks for the response. I discovered the problem, however, I'm
    looking at a different situation than the simple example below. I'll
    modify the ex. below to show you my issue.

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

    public class Test {

    public static final void main( String args[] ) {
    TestPanel viewport = new TestPanel();
    JScrollPane scrollPane = new JScrollPane( viewport );
    scrollPane.setPreferredSize( new Dimension(300,200) );

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    frame.setContentPane( scrollPane );
    frame.pack();
    frame.setVisible(true);
    }
    }
    // Creates the ViewPort panel
    class TestPanel extends JPanel
    {
    public TestPanel()
    {
    setLayout(new GridLayout(0, 6));
    for (int i = 0; i < 6; i++)
    {
    // add new repeated panels
    add(new RepeatedPanel());
    }
    }
    }
    // Repeated panel in grid formation
    // ***** PROBLEM: The JTextArea inside the panel is causing
    // the JScrollPane to get a different viewing area, because every
    // textarea that's added to the parent JScrollPane is messing up the
    scrollbar locations

    class RepeatedPanel extends JPanel
    {
    JTextField singleLine1;
    JTextField singleLine2;
    JTextArea multiLine;

    public TestPanel()
    {
    singleLine1 = new JTextField("single ex. 1");
    add(singleLine1);

    singleLine2 = new JTextField("single ex. 2");
    add(singleLine2);

    multiLine = new JTextArea("multi ex. 1");
    multiLine.setLineWrap(true);
    add(multiLine);
    }
    }

    Is there a way to hide the inner JTextArea from the outside
    JScrollPane, or disable it as a scrollable area? Or would it be
    simpler to create a multi-line JTextField with word-wrap
    and text aligned with the beginning of the field?

    Thanks for your help, it is appreciated.

    Michael Rauscher wrote:
    > schrieb:
    > > Sorry if this has been posted. I went through 3 pages of topics
    > > looking for it, plus numerous google searches to no avail. Does anyone
    > > know why in Java 1.5 the Horizontal scrollbar in a JScrollPane
    > > initializes to the middle?

    >
    > It doesn't
    >
    > import java.awt.Dimension;
    > import javax.swing.*;
    >
    > public class Test {
    >
    > public static final void main( String args[] ) {
    > JTable table = new JTable(5,5);
    > table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    > JScrollPane scrollPane = new JScrollPane( table );
    > scrollPane.setPreferredSize( new Dimension(300,200) );
    >
    > JFrame frame = new JFrame();
    > frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    > frame.setContentPane( scrollPane );
    > frame.pack();
    > frame.setVisible(true);
    > }
    > }
    >
    > Bye
    > Michael
    , Aug 30, 2006
    #3
  4. Hi,
    schrieb:
    > Mike,
    >
    > Thanks for the response. I discovered the problem, however, I'm
    > looking at a different situation than the simple example below. I'll
    > modify the ex. below to show you my issue.


    Sorry, but I can't see your issue.

    Running your example (on build 1.5.0_06-b05 - after fixing some errors
    to get the code compiled) initializes the scrollbar to the left.

    Bye
    Michael
    Michael Rauscher, Aug 30, 2006
    #4
  5. Guest

    Michael,

    I saw what you meant. Sorry for the sloppy code. Here is another
    version more tightly coupled to my code. The only difference is that I
    removed the validate() call from my code and it still displays the same
    way. If you click the update button at the bottom of the window, watch
    the scrollbars, they jump to the middle and stay there. My GUI loads
    that way, which is odd to me because I removed the validate() call.

    import java.awt.Dimension;
    import javax.swing.*;
    import javax.swing.border.*;

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


    public class Test {


    public static final void main( String args[] ) {

    Box displayPane = new Box(BoxLayout.PAGE_AXIS);
    Dimension d = new Dimension(500,500);
    int idx = 0;
    TestPanel viewport = new TestPanel(idx);
    displayPane.add(viewport);

    JButton update = new JButton("update");
    update.addActionListener(viewport);
    displayPane.add(update);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    frame.setContentPane( displayPane );
    frame.pack();
    frame.setVisible(true);
    }
    }
    // Creates the ViewPort panel
    class TestPanel extends JPanel implements ActionListener {

    Dimension dfltSize = new Dimension(400, 150);
    int offset = 36;
    int og;
    Dimension d = new Dimension(1200, 225);
    Dimension vpSize = new Dimension(500,500);
    JScrollPane scrollPane;
    JPanel viewport;


    public TestPanel(int idx) {
    og = idx+1;
    scrollPane = new
    JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    viewport = createPanel(idx);
    add(scrollPane);
    scrollPane.setPreferredSize(vpSize);
    scrollPane.setMaximumSize(vpSize);
    scrollPane.setMinimumSize(vpSize);
    scrollPane.setViewportView(viewport);
    scrollPane.validate();
    }

    public JPanel createPanel(int idx){
    JPanel view = new JPanel();
    view.setLayout(new GridLayout(0, 6));
    for (int i = idx; i < idx+offset; i++) {
    // add new repeated panels
    RepeatedPanel tmp = new RepeatedPanel(i);
    tmp.setPreferredSize(dfltSize);
    tmp.setMaximumSize(dfltSize);
    tmp.setMinimumSize(dfltSize);
    view.add(new RepeatedPanel(i));
    }
    return view;
    }

    public void actionPerformed(ActionEvent e) {
    update();
    }
    public void update(){
    viewport = createPanel(og++);
    scrollPane.setViewportView(viewport);
    scrollPane.validate();
    }
    }

    class RepeatedPanel extends JPanel {
    JTextField singleLine1;
    JTextField singleLine2;
    JTextArea multiLine;
    Border txtAreaBorder;
    Dimension txtAreaSize = new Dimension(100, 100);

    public RepeatedPanel(int idx) {
    singleLine1 = new JTextField(Integer.toString(idx));
    add(singleLine1);

    singleLine2 = new JTextField(Integer.toString(idx));
    add(singleLine2);

    multiLine = new JTextArea(Integer.toString(idx));
    multiLine.setLineWrap(true);
    multiLine.setPreferredSize(txtAreaSize);
    multiLine.setMaximumSize(txtAreaSize);
    multiLine.setMinimumSize(txtAreaSize);
    add(multiLine);

    setBorder(BorderFactory.createLoweredBevelBorder());
    }
    }
    , Aug 30, 2006
    #5
  6. schrieb:
    > Michael,
    >
    > I saw what you meant. Sorry for the sloppy code. Here is another
    > version more tightly coupled to my code. The only difference is that I
    > removed the validate() call from my code and it still displays the same
    > way. If you click the update button at the bottom of the window, watch
    > the scrollbars, they jump to the middle and stay there. My GUI loads
    > that way, which is odd to me because I removed the validate() call.


    Ah, I see. The problem is that JComponent hands scrollRectToVisible up
    the hierarchy. Solutions:

    a) put multiLine into a JScrollPane or
    b) override scrollRectToVisible in RepeatedPanel

    Bye
    Michael
    Michael Rauscher, Aug 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Trahan

    JScrollPane scroll to top

    Richard Trahan, Jul 26, 2003, in forum: Java
    Replies:
    0
    Views:
    2,194
    Richard Trahan
    Jul 26, 2003
  2. Miguel De Anda
    Replies:
    0
    Views:
    596
    Miguel De Anda
    Aug 13, 2003
  3. soni29

    change jscrollpane bar?

    soni29, Aug 21, 2003, in forum: Java
    Replies:
    0
    Views:
    573
    soni29
    Aug 21, 2003
  4. Tobi Krausl
    Replies:
    0
    Views:
    1,802
    Tobi Krausl
    Nov 12, 2003
  5. Alex

    ScrollBar? Does it exist just WEB ScrollBar Control?

    Alex, Apr 1, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    257
    Alvin Bruney [MVP]
    Apr 4, 2004
Loading...

Share This Page