Reputation: 11
I try to put a scroll bar on a frame but it's not working, I don't see the scroll bar.
This my code :
Popup noticePopup = new Popup("Notice", 1500, 900);
JPanel noticePanel = new JPanel();
noticePanel.setPreferredSize(new Dimension(1500,1000));
List<List> ListNote = controller.Medicament.consultationNotice(medocSelectStr);
String noticeThis =null;
for(int n=0;n<ListNote.size();n++) {
noticeThis = (String) ListNote.get(n).get(1);
}
JTextArea noticeArea = new JTextArea(noticeThis);
noticePopup.add(noticePanel);
noticePanel.add(noticeArea);
JScrollPane jScrollPane = new JScrollPane( noticePanel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Then, add the jScrollPane to your frame
noticePopup.getContentPane().add(jScrollPane);
JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
noticePopup.getContentPane().add(scroll);
Upvotes: 1
Views: 164
Reputation: 324197
I see many potential problems:
Popup noticePopup = new Popup("Notice", 1500, 900);
JPanel noticePanel = new JPanel();
noticePanel.setPreferredSize(new Dimension(1500,1000));
Don't attempt to manually code the size of a frame or panel:
If you want the frame to fill the screen then you simply use:
noticePopup.setExtendedState(JFrame.MAXIMIZED_BOTH);
What do you expect the following code to do:
for (int n=0; n < ListNote.size(); n++)
{
noticeThis = (String) ListNote.get(n).get(1);
}
if you have more than one item in the List then you simply replace the text in the "noticeThis" variable with the last item in the list.
Instead you should be appending the text to the text area:
JTextArea noticeArea = new JTextArea(5, 20);
for (int n = 0; n < ListNote.size(); n++)
{
if (n > 0 ) noticeArea.append("\n");
noticeArea.append( (String)ListNote.get(n).get(1) );
}
Why do you have a "noticePanel":
noticePanel.add(noticeArea);
JScrollPane scroll=new JScrollPane(noticePanel, ...);
noticePopup.getContentPane().add(scroll);
By default a JPanel uses a FlowLayout which will respect the preferred size of any component added to it. I have no idea what the preferred size of the text area is but the scrollbars will only appear when the preferred size of the component is greater than the size of the scrollpane.
Just add the text area directly to the scrollpane and scrollbars will appear as needed:
JScrollPane scroll=new JScrollPane(noticeArea, ...);
noticePopup.add(scroll);
Note you don't need to use getContentPane()
since JDK1.4.
Upvotes: 1
Reputation: 14671
You're creating two scrollpanes with the same content:
JScrollPane jScrollPane = new JScrollPane( noticePanel);
and:
JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
And you're adding both of them to the popup and you're also adding the noticepanel to the popup, so there's a lot of code that needs to be removed
Popup noticePopup = new Popup("Notice", 1500, 900);
JPanel noticePanel = new JPanel();
noticePanel.setPreferredSize(new Dimension(1500,1000));
List<List> ListNote = controller.Medicament.consultationNotice(medocSelectStr);
String noticeThis =null;
for(int n=0;n<ListNote.size();n++) {
noticeThis = (String) ListNote.get(n).get(1);
}
JTextArea noticeArea = new JTextArea(noticeThis);
noticePanel.add(noticeArea);
JScrollPane scroll=new JScrollPane(noticePanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
noticePopup.getContentPane().add(scroll);
Upvotes: 1