Java JLabel setHorizontalAlignment with different results

15,622

Solution 1

I found (one) solution that works good for me:

label.setForeground(new Color(255, 255, 255, 0));

this let the Component "appear" but displays nothing => Every JLabel is on the same level

Solution 2

If there are no components in the North or South 'slot' of the BorderLayout, the East and West components will expand to fill that space, causing the center of that component to depend on whether the other components are visible or not.

Three possible solutions come to mind, listed in order of complexity:

  • Show all of the labels all the time, but replace the contents to make them appear invisible if there's no gate on that edge. You would have to set heights manually to keep them consistent, but that's ok for prototyping.
  • Use a different LayoutManager (though no particularly good fit comes to mind)
  • Draw the sector without relying on nested components to represent gates, doing the necessary calculations by hand.

I would probably stick with the first option for now and transition to the third one later on.

Share:
15,622
nidomiro
Author by

nidomiro

Updated on June 04, 2022

Comments

  • nidomiro
    nidomiro almost 2 years

    I want to center an JLabel inside an BorderLayout. For now I use label.setHorizontalAlignment(SwingConstants.CENTER); and label.setVerticalAlignment(SwingConstants.BOTTOM);.

    here the full Code:

    public class JSector extends JRenderPanel implements WarpGateConstants {
    
    private Sector sector;
    
    private JLabel jLabelSectorName;
    private JLabel[] jLabelWarpGate;
    
    public JSector(Sector s) {
        super(new BorderLayout());
        this.setSector(s);
        setBorder(new EmptyBorder(5, 5, 5, 5));
    
    }
    
    @Override
    public void paintView(Graphics2D g) {
        g.setColor(getBackground());
        g.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth() / 3, getHeight() / 3);
        g.setColor(getForeground());
        g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, getWidth() / 3, getHeight() / 3);
    }
    
    private void setSector(Sector s) {
        this.sector = s;
        drawSectorInfo(s);
    }
    
    public Sector getSector() {
        return sector;
    }
    
    private void drawSectorInfo(Sector s) {
        removeAll();
        if (jLabelSectorName == null || jLabelWarpGate == null) {
            this.jLabelWarpGate = new JLabel[WARPGATE_MAX_VALUE + 1];
            this.jLabelWarpGate[WARPGATE_NORTH] = new JLabel("N");
            this.jLabelWarpGate[WARPGATE_EAST] = new JLabel("E");
            this.jLabelWarpGate[WARPGATE_SOUTH] = new JLabel("S");
            this.jLabelWarpGate[WARPGATE_WEST] = new JLabel("W");
    
            for (byte i = 0; i < jLabelWarpGate.length; i++) {
                setupLabel(jLabelWarpGate[i], i);
            }
    
            this.jLabelSectorName = new JLabel("SectorName");
    
            add(this.jLabelWarpGate[WARPGATE_NORTH], BorderLayout.NORTH);
            add(jLabelWarpGate[WARPGATE_EAST], BorderLayout.EAST);
            add(jLabelWarpGate[WARPGATE_SOUTH], BorderLayout.SOUTH);
            add(jLabelWarpGate[WARPGATE_WEST], BorderLayout.WEST);
            add(jLabelSectorName, BorderLayout.CENTER);
    
        }
        for (byte i = 0; i < jLabelWarpGate.length; i++) {
            WarpGate gate = s.getWarpGate(i);
            if (gate != null && gate.exists()) {
                jLabelWarpGate[i].setToolTipText("TargetSector: " + gate.getTargetGridPos());
                jLabelWarpGate[i].setVisible(true);
            } else {
                jLabelWarpGate[i].setVisible(false);
            }
        }
        jLabelSectorName.setText(s.getName());
    
    }
    
    private static JLabel setupLabel(JLabel label, byte warpGateID) {
        Font font = label.getFont();
        font = new Font(font.getName(), Font.BOLD, font.getSize() + 2);
        label.setFont(font);
        label.setForeground(new Color(255, 150, 0));
        label.setBackground(new Color(0, 100, 255, 150));
        label.setOpaque(true);
    
        switch (warpGateID) {
            case WARPGATE_NORTH:
                label.setHorizontalAlignment(SwingConstants.CENTER);
                label.setVerticalAlignment(SwingConstants.TOP);
                break;
            case WARPGATE_EAST:
                label.setHorizontalAlignment(SwingConstants.RIGHT);
                label.setVerticalAlignment(SwingConstants.CENTER);
                break;
            case WARPGATE_SOUTH:
                label.setHorizontalAlignment(SwingConstants.CENTER);
                label.setVerticalAlignment(SwingConstants.BOTTOM);
                break;
            case WARPGATE_WEST:
                label.setHorizontalAlignment(SwingConstants.LEFT);
                label.setVerticalAlignment(SwingConstants.CENTER);
                break;
    
        }
        return label;
    }
    
    }
    

    It works good but the West and East Gates have different Vertical positions:

    West and East Gates have different Vertical positions

    I hope you can help me to solve this problem

    EDIT: I think I found the Problem. I set some Lables visible(false) and this causes the problem. But the problem is still there, how do I get these Jlabels on same line.

  • nidomiro
    nidomiro almost 12 years
    I tried setText("") but the result is the same as setVisible(false)
  • Jacob Raihle
    Jacob Raihle almost 12 years
    @niccomatik setText("") will give you an empty label, which doesn't require any space. setForeground like you noticed won't change the size preferences but still gives you an "invisible" label.