Results 1 to 8 of 8
  1. #1
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default setSize on BoxLayout

    I've looked all over google, bing, yahoo and I can't get this to work.

    I'm trying to get the size of my Jtext box to be reasonable big enough to fit up to a 2 digit number in it, NOT a paragraph. How can I do this? I've try:

    setSize(int l, int w);

    setSize() with no parameters

    setPreferredSize();

    Java Code:
     txtFld = new JTextField(2);
          txtFld.setSize(1);
          c.add(txtFld);
    show I change my layout? We were required to use flowLayout, but i find the boxLayout makes more sense to use for the program i'm writing. please help and thank you so much!

  2. #2
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Im not sure exactly what you are asking for but here is some code you might be able to learn from.

    Java Code:
    public class TextField extends JFrame {
    
    
        public TextField() {
                setTitle("Text Test");
                setSize(250, 75); // the size of the frame is here.
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JLabel label = new JLabel("Type Something to the right: ", JLabel.RIGHT);
                JTextField textField = new JTextField(2); [COLOR="Red"]//size of the text box here[/COLOR]
                FlowLayout pageLayout = new FlowLayout();
                setLayout(pageLayout);
                add(label);
                add(textField);
                setVisible(true);
        }
        public static void main(String[] args) {
            TextField test = new TextField();
        }
    
    }

  3. #3
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    what i am asking is:

    my JTextBox is big enough to write a paragraph in, almost the same size as the GUI. i only want it to be one line and big enough to fit a two digit number it. what you said, i've already done. i just need to make the size of the JtextBox where the use types input to be smaller.

    this is what i have

    Java Code:
    
    
    big space
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    x
    this is what i need

    Java Code:
    small space

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What a JTextbox?

    As there are different JTextWhatevers available, it would help us greatly if you are specific and precise with your description of your problem.

    Luck.

  5. #5
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Sorry I meant a JTextField, the place in a GUI where the user inputs data from the keyboard.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by smray7 View Post
    Sorry I meant a JTextField, the place in a GUI where the user inputs data from the keyboard.
    One possible solution: You can place the JTextField in a JPanel and then add that JPanel into the container that uses BoxLayout.

    If you need more detailed help, you'll want to create and post a Short, Self Contained, Correct (Compilable), Example or SSCCE

  7. #7
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Here's my code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class CandyLandJFrame extends JFrame
    {
      private Container c = getContentPane();
      private ButtonGroup radioButtonGroup;
      private JRadioButton RadioButton1, RadioButton2, RadioButton3, RadioButton4;
      private JLabel JLabelOne; 
      private JTextField txtFld;
      private int pounds;
      private JButton button1, button2;
      
      public static void main (String[]args)
      {
        CandyLandJFrame candyFrame = new CandyLandJFrame(); ///object created
        candyFrame.setSize(500,600); //setting frame size
        candyFrame.setVisible(true); //make it visible
      }
        public CandyLandJFrame()
        {
          setTitle("Welcome to CandyLandy!");  //JFrame message title
          
          getContentPane().setLayout(
          new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));  // layout of GUI
          
          ItemHandler handlerObject = new ItemHandler();
          radioButtonGroup = new ButtonGroup();
          Handler handler = new Handler();
          
          RadioButton1 = new JRadioButton("Dumdums"); // button selection for dumdums
          c.add(RadioButton1);
          radioButtonGroup.add(RadioButton1);
          RadioButton1.addItemListener(handlerObject);
          
          RadioButton2 = new JRadioButton("Snickerballs");    //button selection for snickerballs
          c.add(RadioButton2);
          radioButtonGroup.add(RadioButton2);
          RadioButton2.addItemListener(handlerObject);
          
          RadioButton3 = new JRadioButton("Sweettarts");  //button selction for sweettarts
          c.add(RadioButton3);
          radioButtonGroup.add(RadioButton3);
          RadioButton3.addItemListener(handlerObject);
          
          RadioButton4 = new JRadioButton("Skittles"); //button selection for skittles
          c.add(RadioButton4);
          radioButtonGroup.add(RadioButton4);
          RadioButton4.addItemListener(handlerObject);
          
          JLabelOne = new JLabel("How many pounds? "); 
          c.add(JLabelOne);
          
          txtFld = new JTextField(2); //textbox for user input
          c.add(txtFld);
          
          button1 = new JButton("Enter"); //enter to submit for choice of candy
          c.add(button1);
          button1.addActionListener(handler); 
          
          button2 = new JButton("Submit");
          c.add(button2);
          button2.addActionListener(handler);
       
        } //end of constructor
        
        private class ItemHandler implements ItemListener  // creation of Handler
        {
          public void itemStateChanged(ItemEvent ie) //listener for JRadio buttons and JTextField
          {
            //statements here
          }
        }// end ItemHandler
          private class Handler implements ActionListener //for the JTextField
          {
            public void actionPerformed(ActionEvent ae)
            {
             //statments here
            }
          }
    } //end class
    Last edited by smray7; 12-15-2010 at 11:17 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would suggest several things:

    1) Avoid setting the size of the JFrame. Add your components and let the components themselves set the size of things by calling pack on the JFrame.

    2) Use logical variable names, example below.

    3) Place things that belong together in JPanels. For instance, the JRadioButtons can all go in a JPanel using a GridLayout set to 1 vertical column, and the submit and enter buttons in a JPanel using a GridLayout that is set to have one row. The pound JLabel and JTextField would work best in their own JPanel too.

    4) An array of JRadioButtons would work well here.

    5) I don't think that you need add a listener to the JRadioButtons themselves as you are only interested in their state when a JButton is pressed. So query them (or better, get the ButtonGroups actionCommand) after a button such as the enter button, has been pressed in the button's ActionListener.

    e.g.,

    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    
    public class CandyLandJFrame extends JFrame {
        private static final String[] RADIO_LABELS = {
            "Dumdums", "Snickerballs", "Sweettarts", "Skittles"
        };
        private Container c = getContentPane();
        private ButtonGroup radioButtonGroup;
        private JRadioButton[] radioButtons = new JRadioButton[RADIO_LABELS.length];
                                                               
        private JLabel poundLabel;
        private JTextField poundField; 
        private int pounds;
        private JButton enterBtn, submitBtn;
    
        public static void main(String[] args) {
            CandyLandJFrame candyFrame = new CandyLandJFrame(); 
            //!! candyFrame.setSize(500, 600); 
            candyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            candyFrame.pack(); //!!
            candyFrame.setLocationRelativeTo(null);//!!
            candyFrame.setVisible(true); 
        }
    
        public CandyLandJFrame() {
            setTitle("Welcome to CandyLandy!"); 
    
            getContentPane().setLayout(
                    new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 
                                                                        
    
            ItemHandler handlerObject = new ItemHandler();
            radioButtonGroup = new ButtonGroup();
            Handler handler = new Handler();
            
            JPanel radioPanel = new JPanel(new GridLayout(0, 1));
            for (int i = 0; i < radioButtons.length; i++) {
                radioButtons[i] = new JRadioButton(RADIO_LABELS[i]);
                radioButtons[i].setActionCommand(RADIO_LABELS[i]);
                //radioButtons[i].addItemListener(handlerObject);
                radioPanel.add(radioButtons[i]);
                radioButtonGroup.add(radioButtons[i]);
            }
            c.add(radioPanel);
    
            poundLabel = new JLabel("How many pounds? ");
    
            poundField = new JTextField(2);  
            JPanel poundPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            poundPanel.add(poundLabel);
            poundPanel.add(poundField);
            c.add(poundPanel);
    
            JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 4, 0));        
            enterBtn = new JButton("Enter"); 
            enterBtn.addActionListener(handler);
            submitBtn = new JButton("Submit");
            submitBtn.addActionListener(handler);
            
            buttonPanel.add(enterBtn);
            buttonPanel.add(submitBtn);
            c.add(buttonPanel);
        } 
    
        private class ItemHandler implements ItemListener 
        {
            public void itemStateChanged(ItemEvent ie) 
                                                       
            {
                
            }
        }
    
        private class Handler implements ActionListener 
        {
            public void actionPerformed(ActionEvent ae) {
                
            }
        }
    }

Similar Threads

  1. setSize() not working?
    By AedonetLIRA in forum Java Applets
    Replies: 3
    Last Post: 12-02-2010, 05:17 AM
  2. When to use setSize()
    By Lil_Aziz1 in forum New To Java
    Replies: 7
    Last Post: 05-29-2010, 03:26 PM
  3. [Help] setSize work wrong ???
    By dawp in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 09:30 AM
  4. BoxLayout problems
    By paulb in forum New To Java
    Replies: 2
    Last Post: 11-04-2009, 10:04 PM
  5. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •