So, im making a program for calculating water catchment. And I have 2 textareas(JFrame) and I need to convert them to a double, so, basically I want to do this!
double a = textarea1;
double b = textarea2; // textarea1 and textarea2 are JTextArea's
double c = a * b * 0.0632
How to convert JTextArea to double or how to make a double with the same value as what the user put in the JTextArea?
I think you need this:-
double a = Double.parseDouble(textarea1.getText());
Related
I want to pass the JLabel to Integer, The following code does not work even with Integer.valueOf() and Integer.parse()
This are the following code I've Tried:
Test 1:
JLabel life = new JLabel("204");
int x = Integer.valueOf(life).intValue();
Test 2:
JLabel life = new JLabel("204");
int x = Integer.parseInt(life);
No. You can't magically convert a Label to Integer.
However you can get the string of that label and then convert.
JLabel life = new JLabel("204");
int x = Integer.parseInt(life.getText());
Note that, you'll be succeed when there is proper text. For ex
"203", "34343" works but not "A2342"
I am trying to use a button in order to add doubles each time and have used got this at the moment.
btnAnswer3 = new JButton("C");
btnAnswer3.setBackground(Color.YELLOW);
btnAnswer3.setHorizontalAlignment(SwingConstants.LEFT);
btnAnswer3.addActionListener(new ActionListener() {double scoreAdder, currentScore, ans;
scoreAdder = 30000.00;
currentScore = 0.0;
ans=scoreAdder+currentScore;
currentScoretxt.setText(Double.toString(ans)); //This is textfield in which I wish to display the doubles.
}
});
It already displays a number but once I want it to keep adding up each time the Jbutton is clicked. Please let me know how to do this using my code. Regards.
You need to declare this variables out of the ActionListener
double scoreAdder, currentScore, ans;
if not you are just overwriting the math operation and not acumulating the values...
move those declarations and place it as class variables...
I have the following code:
int[] matrix = new int[9][9];
(for int x = 0; x <= 8; x++){
(for int y = 0; y <= 8; x++){
JtextField matrix[x][y] = new JtextField(“Matrix" x + y)
int[] coords = new int[2];
coords[0] = x;
coords[1] = y;
matrix[x][y].putClientProperty("coords", coords);
matrix[x][y].setText(game[x][y]);
}
}
After the loops end, I need a way of finding out which textfield the user typed into (the user also hits enter). So:
1. How do I check if a JtextField has been edited without knowing which one it is or its name?
2. How do I check which "coords" the textfield is positioned at? I have an idea of how to do it, but for some reason I cannot code it.
I feel like the answer is staring me right in the face, but its late, I'm tired, and I'm getting flustered. Thanks!
The answer depends on what you want to achieve.
If you're only interested in the end result, you could use an ActionListener (for when the user hits Enter) and a FocusListener for when they don't and leave the field any way (assuming you want to know this)
When the respective event occurs, you can inspect the source of the event by using getSource. Now this returns Object, but you can use instanceof to determine if the object can be cast to a JTextField...
For example...
Object source = evt.getSource();
if (source instanceof JTextField) {
JTextField field = (JTextField)source;
int[] coords = field.getClientProperty("coords");
}
Take a look at How to write an Action Listener and How to write a Focus Listener for more details.
Depending on your needs, you could also take a look at Validating Input
I'm finishing up a GUI program that'll act as an online ordering menu for a restuarant, however I seem to be having two problems..
My calculate handler method seems to just freeze the entire java applet. It doesnt show any error or anything as well it just freezes. heres the event handler:
ArrayList<Double> yourOrderPrices = new ArrayList<Double>();
ArrayList<String>yourOrderName = new ArrayList<String>();
ArrayList<String> specifications = new ArrayList<String>();
Iterator<Double> it1 = yourOrderPrices.iterator();
private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {
int x = 0;
double temp;
while(it1.hasNext()){
temp = yourOrderPrices.get(x);
orderTotal += temp;
}
subTotalLabel.setText("Sub Total: " + orderTotal);
totalPriceLabel.setText("Tax Rate: " + orderTotal / TAX_RATE);
totalPriceLabel.setText("Total: " + (orderTotal / TAX_RATE) + orderTotal);
//Reset Variable
orderTotal = 0;
}
Basically what this is supposed to do is calculate the sub total by adding all of the prices in the yourOrderPrices ArrayList, its supposed to divide by the tax rate and display it, and its supposed to add the tax rate for a Total Price. The variables inside are representing prices for food and are doubles.
But everytime I press the button the entire program freezes.
Also, i'm trying to wrap the text in 2 textArea boxes, but everytime I try and call the method setLineWrap(true); it shows up on eclipse as not ablee to do that. Heres the two Text areas im trying to put it in:
detailTextArea.setEditable(false);
detailPanel.add(detailTextArea, java.awt.BorderLayout.CENTER);
and
orderTextArea.setEditable(false);
eastPanel.add(orderTextArea);
Use a for each instead.
double orderTotal = 0;
for(Double price : yourOrderPrices) {
orderTotal += price;
}
As for setLineWrap(true), exactly what does Eclipse say?
Solution:
I installed java and Eclipse and tried it out by myself. I created a JTextArea and set the lineWrap to true and it works without complaints. Have you checked that you imported javax.swing.JTextArea and not something else?
My code for reference:
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JTextArea area = new JTextArea();
area.setLineWrap(true);
}
}
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());