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.LEAD ING));
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(Bor derFactory .createLow eredBevelB order());
question_txt.setLineWrap(t rue);
question_txt.setWrapStyleW ord(true);
correct_txt = new JTextArea(3,65);
correct_txt.setBorder(Bord erFactory. createLowe redBevelBo rder());
correct_txt.setLineWrap(tr ue);
correct_txt.setWrapStyleWo rd(true);
incorrect_txt = new JTextArea(3,65);
incorrect_txt.setBorder(Bo rderFactor y.createLo weredBevel Border());
incorrect_txt.setLineWrap( true);
incorrect_txt.setWrapStyle Word(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.setVerticalScro llBarPolic y(JScrollP ane.VERTIC AL_SCROLLB AR_ALWAYS) ;
Then of course I add scrollPane to the contentPane at a later time. Any suggestions or help would be most appreciated.
Thanks
shaf78
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.LEAD
textPane.setPreferredSize(
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(Bor
question_txt.setLineWrap(t
question_txt.setWrapStyleW
correct_txt = new JTextArea(3,65);
correct_txt.setBorder(Bord
correct_txt.setLineWrap(tr
correct_txt.setWrapStyleWo
incorrect_txt = new JTextArea(3,65);
incorrect_txt.setBorder(Bo
incorrect_txt.setLineWrap(
incorrect_txt.setWrapStyle
//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.setVerticalScro
Then of course I add scrollPane to the contentPane at a later time. Any suggestions or help would be most appreciated.
Thanks
shaf78
well use layout manager and add components... may be it will solve your problem.
regards
jainee
regards
jainee
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?
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));
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
You can adjust the alignment in BoxLayout.
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html
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
thanks again
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?
ASKER
never mind it worked
Also what is the intention of setting the preferred size of the panel?