2

I need to put n(10 for instance, the actual number depends on some circumstances) JLabel in a JPanel in a row. Those JLabels should have size 50x50. What layout should I select? As said in this answer we should avoid using setXxxSize(...).

Here is what I'm trying to get:

enter image description here

Community
  • 1
  • 1
  • 1
    My solution years ago was to create my own LayoutManager to solve this problem – ControlAltDel Apr 18 '16 at 15:16
  • [Override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513)? – trashgod Apr 18 '16 at 15:32
  • What do the labels contain? Text? Image? Just background color? What should happen if there is not enough room horizontally to fit them all? What if the user resizes the window? The answers to these questions will make a difference in what layout manager to use. – FredK Apr 18 '16 at 15:45
  • @FredK What do all these got to do with positioning? And how does the bakground color probably reflect the solution? The panel should be resized so it would be enough space to put all them. –  Apr 18 '16 at 15:46
  • @ControlAltDel Ough.... Sounds too complicated for such an innocent problem. Do we really need to do it? Maybe there's some instant layout manager allowing set the size of components explicitly? –  Apr 18 '16 at 15:49
  • @DmitriiBundin actually, I recall finding it pretty interesting and not so hard. Just saying: You can keep checking StackOverflow / your email for a solution on this question, or you can get busy working on it yourself. – ControlAltDel Apr 18 '16 at 15:54
  • @DmitriiBundinm FredK asked you valid questions. Why are you picking a random size of 50x50. A label will be sized automatically based on the text/icon added to the label, so there should be no need to hardcode a size. Hardcoding a size is generally related to incorrect program design. Based on the lack of information provided in the question trashgod has provided the answer. Then you just use a FlowLayout – camickr Apr 18 '16 at 16:18

0 Answers0