GamingLegion
GamingLegion

Reputation: 23

JLabel strech to end of container

I'm trying to insert a JLabel into a JTabbedPane and have it stretch from the left to the right ends. I can insert it and manipulate the width manually, but don't know how to do it automatically. Below is what it looks like now. The white border should ideally stretch to the right side. enter image description here

I'm using a GroupLayout for this and I am very unfamiliar with it so that's why I'm asking. Below is the code for the page.

public class Gen1 extends JPanel {
    private MaterialTabbed tabs;
    private JPanel red, blue, yellow;
    
    public Gen1() {
        initComponents();
        setOpaque(false);
        initRed();
    }
    
    private void initRed() {
        JLabel gcea = new JLabel();
        gcea.setText("Gotta Catch 'Em All");
        gcea.setBorder(new LineBorder(Color.WHITE, 5));
        gcea.setBackground(new Color(251, 188, 4));
        
        GroupLayout redLayout = new GroupLayout(red);
            red.setLayout(redLayout);
            redLayout.setHorizontalGroup(redLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(redLayout.createSequentialGroup()
                    .addComponent(gcea, GroupLayout.DEFAULT_SIZE, tabs.getUI().getTabBounds(tabs, 0).width, Short.MAX_VALUE)
                    .addContainerGap(518, Short.MAX_VALUE))
            );
            redLayout.setVerticalGroup(redLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(redLayout.createSequentialGroup()
                    .addComponent(gcea)
                    .addContainerGap(256, Short.MAX_VALUE))
            );
    }

    private void initComponents() {
        tabs = new MaterialTabbed();
        red = new ChecklistPage(new BorderLayout());
        blue = new ChecklistPage(new BorderLayout());
        yellow = new ChecklistPage(new BorderLayout());
        
        tabs.setTabPlacement(JTabbedPane.RIGHT);
                
        GroupLayout redLayout = new GroupLayout(red);
        red.setLayout(redLayout);
        redLayout.setHorizontalGroup(redLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 655, Short.MAX_VALUE));
        redLayout.setVerticalGroup(redLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 332, Short.MAX_VALUE));
        tabs.addTab("", ImageResize.resize(new ImageIcon(this.getClass().getResource("/gamecovers/red.png")), 100), red);

        GroupLayout blueLayout = new GroupLayout(blue);
        blue.setLayout(blueLayout);
        blueLayout.setHorizontalGroup(blueLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 655, Short.MAX_VALUE));
        blueLayout.setVerticalGroup(blueLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 332, Short.MAX_VALUE));
        tabs.addTab("", ImageResize.resize(new ImageIcon(this.getClass().getResource("/gamecovers/blue.png")), 100), blue);
        
        GroupLayout yellowLayout = new GroupLayout(yellow);
        yellow.setLayout(yellowLayout);
        yellowLayout.setHorizontalGroup(yellowLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 655, Short.MAX_VALUE));
        yellowLayout.setVerticalGroup(yellowLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 332, Short.MAX_VALUE));
        tabs.addTab("", ImageResize.resize(new ImageIcon(this.getClass().getResource("/gamecovers/yellow.png")), 100), yellow);
        
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(tabs, GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(tabs, GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE).addContainerGap()));
    }
}

MaterialTabbed is just a custom JTabbedPane and ChecklistPage is a custom JPanel. Also the setBackground method isn't working either, if anyone has a fix for that as well.

Upvotes: 0

Views: 42

Answers (0)

Related Questions