KisnardOnline
KisnardOnline

Reputation: 740

GridBagConstraints not right aligning with GridBagConstraints.EAST

I want my multiline label to be right aligned... not sure why this is sooo difficult. I have tried using weightx and added a blank jlabel and giving it weight of 1.0. I cant get this to work. The idea is the one person is all left aligned and the other person is right aligned. (kinda like a texting app). Can anyone suggest something? I keep trying to find similar posts, but there are so many threads with GBLayout issues!

JLabel rightLblName = new JLabel(npcName);
GridBagConstraints gbc_rightLblName = new GridBagConstraints();
gbc_rightLblName.anchor = GridBagConstraints.EAST;
gbc_rightLblName.insets = new Insets(0, 0, 5, 5);
gbc_rightLblName.gridx = 5;
gbc_rightLblName.gridy = 2;
DialogueJInternalFrame.dialoguePanel.add(rightLblName, gbc_rightLblName);

JLabel rightLblImage = new JLabel("");
rightLblImage.setIcon(new ImageIcon(SpriteSheetCutter.makeColorTransparent((BufferedImage) SpriteStore.get().getSprite(npcFilename).getImage())));
rightLblImage.setBorder(DialogueJInternalFrame.raisedetched);
GridBagConstraints gbc_rightLblImage = new GridBagConstraints();
gbc_rightLblImage.insets = new Insets(0, 0, 5, 0);
gbc_rightLblImage.gridx = 6;
gbc_rightLblImage.gridy = 2;
DialogueJInternalFrame.dialoguePanel.add(rightLblImage, gbc_rightLblImage);

JMultilineLabel rightLblChatText = new JMultilineLabel(valuesSplit[1]);
GridBagConstraints gbc_rightLblChatText = new GridBagConstraints();
gbc_rightLblChatText.fill = GridBagConstraints.HORIZONTAL;
gbc_rightLblChatText.anchor = GridBagConstraints.EAST;
gbc_rightLblChatText.gridwidth = 7;
gbc_rightLblChatText.insets = new Insets(0, 0, 5, 0);
gbc_rightLblChatText.gridx = 0;
gbc_rightLblChatText.gridy = 3;
DialogueJInternalFrame.dialoguePanel.add(rightLblChatText, gbc_rightLblChatText);

enter image description here

The bottom text chunk should be hugging the right side. Thanks for any help.

Upvotes: 0

Views: 111

Answers (1)

alex2410
alex2410

Reputation: 10994

I think your problem in next line gbc_rightLblChatText.fill = GridBagConstraints.HORIZONTAL;

with that you loose effect of anchor, set fill property to gbc_rightLblChatText.fill = GridBagConstraints.NONE;, it will help you.

EDIT: seems your label extends JLabel in this case use next code, it align your component to the EAST:

gbc_rightLblChatText.weightx = 1;
gbc_rightLblChatText.fill = GridBagConstraints.HORIZONTAL;
jLabel.setHorizontalAlignment(JLabel.RIGHT);

jLabel is your label.

Upvotes: 1

Related Questions