3

So i want to somehow set the size of my JOptionPane - without setting it, it is just too large for my purposes and won't look nice. here is my code:

   JScrollPane scrollpane = new JScrollPane(); 
   String categories[] = { "1. Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One", "2. Problem Two", "3. Extended Family", "4. Extended Family", "5. Extended Family"};
   JList list = new JList(categories);
   errorListCellRenderer cellRenderer = new errorListCellRenderer();
   list.setCellRenderer(cellRenderer);
   scrollpane = new JScrollPane(list);
   JPanel panel = new JPanel(); 
   panel.add(scrollpane);
   JOptionPane.showMessageDialog(null, panel, "Error List",  
                                          JOptionPane.PLAIN_MESSAGE);

when i set the size of the JPanel as such:

panel.setPreferredSize(new Dimension(500, 200));

the scroll bar disappears and hence not everything is displayed.

any ideas? Java n00b...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
BigBug
  • 6,202
  • 23
  • 87
  • 138

3 Answers3

16

If you just want to change the size of a JOptionPane window you can do that by using the UIManager. Like this:

UIManager.put("OptionPane.minimumSize",new Dimension(500,500)); 
JOptionPane.showMessageDialog(null, "Hi " );
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
Lori
  • 161
  • 1
  • 3
7

give the scrollpane a preferredSize, then add it directly to the optionPane

String categories[] = { "1. Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One",
                       "2. Problem Two", "3. Extended Family", "4. Extended Family", "5. Extended Family"};
JList list = new JList(categories);
JScrollPane scrollpane = new JScrollPane(list);
scrollpane.setPreferredSize(new Dimension(300,125));//<-----any size you want
JOptionPane.showMessageDialog(null, scrollpane, "Error List",JOptionPane.PLAIN_MESSAGE);
Michael Dunn
  • 818
  • 5
  • 3
6

Please do the following changes and see;

  1. Do not put the JScrollPane within another Panel. Pass the JScrollPane directly within the showMessageDialog().

  2. Also use scrollPane.getViewport().add(list);

EDIT: I just tried the following, and the scroll bar is visible for me as the items increased.

 JScrollPane scrollpane = new JScrollPane(); 
       String categories[] = { "1. Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One Problem One", "2. Problem Two", "3. Extended Family", "4. Extended Family", "5. Extended Family"
               ,"6. Extended Family","7. Extended Family","8. Extended Family","9. Extended Family"};
       JList list = new JList(categories);

       scrollpane = new JScrollPane(list);

       JPanel panel = new JPanel(); 
       panel.add(scrollpane);

       scrollpane.getViewport().add(list);
       JOptionPane.showMessageDialog(null, scrollpane, "Error List",  
                                              JOptionPane.PLAIN_MESSAGE);

screenshot attached;

enter image description here

dinukadev
  • 2,279
  • 17
  • 22
  • 1.not putting it onto a panel doesn't change the size or fix it.... i initially had it this way, but then i realized i knew how to change the size of a panel and that, changing the panel size would help. however, it introduced another problem. 2. i can make this change, but it's not going to solve the problem.. – BigBug Jan 13 '13 at 01:37
  • The point of using a Scroll pane is to put elements to it thereby letting it decide whether or not to show the scroll bars when the size increases. So there is no point what so ever of wrapping a scroll pane inside another panel. What you can do is to put the list within the panel and put that panel within the scroll pane. – dinukadev Jan 13 '13 at 03:47
  • i've made the changes you suggest now. however, the scroll bar still does not appear. instead, the entire box gets larger and larger as i add more and more entries - this is not desired as it will become too large. what do i need to change in order to keep the scroll bar and not have the box become so large? thanks for your help btw – BigBug Jan 13 '13 at 03:59
  • i've also tried adding verticalscrollbarpolicy: scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); but the scroll bar does not ever appear, unless i change it to ALWAYS - at which point it appears but doesn't work... the box still gets too large – BigBug Jan 13 '13 at 04:01
  • Hi again, i just edited my post to include the sample i just tried on my eclipse workbench. The scroll bar was visible for me. can you try the code snippet i just posted under the EDIT section. It is working. – dinukadev Jan 13 '13 at 04:11
  • Hi :) i figured out why it's not working with your help. i put the list into an html table so i can keep the width of the list at a reasonable length and allow the text to go on multiple lines instead of one line. however this is messing it up... guess i need to find a solution for that. thanks though! – BigBug Jan 13 '13 at 04:16
  • glad to help :). You could try wrapping within a text area on your list renderer and wrapping the text within that text area. Just an idea though. Cheers. – dinukadev Jan 13 '13 at 04:18
  • 1
    Tip re screenshots: Alt+Print Screen will capture only the area of the focused GUI. +1 – Andrew Thompson Jan 13 '13 at 04:54