How to add multiple selected radio buttons to text field [duplicate] - java

How to convert JTextField to String and String to JTextField in Java?

how to convert JTextField to string
and string to JTextField in java
If you mean how to get and set String from jTextField then you can use following methods:
String str = jTextField.getText() // get string from jtextfield
and
jTextField.setText(str) // set string to jtextfield
//or
new JTextField(str) // set string to jtextfield
You should check JavaDoc for JTextField

// to string
String text = textField.getText();
// to JTextField
textField.setText(text);
You can also create a new text field: new JTextField(text)
Note that this is not conversion. You have two objects, where one has a property of the type of the other one, and you just set/get it.
Reference: javadocs of JTextField

The JTextField offers a getText() and a setText() method - those are for getting and setting the content of the text field.

JTextField allows us to getText() and setText() these are used to get and set the contents of the text field, for example.
text = texfield.getText();
hope this helps

Related

Refresh data in a JLabel

I have a program which calculates an integer and then uses the value within a JLabel. On intital creation everything is fine with the initialized value, but when I change the value of the int within the label I can't seem to find a way to update the JLabel. The relevant code is as follows:
JLabel carbLbl;
int totCarbs = 0;
public Main() {
carbLbl = new JLabel("Total Carbs: " + totCarbs);
carbLbl.setFont(new Font("KidSans", Font.PLAIN, 38));
carbLbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
void addFoodToTable() {
String[] s = new String[3];
s = (String[]) foodData.get(foodChoice.getSelectedIndex());
foodList.addRow(s);
totCarbs += Integer.parseInt(s[2]);
carbLbl.repaint();
}
}
There's obviously much more code, but it's too long to include the entire script. Is there a way I can have the label update whenever I invoke the addFoodToTable() method?
The JLabel is not "bound" to your integer variable. When you change the integer you need to update the JLabel using carbLbl.setText(String.valueOf(totCarbs))
Is there not a way to simply update the JLabel using the initial constructor parameters?
carbLbl = new JLabel("Total Carbs: " + totCarbs);
What parameters? There is only a single parameter, the String to be displayed.
The compiler concatenates the hard coded String with the value of your "totCarbs" variable to create a single String.
The compiler will essentially treat the above code like:
String text = "Total Carbs" + totCarbs;
carbLbl = new JLabel( text );
The JLabel has no idea how the String was created (ie. that a variable was used to build the string).
I understand the concept of concatenation, but I just feel like that's a workaround
It is not a work around. The API of the setText(...) method states you provide a single String. So, if you want to update the label you need to provide the entire String.

How to set an array in Jtextfield using netbeans

I am creating a small application . In this application I have 5 words. I have one JtextField and JButton.The name of JtextField is set as name1 and JButton name set as next.
I want to set a default word in name1 from 1st word of string array ( from this code I am giving one example is need to see in JtextField is as "me") when application run I want to see the word from String array before the the next button click. how I can set the data to name1?
code is as follows:
String s[]={"me","and","my","friends","are"};
int i=0;
private void nextActionPerformed(java.awt.event.ActionEvent evt) {
if(i>=s.length)
i=0;
name1.setText(s[i]);
i++;
}
Call the method after you created your fields with null as argument as you don't use the argument.
nextActionPerformed(null);
This will simulate that the button is pressed when the program starts up.
Assuming you have a button which has action Listener attached. If not then it should be something like below:
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
nextActionPerformed(e);
// Or move your code here from nextActionPerformed(e)
}
});
//And you can also initialize your text filed with first value of array:
textField.setText(s[0]);

How to have a unique identifier of a Jlabel?

