md1hunox
md1hunox

Reputation: 3955

Not able to change size of JLabel in GridLayout

I have a label which would display strings from "0 ft" to say "999 ft", so maximum it would take string of size 6. I'm using a grid layout so that i could place that label in a particular column where it should be. I want a background for the label too. But the problem is i cant properly set the size of background.

I want to size down the black part to only the required amount which im not ablet to do

    public Controller() {
    super(new GridLayout(9, 9));
    try {
        Background = ImageIO.read(getClass().getResourceAsStream(
                "background.jpg"));
        TestImage = ImageIO.read(getClass().getResourceAsStream(
                "VTOLHorizontalLevelIndicator.png"));
        setPreferredSize(new Dimension(Background.getWidth(null),
                Background.getHeight(null)));

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    for (Direction direction : Direction.values()) {
        directionMap.put(direction, false);
    }
    setKeyBindings();
    Timer timer = new Timer(TIMER_DELAY, new TimerListener());
    timer.start();
    int i = 3;
    int j = 11;
    JLabel[][] panelHolder = new JLabel[i][j];
    setLayout(new GridLayout(i, j));

    for (int m = 0; m <= 2; m++) {
        for (int n = 0; n <= 10; n++) {
            if (m == 1 && n == 1) {
                lblAltitude.setFont(new Font("Gotham Bold",1,12));
                lblAltitude.setToolTipText("Altitude");
                lblAltitude.setOpaque(true);
                lblAltitude.setBackground(Color.black);
                lblAltitude.setMinimumSize(new Dimension(50,25));
                add(lblAltitude);
            } else {
                panelHolder[m][n] = new JLabel(" ");
                add(panelHolder[m][n]);
            }
        }
    }

}

the above code shows the constructor from which I'm initializing the the GridLayout and the label. Am I using the proper layout? My main concern is getting labels in right places , may not be in a grid fashion but at certain coordinates in this GUI.

Upvotes: 0

Views: 1976

Answers (1)

Rob Wagner
Rob Wagner

Reputation: 4421

GridLayout will expand all of the components to fill the panel (giving each component equal size).

Upvotes: 4

Related Questions