Results 1 to 8 of 8
Thread: setSize on BoxLayout
- 12-14-2010, 04:42 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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);
- 12-14-2010, 05:12 PM #2
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 10
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(); } }
- 12-15-2010, 08:46 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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
Java Code:small space
-
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.
- 12-15-2010, 11:36 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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
- 12-15-2010, 11:48 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 25
- Rep Power
- 0
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-16-2010 at 12:17 AM.
-
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
-
setSize() not working?
By AedonetLIRA in forum Java AppletsReplies: 3Last Post: 12-02-2010, 06:17 AM -
When to use setSize()
By Lil_Aziz1 in forum New To JavaReplies: 7Last Post: 05-29-2010, 04:26 PM -
[Help] setSize work wrong ???
By dawp in forum New To JavaReplies: 7Last Post: 11-05-2009, 10:30 AM -
BoxLayout problems
By paulb in forum New To JavaReplies: 2Last Post: 11-04-2009, 11:04 PM -
BoxLayout Behaviour
By PetalumaBoy in forum AWT / SwingReplies: 4Last Post: 06-10-2009, 02:27 PM
Bookmarks