Question

I don't know why each button isn't displaying the text i want, and why it is displaying "..."

ButtonGroup operations = new ButtonGroup(); 

 JRadioButton[] buttons = new JRadioButton[2]; 

 String[] buttonLabels = {"Male", "Female"}; 

 for (int i=0; i<2; i++) { 
     buttons[i] = new JRadioButton(buttonLabels[i], true); 

     buttons[i].setLocation(400 + (i * 50) , 170); 
     buttons[i].setSize(35, 30); 

     add(buttons[i]); 
     operations.add(buttons[i]); 


 }

This IS what Im getting

I want button[0] to say Male and button1 to say Female

Was it helpful?

Solution

This happens when there isn't enough space for the whole text to display. You can try placing them under each other or stretching the width a little.

OTHER TIPS

Your main problem is that you're using null layout and absolute positioning. If you use decent layout managers, they'll take care of this for you. In addition, you'll have much easier to maintain GUI's that look good on all platforms and screen resolutions.

e.g.,

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class LayoutEg extends JPanel {

   public LayoutEg() {

      setBorder(BorderFactory.createTitledBorder("Personal Information"));
      setLayout(new GridBagLayout());

      addLabelWithComponent("First Name:", new JTextField(10), 0, 0);
      addLabelWithComponent("Last Name:", new JTextField(10), 0, 1);
      addLabelWithComponent("City:", new JTextField(10), 0, 2);
      addLabelWithComponent("Street:", new JTextField(10), 0, 3);
      addLabelWithComponent("Province:", 
            new JComboBox<>(new String[] { "Foo", "Bar" }), 0, 4);
      addLabelWithComponent("Home Phone:", new JTextField(10), 2, 0);
      addLabelWithComponent("Cell Phone:", new JTextField(10), 2, 1);
      addLabelWithComponent("Email Address:", new JTextField(10), 2, 2);
      addLabelWithComponent("Age", new JSpinner(
            new SpinnerNumberModel(18, 17, 100, 1)), 2, 3);
      JPanel radioPanel = new JPanel(new GridLayout(1, 0));
      radioPanel.add(new JRadioButton("Male"));
      radioPanel.add(new JRadioButton("Female"));
      addLabelWithComponent("Gender:", radioPanel, 2, 4);

   }

   private void addLabelWithComponent(String text, JComponent component, int x, int y) {
      addLabel(new JLabel(text), x, y);
      addComponent(component, x + 1, y);
   }

   private void addComponent(JComponent component, int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = new Insets(5, 5, 5, 5);
      add(component, gbc);
   }

   private void addLabel(JComponent component, int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.BOTH;
      int left = x == 2 ? 35 : 5;
      gbc.insets = new Insets(5, left, 5, 5);
      add(component, gbc);
   }

   private static void createAndShowGui() {
      LayoutEg mainPanel = new LayoutEg();

      JFrame frame = new JFrame("LayoutEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

If you simply make your JRadioButtons bigger and then run this application on another platform and the names disappear, what then? That's what the layouts are for.

For e.g., the above displays as:

enter image description here

You need to increase the width of your JRadioButton.

Turns out I had to make .SetSize greater

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top