Link to home
Start Free TrialLog in
Avatar of shaf78
shaf78

asked on

scrollable jpanel

Hi,

I have a swing application using with several jpanel's contained in a jframe I am trying to make it so one of the jpanels is vertically scrollable. I have tried to use a jscrollpane to achieve this goal but for some reason all that happens is the size of the jpanel keeps getting bigger. I don't know if I am screwing up the viewport or what. The jpanel itself contains just a large group of jtextpane's and jlabel's. Here is the code that I use to try and implement this scroll feature:

// Initialize components of textPane
    textPane = new JPanel();
    textPane.setLayout(new FlowLayout(FlowLayout.LEADING));
    textPane.setPreferredSize(new Dimension(350, 300));

    question_lbl = new JLabel("Question:");
    correct_lbl = new JLabel("Correct Answer:");
    incorrect_lbl = new JLabel("Incorrect Answer:");

    question_txt = new JTextArea(3,65);
    question_txt.setBorder(BorderFactory.createLoweredBevelBorder());
    question_txt.setLineWrap(true);
    question_txt.setWrapStyleWord(true);

    correct_txt = new JTextArea(3,65);
    correct_txt.setBorder(BorderFactory.createLoweredBevelBorder());
    correct_txt.setLineWrap(true);
    correct_txt.setWrapStyleWord(true);

    incorrect_txt = new JTextArea(3,65);
    incorrect_txt.setBorder(BorderFactory.createLoweredBevelBorder());
    incorrect_txt.setLineWrap(true);
    incorrect_txt.setWrapStyleWord(true);

    //add components to textPane
    textPane.add(question_lbl);
    textPane.add(question_txt);
    textPane.add(correct_lbl);
    textPane.add(correct_txt);
    textPane.add(incorrect_lbl);
    textPane.add(incorrect_txt);

    scrollPane = new JScrollPane(textPane);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  

Then of course I add scrollPane to the contentPane at a later time. Any suggestions or help would be most appreciated.

Thanks
shaf78
Avatar of Mick Barry
Mick Barry
Flag of Australia image

I don't see any obvious problems with your code, what exactly do you mean by the panel just gets bigger?
Also what is the intention of setting the preferred size of the panel?
Avatar of jainee
jainee

well use layout manager and add components... may be it will solve your problem.

regards
jainee
Avatar of shaf78

ASKER

what i mean by the jpanel just gets bigger is that the panel itself increases in height when I add components and the new size is completely visible on the frame instead of the vertical scrollbar being added to the panel, i thought that i needed the preferred size of the panel but maybe i don't, i'll try removing the preferred size
Try to put PreferredSize to the scrollPane, not to the Panel.
What is the layoutmanager of the container you are adding the scroll pane to?
Avatar of shaf78

ASKER

the layout manager that scrollpane is being added to is boxlayout the line of code used is:
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaf78

ASKER

thanks objects, that seemed to help. however, if i continue to use flow layout when i add new components to the textPane they are added horizontally on the top line, if i switch to boxlayout the jlabel's do not align on the left side of the pane but rather in the center, any suggestions?
Avatar of shaf78

ASKER

thanks a lot objects, you get the points it works great now, i'll try and read the documentation before posting anymore trivial questions

thanks again
Avatar of shaf78

ASKER

for some reason i can't accept your answer, i mean the system keeps giving me an error "you can not accept this answer" any suggestions?
Avatar of shaf78

ASKER

never mind it worked