I'm working on a program in Javafx, which allows the user to input values in TextFields. Additionaly, there is an empty Choicebox, where a value beginning from "1" is added. Everytime he presses the "Submit" button, the TextFields also get cleared and he can input another set of values in said TextFields.
Let's say he inputs five sets of values and presses "Submit", I'd like to have those values stored in an array, so when he chooses a number in the ChoiceBox, the corresponding values are shown again in the TextFields.
For this I need to store them in an array. Below is the eventhandler for the buttonclick
private void next(javafx.event.ActionEvent event) {
double c_stueck = Double.parseDouble(stueck.getText());
double c_pr_pro_einheit = Double.parseDouble(pr_pro_eh.getText());
double c_betrag = Double.parseDouble(betrag.getText());
String c_bezeichnung=bezeichnung.getText().toString();
c_betrag += c_stueck * c_pr_pro_einheit;
String c_betragval = String.valueOf(c_betrag);
betrag.setText(c_betragval);
stueck.setText("");
pr_pro_eh.setText("");
bezeichnung.setText("");
for(int j=0; j<=49;j++)
{
arr_stueck[j]= c_stueck;
arr_pr_pro_eh[j]= c_pr_pro_einheit;
arr_bezeichnung[j]= c_bezeichnung;
position.getItems().add(j);
}
}
The problem lies herein, that everytime the eventhandler is fired, the array is reset. Is there a way to save the increment, so the next time the button is pressed, the for loop continues from the last increment?
Related
My code is very long so I will only be adding snippets that are relevant.
Okay so I've been trying to increment a label by one using the following code:
btnComplete.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//if the list has a minimum of 1 item
if (currentCartTxt.getItems().size() > 0) {
int sales=0;
sales++;
String x = Integer.toString(sales);
numberOfSalesTxt.setText(x);
}
}});
However it only changes my textfield to 1 and never increases it. Any help would be greatly appreciated.
the currentCartTxt is a listView and the numberOfSalesTxt is a textfield.
Basically to explain my app, I have a list of items that I am adding to a textfield (currentCartTxt) and I need to press the complete button whenever but there must be at least 1 item in the textfield. And every time the button is pressed the textfield(numberOfSalesTxt) increases by 1.
Thanks!
You have to:
read current value (from Label/View/TextView...)
increment it (just add 1)
set new value to view
if (currentCartTxt.getItems().size() > 0) {
// get current value
String text = numberOfSalesTxt.getText();
// convert it from "String" to "int"
int sales = Integer.parseInt(text);
// increment it
sales++;
// Convert from "int" to "String"
String x = Integer.toString(sales);
// Set new value
numberOfSalesTxt.setText(x);
}
I'm encountering an odd bug while updating the contents of a JTable. I've got a two dimensional array which is storing arrays of values to be displayed by the table (I'm only displaying one row of data at a time). When the user clicks a button, the next set of values (along with the next set of column headers) is displayed, and when the user clicks another button, it displays the previous set. The problem is that the first time I call getModel().setValueAt, on the first array in the two dimensional array, it is removed.
Here's the code that updates the JTable:
for(int i = 0; i < formattedData[displayedDataSet].length - 1; i++) {
TableColumn col = displayTable.getTableHeader().getColumnModel().getColumn(i);
col.setHeaderValue(WeatherData.WEATHER_MAP_KEYS[displayedDataSet * 5 + i]);
displayTable.getTableHeader().resizeAndRepaint();
displayTable.getModel().setValueAt(formattedData[displayedDataSet][i], 0, i);
}
for(String s: formattedData[0]) {
System.out.println(s);
}
I expect the output to be the first array in formattedData, but it is the second. However, commenting out the line with displayTable.getModel().setValueAt causes the correct result to be output. Why is this happening, and how can I update the JTable while preserving my array?
I'm trying to create a menu for toll data entry that runs in a loop until the user requests to exit. The menu will ask the user to enter data which is stored in an array and keeps adding to the array until the user exits from the menu.
The menu should look as follows:
Toll Data Entry Menu
My menu works fine until asking the user to enter the trip date. The menu will continue to ask for each trip date up until the end of the length of the array (30 - arbitrary length).
I however want the menu to ask for the trip date, store the value entered by the user, then move onto entering the entry point, store value, etc and then loop back to the selection for the user to enter the above details again for a separate trip until they choose to exit. (I need to print these values later on) I'm not sure how to store the values from the user separately without prompting the user to enter all (30) values at the one time.
Should I be using an array or is there another way to store multiple values for a looped menu?
Hope my explanation is clear.
To fetch the complete set of details one at a time, you just need a single for loop as:
String[] datesA = new String [30];
int[] entryP = new int [30];
int[] exitP = new int [30];
for (int i = 0; i < 30; i++) {
System.out.println("Enter trip date: ");
datesA[i] = inputA.nextLine();
// User to enter entry point
System.out.println("Enter entry point: ");
entryP[j] = inputA.nextInt();
// User to enter exit point
System.out.println("Enter exit point: ");
exitP[k] = inputA.nextInt();
}
To improve the implementation though think over the lines of creating an object that includes all the details in one as :
class TollDataEntry {
String entryDate;
int entryPoint;
int exitPoint;
.... getter, setter etc.
}
and then use an array or Collection of this object to store the details of each TollDataEntry with those three values as a single entity.
I'm trying to solve a strange problem with my program. This program creates a series of GUI's and JTables that give a user the ability to generate an XML file. One of these tables is for creating the "statements". I won't get into detail as far as that except to say that the data is stored in multiple 2D arrays which are in turn stored in a hash map.
Here is what happens. When a user enters the Statement screen a JTable is generated using the contents from the 2D array. This data populates the cell's which the user is able to modify. One of these cells (and the most important) is the amount. The amounts they set for the rows much match another amount from another class.
At the bottom of the table is a "finished" button. When the user clicks this button the logic will check to see if the money amounts match. If they do then the program will update the 2D array with any changed values and dispose of the JTable.
My problem is that once a user updates a cell and clicks "finished" the last updates made do not work. Essentially the user must first click somewhere else in the table and THEN hit finished. I would like this action to happen automatically so that when the user clicks "finished" cell editing is stopped. Here is the code for the finished button:
finishedButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//Creates another table model for the finished button logic.
DefaultTableModel dm = (DefaultTableModel)StatementGUI.tbl.getModel();
//Gets the total number of table rows.
int rows = dm.getRowCount();
//Creates a variable to store the statement transaction total.
double statementTransactionTotal=0;
//For each row in the table.
for(int i = 0; i < dm.getRowCount(); i++){
//Gets the total of all transactions in the table.
String currentTotal = tbl.getValueAt(i, 3).toString();
Double currentTotalDouble = Double.parseDouble(currentTotal);
statementTransactionTotal=statementTransactionTotal+currentTotalDouble;
}
//Creates a decimal format and applies the statement total.
DecimalFormat df = new DecimalFormat("0.00");
String currentTotalDF = df.format(statementTransactionTotal);
//Stops editing on the table so that the data can be used.
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
//If the statement total matches the transaction total..
if(currentTotalDF.matches(ClearedMainGUI.currentTransactionAmount)){
//For each row in the table..
for(int i = 0; i < dm.getRowCount(); i++){
//Will replace the hash/array value with the table value.
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][0]=tbl.getValueAt(i, 0).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][1]=tbl.getValueAt(i, 1).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][2]=tbl.getValueAt(i, 2).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][3]=tbl.getValueAt(i, 3).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][4]=tbl.getValueAt(i, 4).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][5]=tbl.getValueAt(i, 5).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][6]=tbl.getValueAt(i, 6).toString();
ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][7]=tbl.getValueAt(i, 7).toString();
}
//For each row in the table..
for(int i = rows - 1; i >=0; i--){
//Removes the current row so the table will be empty next time.
dm.removeRow(i);
}
//Removes the frame and goes back to the previous GUI.
frame.dispose();
//If the statement total and transaction total do not match..
}else{
JOptionPane.showMessageDialog(null, "The statement total entered: $"+statementTransactionTotal+" " +
"does not match the transaction total of: $"+ClearedMainGUI.currentTransactionAmount);
}
}
});
I think my problem is with this line:
if(null != tbl.getCellEditor()){
tbl.getCellEditor().stopCellEditing();
}
This only seems to work once the user has clicked another area of the table after editing a cell.
I appreciate the help!
My problem is that once a user updates a cell and clicks "finished" the last updates made do not work.
Check out Table Stop Editing for two approaches:
You can either:
Add code to the ActionListener:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
or set a property on the table:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Edit:
When the user clicks finished all the cell data will be saved to a 2D array.
Why? All the data is already stored in the TableModel.
In any case you need to stop editing BEFORE you attempt to copy data from the TableModel to the Array.
For my current project I need to create a custom text box that gets each key pressed and adds it to a string. I'm constantly updating the method that retrieves the key a user is pressing and then I add it like so:
public void addCharacter(String c) {
String before = text;
String after = before;
if (!before.endsWith(c)) {
after = text + c;
} else {
//What can I do here to check if the key
//was released and then pressed again, so that
//it only adds the character the number of times the user presses the key.
}
text = after;
}
My problem is that if I type a key it adds tons of them because of the fact that it is constantly updating, which is why I had to check if it's the same letter as before, and not add it.
EDIT:
Example of how we add the key:
if (key.a) {
addCharacter("a");
return;
}
I think you can get the job done by using a KeyListener. This is basically a listener which sends events each time a key is typed, pressed and released. In a nutshell, I would listen to a keyPressed event and then I would not type that letter until I receive a keyReleased event.