I am new to java swing and I am creatin a Jlabel as follows :
JLabel Lport = new JLabel ("Port: ");
final JTextField Tport = new JTextField ("1883", 10);
what i want to do is to get the name of the label as a string because i want to use it in a switch-case, so i need to get the label name or a unique identifier of that label, some thing like an ID as it exists in Android, i tried the method ",getAction.toString", ".getName" but none of them displayed the name of the labe, which is according to the code posted is "Port: ". please see my attempts below:
if ( (isIPReady(Tip)) && (isPortReady(Tport)) ) {
Thread mqttThread = new Thread(MQTTRunnable, MQTT_THREAD);
mqttThread.start();
System.out.println("Action: " + Tport.get); //here i do not know which method to use
setViewEnableState(Bconnect, true);
}
The short answer is to use JLabel#getText which will return the text which is displayed by the JLabel.
An alternative could be to store your own key-value pair into the different JComponent instances. Each JComponent allows to put and retrieve client properties. A copy-paste from the class javadoc:
Support for component-specific properties. With the
putClientProperty(java.lang.Object, java.lang.Object) and
getClientProperty(java.lang.Object) methods, you can associate
name-object pairs with any object that descends from JComponent.
This would allow you to write:
private static final String ID_KEY = "MyUniqueIDKey";
JLabel label = new JLabel( "Whatever" );
label.putClientProperty( ID_KEY, "labelName" );
and then later on
String labelName = (String) label.getClientProperty( ID_KEY );
Note that this works with any JComponent, including JLabel and JTextField instances like the ones you are using in your code.
JLabel's name is different than the text it displays. To get the text from a JLabel, use getText().
You mention you want the name of the label but in your example you're calling a get on your textfield.
This applies to both a textfield and a label anyway.
That constructor sets the initial text that will display in the text field (or label).
If you want to set a name, you must first set it using setName() then use getName().

Making a JButton that switches text from 2 JTextFields on click with mouse

I have made a window using JFrame and on my JPanel I have added 3 components: 2 JTextFields ("field1" and "field2") and inbetween them a JButton ("switch"). My goal is to switch the value of field1 to field2 and vice versa when the JButton is clicked. I thought this ActionListener which I have added to my JButton would achieve my goal:
public void actionPerformed(ActionEvent e) {
field2.setText(field1.getText());
field1.setText(field2.getText());
}
However, it changes the value of field2 into the value of field1 but not the other way around.
Any help would be appreciated.
You need a temporarily variable todo. If you do not use on, you set the text from field1 to field2 and then you get the wrong value.
public void actionPerformed(ActionEvent e) {
String tmp= field2.getText()
field2.setText(field1.getText());
field1.setText(tmp);
}
You need a temporary variable to store it in.
Right now field2 is first set to whatever is in field1, and then you set field1 to what you just set field2 to. You must save the content temporarily before you overwrite it:
String temp = field2.getText();
field2.setText(field1.getText());
field1.setText(temp);

Getting user input by JTextField in Java?

I am trying to get user inputs from a textbox so I can take the user input to calculate the displacement for a ball.
I tried this
double initialvelocity = new JTextBox("enter your initial velocity");
so I have a variable with data that I can use. I just cant get it to work and this has been a obstacle to my first proper java project.
Could anyone suggest how I can get a variable to store data using JTextField or is there other alternatives?
A JTextField should be stored like this:
JTextField v0TextField = new JTextField ("initial velocity");
And when you want to access the current string in the text box:
String strV0TextBox = v0TextField.getText();
Then you'll want to parse this into a double:
double initialvelocity = Double.parseDouble(strV0TextField);
If you just want to get input from the user the easiest way is to use a dialog:
double value = Double.parseDouble(
JOptionPane.showInputDialog("please enter value"));
The correct way to use JTextField is
JTextField box = new JTextField(" Enter Initial Velocity");
String velocity_str = box.getText();
Than parse into a double
double initialvelocity = Double.parseDouble(velocity_str);
See this tutorial for using a jTextField here.
You want to instantiate the box like:
JTextField myTextField = new JTextField("enter your initial velocity");
//Probably should do some kind of validation on the input if you only want numbers
//get The Text and parse it as a double
double initVelocity = Double.parseDouble(myTextField.getText());

Categories

Resources