0

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
Bauer
  • 159
  • 4
  • 13

4 Answers4

8

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.

Mateusz
  • 3,038
  • 4
  • 27
  • 41
  • 1
    I believe that a LayoutManager would take care of this, if you set text it , layoutmanager would know how to fit it in the screen – nachokk Feb 22 '14 at 18:26
  • @nachokk I think it normally would, but in this case the size is set manually, so the layout manager can't stretch it more than it's told to. – Mateusz Feb 22 '14 at 18:30
  • 1
    I correct myself, he should use layout manager not using null layout – nachokk Feb 22 '14 at 18:33
  • 2
    That's the point. If he used a layout manager, he wouldn't have to worry about sizing it appropriately. If he makes it bigger and then runs the application on another platform and the names disappear, what then? That's what the layouts are for. – Hovercraft Full Of Eels Feb 22 '14 at 18:35
  • There's no evidence that the OP is using a `null` layout, in fact, the fact they are getting "..." as output would highly suggest that a layout manager is in use, they just don't understand it - It doesn't change the answer though - it's more likely they are setting the size of the window arbitrarily instead of using `pack`... – MadProgrammer Feb 22 '14 at 20:48
7

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

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • There's no evidence that the OP is using a `null` layout, in fact, the fact they are getting "..." as output would highly suggest that a layout manager is in use, they just don't understand it - It doesn't change the answer though - it's more likely they are setting the size of the window arbitrarily instead of using `pack`...+1 – MadProgrammer Feb 22 '14 at 20:49
3

You need to increase the width of your JRadioButton.

ltalhouarne
  • 4,586
  • 2
  • 22
  • 31
  • `setSize` will have no effect if the buttons are under the control of a layout manager, which they should be, but that's another rant. `setSize` and `setLoaction` should be avoid and approximate layout managers should be employed. This would allow the OP to use `JFrame#pack` to set the initial size of the frame, taking into account the preferred size of the overall container, which would solve the issue automatically. It would also solve the issue of involving different font metrics on different systems, making the application more portable... – MadProgrammer Feb 22 '14 at 20:46
  • Removed suggestion based on your input. – ltalhouarne Feb 22 '14 at 20:52
0

Turns out I had to make .SetSize greater

Bauer
  • 159
  • 4
  • 13