Setting Size of JPanel or JOptionPane

偶尔善良 提交于 2019-12-23 18:16:49

问题


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...


回答1:


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;




回答2:


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 " );



回答3:


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);


来源:https://stackoverflow.com/questions/14299741/setting-size-of-jpanel-or-joptionpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!