Reputation: 1
I have been trying to add 2 radio buttons which indicate Modes like A and B with a long description like shown in the below image.
I need,
I have tried few available suggestions online and not been successful so far. Please suggest me on how to achieve the same.
JLabel label = new JLabel();
label.setHorizontalAlignment( SwingConstants.RIGHT);
label.setName("LABEL NAME");
label.setVisible(true);
JRadioButton modeA = new JRadioButton("<html><dl><dt><b>Mode A: </b></dt><dd>This sentence is really long and is expected to be displayed in 2 or more lines.</dd></dl></html>");
modeA.setFocusPainted(false);
modeA.setBorder(BorderFactory.createEmptyBorder(0, 0, 12,0));
modeA.setName("Mode_A");
modeA.setVisible(true);
JRadioButton modeB = new JRadioButton("<html><dl><dt><b>Mode B: </b></dt><dd>This sentence is another example of a long string and is expected to be displayed in 2 or more lines.</dd></dl></html>");
modeB.setFocusPainted(false);
modeB.setBorder(BorderFactory.createEmptyBorder(12, 0, 0, 0));
modeB.setName("Mode_B");
modeB.setVisible(true);
ButtonGroup grp = new ButtonGroup();
grp.add(modeA);
grp.add(modeB);
JPanel togglePanel = new JPanel();
togglePanel.setLayout(new BoxLayout (togglePanel, BoxLayout.Y_AXIS));
togglePanel.add(modeA);
togglePanel.add(modeB);
int hgap = 15;
int vgap = 15;
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, hGap, vGap));
mainPanel.setOpaque(false);
mainPanel.add(label);
mainPanel.add(togglePanel);
Expected result is,
both have not worked for me.
Upvotes: 0
Views: 52
Reputation: 51445
When I ran your code, this is what your GUI looked like:
With a few modifications, this is what I came up with:
I used @Progman's suggestion and put the text in an HTML table. And yes, I used a <br>
to break the sentences into two lines.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class JRadioButtonExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JRadioButtonExample());
}
@Override
public void run() {
JFrame frame = new JFrame("JRadioButton Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
int hgap = 15;
int vgap = 15;
JPanel mainPanel = new JPanel(
new FlowLayout(FlowLayout.LEFT, hgap, vgap));
JLabel label = new JLabel("Please select either of the modes:");
label.setHorizontalAlignment(SwingConstants.RIGHT);
label.setName("LABEL NAME");
String text = "<html><table><tr><td>";
text += "Mode A:</td><td>This sentence is really long";
text += " and is expected<br>to be displayed in 2 or more lines.";
text += "</td></tr></table></html>";
JRadioButton modeA = new JRadioButton(text);
modeA.setFocusPainted(false);
modeA.setBorder(BorderFactory.createEmptyBorder(0, 0, 12, 0));
modeA.setName("Mode_A");
text = "<html><table><tr><td>";
text += "Mode B:</td><td>This sentence is another example of a";
text += " long string<br>and is expected to be displayed";
text += " in 2 or more lines.";
text += "</td></tr></table></html>";
JRadioButton modeB = new JRadioButton(text);
modeB.setFocusPainted(false);
modeB.setBorder(BorderFactory.createEmptyBorder(12, 0, 0, 0));
modeB.setName("Mode_B");
ButtonGroup grp = new ButtonGroup();
grp.add(modeA);
grp.add(modeB);
JPanel togglePanel = new JPanel();
togglePanel.setLayout(new BoxLayout(togglePanel, BoxLayout.Y_AXIS));
togglePanel.add(modeA);
togglePanel.add(modeB);
mainPanel.add(label);
mainPanel.add(togglePanel);
return mainPanel;
}
}
Upvotes: 2