I am currently using the AutoComplete TextField functionality from ControlsFX to show suggestions when the user is typing. The amount of suggestions is large, and therefore the list doesn't fit on the page.
I would like to set the length of the list of strings to a maximum, but this is not yet possible in ControlsFX (as fas as I can conclude that). Therefore, I was thinking of a workaround, in which the list only shows up when the user has typed a string of 3 characters or more.
I have now set this action to execute when the TextField is clicked (where searchCustomer is my TextField):
#FXML
private void searchCustomer() {
//Get all customers from shop
String[][] customersOfShop = octocash.Main.databaseConnection.getData("some query",
Arrays.asList("some columname"));
//Convert 2D array to 1D array
int noOfRows = customersOfShop.length;
String[] customersForList = new String[noOfRows];
for(int k=0; k<noOfRows; k++) {
customersForList[k] = customersOfShop[k][0];
}
//Set values to AutoComplete TextField
TextFields.bindAutoCompletion(searchCustomer, customersForList);
}
How to do this in java/javaFX8?
One of approaches can be to observe text length:
IntegerBinding ib = Bindings.length(textField.textProperty());
ib.addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
if(newValue.intValue() >= 3) {
// trigger auto complete
}
});
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 am using Eclipse Window Builder, and in my program, I would like to ask the user to enter the highest degree of the polynomial and based on his answer, I would like my program to display n text boxes and n label for him to enter the coefficient of each x
Example:
Enter Highest Degree: 3
-- X^3
-- X^2
-- X^1
-- X^0
Anyone knows how this can be done?
If you know the number of boxes you need, simply pass that number and a parent Composite (with the Layout you need) to the method below:
private void addBoxes(Composite parent, int number)
{
for(int i = 0; i < number; i++)
{
Text text = new Text(parent, SWT.BORDER);
// Maybe add them to a List here so you can use them again later.
}
}
If you want to call this method more than once, remember to dispose() of the old Texts before you do so.
So I have this assignment for a Java class, I hit a bit of a road block. My code is working fine except on the last iteration of a loop, where peek() gives the wrong value. Every other iteration it gives the correct value, so I am lost as to why it gives the wrong value.
My assignment is to make a priority queue of boxes. We read float values from a text file, and then fill the boxes with the values. No box can have a total sum greater than 1.0.
The data from the text file is: 0.1, 0.6, 0.11, 0.765, 0.01, 0.42, 0.1492,0.667, 0.333, 0.111
Code:
Box Class:
public class box implements Comparable<box>
{
//instance variables
private ArrayList<Float> values;
private Float totalValue=0.0f;
public box ()
{
values = new ArrayList<Float>();
}
public box(Float val)
{
values = new ArrayList<Float>();
values.add(val);
}
//add new values to the box
public void addVal(Float newVal)
{
values.add(newVal);
}
/*
* Adds up all the values in the box, returns sum as Float.
*/
public Float findSum()
{
totalValue=0.0f;
for(Float val : values)
{
totalValue=totalValue+val;
}
return totalValue;
}
}
Code for comparator
import java.util.Comparator;
public class boxComparator implements Comparator<box>
{
//compare method for boxes
public int compare(box a, box b)
{
if (a.findSum()>b.findSum())
return 10;
else if (a.findSum()<b.findSum())
return -10;
else
return 0;
}
}
Code for PackingBoxes (contains Main):
import java.util.;
import java.io.;
public class PackBoxes
{
public static void main(String[] args)
{
...
boxComparator compSum = new boxComparator();
...
*Strategy A: As each new item is processed, it is placed in the
* fullest box that can hold it.
*/
System.out.println("=========");
System.out.println("Packing via most-filled strategy:");
System.out.println();
//Create new priorityqueue of boxes for strategy A
PriorityQueue<box> boxesA = new PriorityQueue<box>(11,compSum);
//add first box to queue
boxesA.add(new box());
//for each loop, goes through arrayList L
for(Float x : L)
{
//if element + sum of current box is less than 1 put element
//into the current box
if(x + boxesA.peek().findSum() <1)
{
boxesA.peek().addVal(x);
}
//if element + sum of current box is greater than 1, then make
// a new box
else
{
boxesA.add(new box(x));
}
}
System.out.println(boxesA.size()+" boxes were used.");
//print queue
while(!boxesA.isEmpty())
{
System.out.println(boxesA.poll().findSum());
}
}
After much debugging, I've found that the program does the following:
Read 0.1, 0.6, 0.11 put in box 1. Read .765, does't fit in box 1, create box 2. Read .01 put in box 2 (which is smallest total sum). Read 0.42, doesn't fit in box 2, create box 3. Put .1492 in box 3. Read .667, create box 4. Read .333 put in box 3 (box 3 has smallest current sum). read .111, doesn't fit in box 3, create box 5
The bolded part is the problem. It should read .111 and try and put it into box 4, with .667. But when I use peek it brings up the box with, 0.42, 0.1492, 0.33, for a total sum of .9022. Since 0.667 is smaller than 0.9022 I don't understand why peek() isn't giving me box 4. Any help here would be greatly appreciated.
Edit: Tried to delete code that wasn't part of the problem.
The problem as far as I can tell is that you do not re-order the boxes when you put .333 in box 3. When you do that, box 3 is no longer the least full box - it should no longer be on top of the queue. Box 4 should be moved to the top of the queue. But since box 3 stays on top of the queue, next time you try to insert an item, it looks at box 3 (the top item of the queue). When it fails to insert into box 3, it makes a new box.
To solve this, all you need to do is place a simple sort function for your queue after you add a new value or a new box. That way the box that is the least full will always be on top.
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?
I basically checked out a book from the Library and started learning Java. I'm trying to code a little score calculator for my golf league and this site has been a lof of help! So thanks for even being here!
Now to the question:
I have a 9 labels, created with NetBeans GUI, with names like jLabel_Hole1, jLabel_Hole2, ...
If a user selects the radio option to play the front nine those labels have number 1 - 9 and if they change it to the "Back Nine" then they should display 10 - 18. I can manually set each label to the new value on a selection change but I wanted to know if there was a more elegant way and if so if one of you could be kind enough to explain how it works.
Here is the code that I want to try and truncate:
TGL.jLbl_Hole1.setText("10");
TGL.jLbl_Hole2.setText("11");
TGL.jLbl_Hole3.setText("12");
TGL.jLbl_Hole4.setText("13");
TGL.jLbl_Hole5.setText("14");
TGL.jLbl_Hole6.setText("15");
TGL.jLbl_Hole7.setText("16");
TGL.jLbl_Hole8.setText("17");
TGL.jLbl_Hole9.setText("18");
I've read some things about String being immutable and maybe it's just a limitation but I would think there has to be way and I just can't imagine it.
Thanks.
Basically, rather then creating a individual label for each hole, you should create an array of labels, where each element in the array represents a individual hole.
So instead of...
TGL.jLbl_Hole1.setText("10");
TGL.jLbl_Hole2.setText("11");
TGL.jLbl_Hole3.setText("12");
TGL.jLbl_Hole4.setText("13");
TGL.jLbl_Hole5.setText("14");
TGL.jLbl_Hole6.setText("15");
TGL.jLbl_Hole7.setText("16");
TGL.jLbl_Hole8.setText("17");
TGL.jLbl_Hole9.setText("18");
You would have...
for (JLabel label : TGL.holeLables) {
lable.setText(...);
}
A better solution would be to hide the labels from the developer and simply provide a setter...
TGL.setHoleText(hole, text); // hole is int and text is String
Internally to your TGL class, you have two choices...
If you've used the form editor in Netbeans, you're going to have to place the components that Netbeans creates into your own array...
private JLabel[] holes;
//...//
// Some where after initComponents is called...
holes = new JLabel[9];
holes[0] = jLbl_Hole1;
// There other 8 holes...
Then you would simply provide a setter and getter methods that can update or return the value...
public void setHole(int hole, String text) {
if (hole >= 0 && hole < holes.length) {
holes[hole].setText(text);
}
}
public String getHole() {
String text = null;
if (hole >= 0 && hole < holes.length) {
text = holes[hole].getText();
}
return text;
}
Take a closer look at the Arrays tutorial for more details...
I've never found a Java GUI-generator to provide code that's any good. I may be wrong--there may be a good one, but I always prefer to position and name them myself. So,
/**
* The JLabels for the holes on the golf course.
* <p>
* holeLabels[0][i] are for the outward holes, 1-9.
* holeLabels[1][i] are for the inward holes, 10-18.
*/
private JLabel[][] holeLabels;
/**
* The starts of the outward and inward ranges of holes.
*/
private static final int[] holeStart = {1, 10};
// Later
holeLabels = new JLabel[2][9];
for(final int i = 0; i < holeLabels.length; i++) {
for (final int j = 0; j < holeLabels[i].length; j++) {
holeLabel[i][j] = new JLabel();
holeLabel[i][j].setText(Integer.toString(holeStart[i] + j));
}
}
Interestingly, holeLabels.length is 2. holeLabels is an array of 2 arrays of 9 ints. i goes from 0 to 1, and j goes from 0 to 8, so the text computation works. The reason I did things this way is so you can easily place the labels in an appropriate GridLayout later.