6 Replies - 812 Views - Last Post: 02 September 2013 - 10:30 AM Rate Topic: -----

#1 ftlog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-July 13

Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 07:34 AM

Hi,

I am attempting to make a panel with a title that simply says 'Insert number', a field where this could be entered and then a submit button. As simple as this sounds, I have been struggling because I have been finding a lot of conflicting information online and have not been able to get it working, so I was hoping that someone could please give me some help and point me in the right direction? I have been finding the process of making buttons and titles okay, so it's mainly just this number field box that is causing me the issues.

Thank you in advance for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Create a number input field in a Java GUI panel?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 07:36 AM

What have you tried?
Was This Post Helpful? 1
  • +
  • -

#3 ftlog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-July 13

Re: Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 07:47 AM

View PostGregBrannon, on 02 September 2013 - 07:36 AM, said:


Apologies if I am not coming across very well - I am very new to Java.

I've been searching online and for example, with the submit button, I just needed to do something like:
JButton submit = new JButton("Submit");
submit.setLocation (40, 40);
Panel.add(submit)
return titlePanel;

so I tried replacing JButton with JTextField, but nothing comes up when I press run on Eclipse. I also saw some examples online about how you can make a text field which only allows numbers to be inputted, but I just cannot work out how to manipulate these to just be a simple text box and they look way too complicated for what I'm trying to do! The other issue I am finding is that a lot of my searches are coming up with how to make fields, but you have to input the numbers into the code and not into the created panel.

I think what I'm mainly looking for is just to be pointed in the right direction as I haven't been able to try many things as I am not sure what is right and what isn't right or how to extract the information that I desire. I apologise for my basic knowledge - I just wanted to try a few things and this was something on my list to learn about, but it's proving tricky!
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 07:57 AM

The Java Swing Tutorials are an excellent place to start, just ignore their advice to use Netbeans and the GUI Builder. There are also some basic Swing tutorials here on DIC. I recommend you start at the beginning and work your way through rather than jumping into the middle to do what you've described.

If you have code that you've tried and are confused about, then post it and we can work through your confusion.

If you're asking for a complete example of a JButton on a JPanel that does something, then that's what you should say, though we tend to point to tutorials to provide working examples of code. We don't know what you don't know, apparently quite a bit in this case, so giving you working code that does as you ask may not be that useful to you.
Was This Post Helpful? 0
  • +
  • -

#5 ChrisNt  Icon User is offline

  • cute bug

Reputation: 218
  • View blog
  • Posts: 661
  • Joined: 31-July 13

Re: Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 08:13 AM

If you want to accept only numbers you have to use a JFormattedTextField.
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 09:05 AM

You don't want to limit input on your text fields. If you do, some users may think your application is broken, like if you have a field that only accepts numeric input but they're an average computer user, so they're still typing in their last name, getting frustrated as nothing is happening. You want to use an InputVerifier instead. The javadocs have an easy example as to how to use it, and you can still use it with a JFormattedTextField. i think it's probably over your head, however...

This post has been edited by farrell2k: 02 September 2013 - 09:06 AM

Was This Post Helpful? 1
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Create a number input field in a Java GUI panel?

Posted 02 September 2013 - 10:30 AM

I would suggest using a JSpinner
You can set it up for only allowing numeric input. Just like my example below
It does not give a notice to the user when he does a bad input, but the value of the model will always be valid :)
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;


public class App extends JFrame {

	public static void main(String[] args) {
		new App();
	}
	
	public App() {
		setLayout(new FlowLayout());
		
		SpinnerModel model = new SpinnerNumberModel(5, 0, 10, 1); 
		JSpinner spinner = new JSpinner(model);
		spinner.setEditor(new JSpinner.NumberEditor(spinner));
		
		add(spinner);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400, 400);
		setVisible(true);
	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1