0

I have a JList that displays the list horizontally, in one row. The JList's items are dynamically added. When there are too many items on the list. some items are cut off and not displayed. My JFrame is not resizable and that is because I have a panel with custom painting. My co-workers say that it is generally a bad idea to make JFrame not resizable. Some say I should pre-calculate sizes. Here is a simple layout of my program:

+---------------------------------------------------+
|                                                   |
|                                                   |
|              Custom Painting JPanel               |
|                                                   |
|                                                   |
|---------------------------------------------------|
|                                                   |
|   Dynamic JList:                                  |
|   +--------+    +--------+   +--------+           |
|   | Item 1 |    | Item 2 |   | Item 3 |           |
|   +--------+    +--------+   +--------+           |
|                                                   |
+---------------------------------------------------+

When too much items are added, this happens:

+---------------------------------------------------+
|                                                   |
|                                                   |
|              Custom Painting JPanel               |
|                                                   |
|                                                   |
|---------------------------------------------------|
|                                                   |
|   Dynamic JList:                                  |
|   +--------+    +--------+   +--------+   +-------|
|   | Item 1 |    | Item 2 |   | Item 3 |   | Item 4|
|   +--------+    +--------+   +--------+   +-------|
|                                                   |
+---------------------------------------------------+

The whole application (JFrame) should preferably not be resized, and also preferable fill the whole width of the application. If not resizing cannot be done, I can make do with other solutions.

EDIT: The items in this case are actually buttons

EDIT: I do have an idea. I forgot to mention that the items shown are from a an array of buttons. I have another component that controls which buttons are shown. So basically, all I have to do is add all the buttons, then sort of "hide" some, depending on the control

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
userx01
  • 273
  • 2
  • 13
  • So it sounds like you need to use a JScrollPane – ControlAltDel Apr 30 '15 at 13:10
  • They are actually buttons, I forgot to add. I updated the post. Is there really no other way? I personally think scrollpanes would not look good in this case – userx01 Apr 30 '15 at 13:14
  • 1
    So make your JFrame wide enough to fit the maximum number of buttons that can be in this JList. – Jaroslaw Pawlak Apr 30 '15 at 13:17
  • Yes, there are other ways. You seem to have some thoughts on how you think this should be done... Why don't you update your question to specify? – ControlAltDel Apr 30 '15 at 13:17
  • Oh, so I should pre-calculate the minimum sizes. Good. – userx01 Apr 30 '15 at 13:18
  • 1
    Consider `JToolBar` in `BorderLayout.SOUTH`, for [example](http://stackoverflow.com/a/11944233/230513). – trashgod Apr 30 '15 at 13:31
  • 2
    "*My co-workers say that it is generally a bad idea to make JFrame not resizable.*" And they would be right. Depending on the purpose of your buttons, you might want to add all of them and set some to disabled. Usually popping buttons on and off visibility is not user friendly. – user1803551 Apr 30 '15 at 13:52
  • A JList can't display buttons, so I have no ideas what you are really doing. If you are using real Swing components then you just need to add the components to the frame and then pack the frame. The layout manager will determine the proper size so all the components are displayed. – camickr Apr 30 '15 at 14:20
  • It can display buttons using ListCellRenderer – userx01 Apr 30 '15 at 14:32
  • So why are you using a renderer when you can use the real thing and get the benefits of the layout manager determining the proper size for you? Then you can use setResizable without a problem. – camickr Apr 30 '15 at 14:42

0 Answers0