1

enter image description here

I have a List of JPanel and each element of a list have 10 JPanel elements inside it which contains a picture. (as the Picture)

I set Float layout for aligning them horizontally one after another. (Each row JPanel elements)

I put each element of this list on another outer JPanel vertically and everything is ok. (Each Vertical JPanel)

now I want to put the above label F1 till F10 exactly at the center of the first now elements ? how am going to do that ? any recommendation ?

Take note I can't use TitledBorder (with title and no border) for the first row elements because I have a selection function for each element and If I do this, It select the whole first row element (element + titledborder) which is pretty ugly and not similar to the other rows ?

do you hae any solution ?

Mehdi
  • 3,795
  • 3
  • 36
  • 65

1 Answers1

2

Make the top row a JPanel having the default layout, FlowLayout. Add ten instances of a custom JLabel in which you override getPreferredSize() to return the nominal picture width and a height no less than that returned by the parent's implementation.

image

private static final int W = 50;

private static class MyLabel extends JLabel {

    public MyLabel(String text) {
        super(text);
        this.setHorizontalAlignment(CENTER);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(W, super.getPreferredSize().height);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    See also [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?*](http://stackoverflow.com/q/7229226/230513) – trashgod Nov 30 '15 at 12:36
  • this does not help me dude because I'm selecting the elements as I told you but I made a JPanel and put ar Array of JLabel with the same preferred size of the element and put it at the top of the others – Mehdi Nov 30 '15 at 12:38
  • @transhgod thank you anyways for your effort helping me. I solve the problem with my way but your way was also very good but didn't fit on my problem ~ – Mehdi Nov 30 '15 at 13:00
  • Glad it helped point the way forward; don't forget that you can [answer your own question](http://meta.stackoverflow.com/q/17463/163188), too. – trashgod Nov 30 '15 at 16:48