Jump to content

Set Skill Level inside Gui


Elixar

Recommended Posts

So I read explv's GUI tutorial however It didn't quite cover retrieving integers from JtextField and then setting that input into static Ints

 

  • I want this Public Int to be changed to whatever the user types into the JtextField 
  • I have all the panels and Jframe / Dialogues setup and the GUI is functioning, now Im just trying to get the input data.
	public static int chosenCookingLvl = Gui.thisLevelCook;	 			// This is in another class, This is where I need the users chosen cooking level to be placed


//  GUI CLASS

	public static int thisLevelCook;        

        JTextField cookLvlTxt = new JTextField(7);   						 // This is where you write what level you want
		  cookLvlTxt.addActionListener(cook -> {      						 // This is the action listener attached to the JtextField where you write what level you want.
		        int cookboi = Integer.parseInt(cookLvlTxt.getText().trim());   
		        thisLevelCook = cookboi;                                 			 // THIS IS WHAT I NEED HELP WITH 
              // How do I get this value 
		  });

Any help is appreciated. I have tried to make sense of the JtextField  Swing Documents and looked through multiple cancerous Youtube Videos that had horrendous dubstep music and horrendous talking volumes. :(I have a start button that is functioning, Maybe I need to set the value when the user press's start? not sure how though...

How are you guys making Scripts with a GUI  that let you choose Input for desired Skill levels?
:doge:

 

Edited by Elixar
Link to comment
Share on other sites

It's not enough just to copy/paste code, you actually need to read it and try to understand what it's doing.

Here:

 


	// ...
	
	textField.addActionListener(e -> {
	
		String text = ((JTextField) e.getSource()).getText()
		
		int number = -1;
		
		try {
		
			number = Integer.parseInt(text);
			
			someStaticNumber = number; // let's pretend someStaticNumber is a static int
			
		} catch (NumberFormatException e) {
		
			// err you typed something in, but it wasn't a number!
		}
	});
	
	
	// ...

When you hit [enter] after typing in a number, that'll kick-start an "action event" which will go on to kick-start a bunch of routines that deal with that event.

Link to comment
Share on other sites

SOLVED
Solution

 

Gui class{

	private JSpinner cookLvl;   				// Adding this was part of the solution
 	
	public Gui(){
	// I left out 99% of my actual GUI because obviously you need frames and panels.
	cookLvl = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));              						 // Adding this was part of the solution
      	JButton startButton = new JButton("Start");
		startButton.addActionListener(e -> startButtonActionPerformed(e));     					 // Adding this was part of the solution
			started = true;  
			close();
		});
}

	private void startButtonActionPerformed(ActionEvent e) {     									 // Adding this was part of the solution
		Task.chosenCookingLvl = (int) cookLvl.getValue();              										// Adding this was part of the solution
		this.close();
	}
}


I appreciate the PMS everyone, However I just did the less complex solution, I also forgot to 

Use Components for what they are intended for.

  • Jtextifled = Text
  • Jspinner = numbers with inbuilt restriction Parameters


Instead of using a Jtextfield and Parsing the Integer + setting up a filter to make sure it only accepts numbers in the text feed that is between 0-99

I just used a Jspinner instead and made sure to set proper variables
problem solved

Edited by Elixar
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...