I want to Left align the tab labeled "Unwanted Centered Panel" here is my SSCE:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Issue {
public Issue() {
}
public JPanel buildIssuePanel() {
GridBagLayout layout1 = new GridBagLayout();
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.anchor = GridBagConstraints.LINE_START;
JPanel unWantedCenteredPanel = new JPanel(layout1);
unWantedCenteredPanel.add(new JLabel("SHORT"),gbc1);
return unWantedCenteredPanel;
}
public JPanel buildWiderPanel() {
GridBagLayout layout2 = new GridBagLayout();
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.anchor = GridBagConstraints.LINE_START;
JPanel widerPanel = new JPanel(layout2);
widerPanel.add(new JLabel("LOOOOOOOOOOOONNNNNNNNGGGGGGGGGG"),gbc2);
return widerPanel;
}
public void createAndShow() {
JFrame f = new JFrame();
JTabbedPane tp = new JTabbedPane();
tp.addTab("Unwanted Centered Panel", buildIssuePanel());
tp.addTab("Wider Panel",buildWiderPanel());
f.add(tp);
f.validate();
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
Issue issue = new Issue();
issue.createAndShow();
}
}
The issue appears to be that JTabbedPane defaults to the larger JPanel child width and centers the smaller one.
I've tried setting:
GridBagConstraints.anchor = GridBagConstraints.LINE_START;
But that seems to get overridden. So, how do I get the JLabel with text "SHORT" to be left aligned?
CodePudding user response:
As @camickr suggested, adding weightx=1 made the achor=LINE_START apply itself.
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class IssueResolved {
public IssueResolved() {
}
public JPanel buildIssueResolvedPanel() {
GridBagLayout layout1 = new GridBagLayout();
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.anchor = GridBagConstraints.LINE_START;
//adding weightx = 1 applies the anchor = LINE_START and gives a left align
gbc1.weightx = 1;
JPanel unWantedCenteredPanelNowLeftAligned = new JPanel(layout1);
unWantedCenteredPanelNowLeftAligned.add(new JLabel("SHORT"),gbc1);
return unWantedCenteredPanelNowLeftAligned;
}
public JPanel buildWiderPanel() {
GridBagLayout layout2 = new GridBagLayout();
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.anchor = GridBagConstraints.LINE_START;
//adding weightx = 1 applies the anchor = LINE_START and gives a left align
gbc2.weightx = 1;
JPanel widerPanel = new JPanel(layout2);
widerPanel.add(new JLabel("LOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGG"),gbc2);
return widerPanel;
}
public void createAndShow() {
JFrame f = new JFrame();
JTabbedPane tp = new JTabbedPane();
tp.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
tp.addTab("Unwanted Centered Panel Now Left Aligned", buildIssueResolvedPanel());
tp.addTab("Wider Panel",buildWiderPanel());
f.add(tp);
f.validate();
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
IssueResolved issueResolved = new IssueResolved();
issueResolved.createAndShow();
}
}