Question

I have the below image. It consists of a JPanel containing one label for the picture, another one for the '@usuario1' text, another one for 'contenido', and another one for '#demo'. Below the image is the code which constructs it.

src

SettingsManagerTab.panelForTweet1 = new JPanel();
SettingsManagerTab.panelForTweet1.setOpaque(true);
SettingsManagerTab.panelForTweet1.setLayout(new net.miginfocom.swing.MigLayout());
this.add(SettingsManagerTab.panelForTweet1);

SettingsManagerTab.labelForTweetPhoto1 = new JLabel(new ImageIcon(
        SettingsManagerTab.class.getResource("/res/User1.png")));
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetPhoto1);

SettingsManagerTab.labelForTweetUsername1 = new JLabel("@usuario1");
final Font contentFont = new Font(SettingsManagerTab.labelForTweetUsername1.getFont()
        .getName(), Font.BOLD, SettingsManagerTab.labelForTweetUsername1.getFont()
        .getSize());
final Font userFont = new Font(contentFont.getName(), Font.BOLD,
        (int) (contentFont.getSize() * 0.7));
SettingsManagerTab.labelForTweetUsername1.setFont(userFont);
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetUsername1);

SettingsManagerTab.labelForTweetContent1 = new JLabel("contenido");
SettingsManagerTab.labelForTweetContent1.setFont(contentFont);
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetContent1);

SettingsManagerTab.labelForTweetHashtag1 = new JLabel("#demo");
SettingsManagerTab.labelForTweetHashtag1.setFont(contentFont);
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetHashtag1);

But what I want to achieve is this:

goal

It looked trivial to me. Simply make growy the label with the picture, and then add the '@usuario1' with wrap option so the next component (the 'contenido' label) is added at the beginning of the next line and, since the picture label is occupying all the height of the JPanel, the mentioned next line will start just below the '@usuario1' label. However what seems to happen is that the next line starts just below the picture label, but since the label is taking all the height of the JPanel, it isn't shown, so I get a result which is worse than the beginning.

Is my goal possible? And if so, how?

Was it helpful?

Solution

Go through PDF present at link for MiGLayout or you can try GridBagLayout. If you want to use GridBagLayout this link might be helpful. Using GridBagLayout you can set the component as you want.

Take reference of below example:

    panel.add(comp1);
    panel.add(comp2, "span 2 2"); // The component will span 2x2 cells.
    panel.add(comp3, "wrap"); // Wrap to next row
    panel.add(comp4);
    panel.add(comp5, "wrap"); // Note that it "jumps over" the occupied cells.
    panel.add(comp6);
    panel.add(comp7);


O/p:

enter image description here

OTHER TIPS

Haha! Managed to do it thanks to your indications sachin, but with GridBagLayout.

SettingsManagerTab.panelForTweet1.setLayout(new GridBagLayout());
this.add(SettingsManagerTab.panelForTweet1);

SettingsManagerTab.labelForTweetPhoto1 = new JLabel(new ImageIcon(
        SettingsManagerTab.class.getResource("/res/User1.png")));

final GridBagConstraints a = new GridBagConstraints(), b = new GridBagConstraints(), c = new GridBagConstraints(), d = new GridBagConstraints();
a.gridheight = GridBagConstraints.REMAINDER;
a.gridx = 0;
a.weightx = 0.1;
a.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetPhoto1, a);

SettingsManagerTab.labelForTweetUsername1 = new JLabel("@usuario1");
final Font contentFont = new Font(SettingsManagerTab.labelForTweetUsername1.getFont()
        .getName(), Font.BOLD, SettingsManagerTab.labelForTweetUsername1.getFont()
        .getSize());
final Font userFont = new Font(contentFont.getName(), Font.BOLD,
        (int) (contentFont.getSize() * 0.7));
SettingsManagerTab.labelForTweetUsername1.setFont(userFont);
b.weightx = 1;
b.gridwidth = 2;
b.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetUsername1, b);

SettingsManagerTab.labelForTweetContent1 = new JLabel("contenido");
SettingsManagerTab.labelForTweetContent1.setFont(contentFont);
c.gridheight = GridBagConstraints.REMAINDER;
c.weightx = 1;
c.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetContent1, c);

SettingsManagerTab.labelForTweetHashtag1 = new JLabel("#demo");
SettingsManagerTab.labelForTweetHashtag1.setFont(contentFont);
d.weightx = 30;
d.anchor = GridBagConstraints.LINE_START;
SettingsManagerTab.panelForTweet1.add(SettingsManagerTab.labelForTweetHashtag1, d);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top