I'm new to Java and JavaFX I have php,javascript,HTML experience... I'm trying to create a GUI for doing math and showing the results using javaFX text objects. I have a constructor method to create each text object based on its input. Then I have another constructor method which would call the previous one several times creating multiple text objects, but I can't get this to work because it won't let me define the new text objects based on the methods input ex:
public function(variable)
{
Text variable = new Text();
}
The problem is that each time the program is run there would be a different amount of text objects on screen, so I'm not sure how to accomplish the naming convention when I won't know the total # of objects beforehand.
Also how would I add them all to the scene at the end. Would the constructor method add each to a group as they are created and then the whole group would get added with the get children method?
A more general idea of what I want to create:
The program would start with 10 text items on the screen. The user could click on two objects and choose to add/subtract/multiply them together, and then another text label would be created with the answer after processing the event. The new one could then be used to create another answer, and another and so on...
Maybe I'm approaching this totally wrong, let me know how you would approach it.
-back to my initial question, I could name the starting 10 statically if I wanted, but I'll need to know how to do it dynamically to add more I think... ...or maybe I'm waaay off...
Thanks in advance,
Brad
You want to use some sort of simple collection, like an Array or List. You may want to read through the Collections tutorials for Java. For your program, the simplest approach would probably be an array of Text objects:
Text[] textObjects = new Text[10];//Replace 10 with however many you will need
for(int i = 0; i < 10; i++) {
textObjects[i] = new Text();
}
Related
I am currently working through the "Providing a Custom Renderer" example on this page. And now I wanted to create more than just one of these boxes. I did this by creating six renderer classes, one for each box.
And now for my question. Is it possible to just have one renderer class for all six boxes? For that purpose I tried to parse two variables to the constructor of the CustomBoxRenderer, like this.
public ComboBoxRenderer(ImageIcon[] currentImage, String[] currentString)
But due to how the programm seems to work, the currentImage array is null until a certain point, so I get a exception.
But let's assume this would work how I expected it to work, I still would have to create six seperate instances of the renderer for each box, which I'd like to avoid aswell.
I hope this is enough information, I could also provide my full code, but I think that'd be too much for this page here, if not, let me know.
If i'm reading correctly you could create a class that extends combobox and just adjust it so that it automatically uses your custom renderer, then all you have to is create a normal instance of your custom combobox and use it as normal except it will use your renderer without any hassle.
e.g. in your constructor you would just have this line
this.setRenderer(new ComboBoxRenderer(currentImage, currentString));
Im unsure why you think you would need to create six instances as the renderer deals with each box.
Hope this helps.
Basically, what i wanna do:
A listView fed by two ArrayLists at once, and each list put in a section
Two ListViews that appear to the user like one, so if you scroll to the end of the first ListView you can smoothly scroll down further down the secound listView.
Yet i lag of knowledge to do so.
Following are some details on the problem i need to solve and why i am seeking a soloution like stated above.
I am trying to build a rather complex listview in android. Its data is stored in one ArrayList at the moment. Each row's data is provided by a custom object. I sorted the entries by date,
which works fine. I now need to
Split that table into two sections based on wheather a secound date provided by my custom object lies in the past or not (so basically based on a boolean)
Add a seperator in custom design between those two sections
If any of my two sections is empty, i need to ad the coresponding placeholder showing a message
This begs for a soloution involving two lists, it would be way easier to sort the entries and to handle design.
In case you ask what i tried: I already worked around it by mapping the relevant days way more into the future so they are on top of the list
Collections.sort(newsList, new Comparator<News>() {
#Override
public int compare(News p1, News p2) {
DateTime startDate = new DateTime(p2.getValidUntil());
DateTime endDate = new DateTime(p1.getValidUntil());
if(Util.isEntryRelevant(p1)){
endDate = endDate.plusDays(1000);
}
if(Util.isEntryRelevant(p2)){
startDate.plusDays(1000);
}
Minutes diff = Minutes.minutesBetween(endDate, startDate);
diff.getMinutes();
return diff.getMinutes();
}
});
Which is probably the dodgiest piece of code ive written so far. A few more workarounds come to my mind, but they all seem to get messy and unclean.
Essentially, you have to implement a custom BaseAdapter that knows to iterate over your two data sources. You could save yourself some time by using something like this to which you can add several regular adapters.
I need to build a View which has a 4 layer nested Multi-Accordion with a lot of checkboxes inside them. All together there might be around 30-40 Checkboxes all through the Accordions.
The next step will be, that i have some sort of logic behind all this. Depending on the selection combination of the checkboxes I will change a text label accordingly.
My thought process was: I put up all these checkboxes and give them a numeric fx:id representing there position in the nested accordion graph. Something like "1_1" or "2_4_1".
After that, I build one ChangeListener calling a Method on Selection of a Checkbox. I can look up the Id of the checked box, look it up in my data (to see which Text belongs to it and if any rules interfer with other boxes) and handle the logic accordingly while putting the id and its text in a Map or List to keep it for later and to keep track of the checked boxes.
Now I came to know, that getting the fx:id isnt something JavaFX wants me to do. I cant deliver a custom id in custom property inside the FXML either (couldnt find anything regarding this).
I am now pretty much at the end of my knowledge (I did just start with JavaFX and have some basic Java knowledge) and it seems to me, that I tackle this topic from the wrong side.
My question is now: What would be a best practice to handle dozens of checkboxes and trigger logic in the code according to the box that was checked without writing a ChangeListener for every single Check Box leaving me with some (imo) ugly code all the way.
EDIT: I forgot to mention: I did achieve some sort of functional solution by writing a custom CheckboxChangeListener with a reference to the Element the addListener method was called on and using "getId()" on this reference. I came to know though, that this method references the css:id of the fxml element and not its fx:id and I am not quite sure if this is a proper way to go
You should look into databinding with javafx. For example:
CheckBox cb1 = new CheckBox("1");
CheckBox cb2 = new CheckBox("2");
BooleanProperty isCb1Selected = cb1.selectedProperty();
BooleanProperty isCb2Selected = cb2.selectedProperty();
Textfield foo = new TextField().visibleProperty().bind(isCb1Selected.and(isCb2Selected));
This would hide the textfield foo if atleast one of the checkboxes isn't selected.
You can find other examples here and here an oracle tutorial
I have an ArrayList<ArrayList<Object>> Suppliers. Each ArrayList<Object> has 2 elements, an int (an id and the id may not be sequential with each other.) and a string (a name).
Now in a JComboBox, I create a DefaultComboBoxModel, containing the main ArrayList 'Suppliers'.
jcbSuppliers.setModel (new DefaultComboBoxModel (suppliersdata.Suppliers.toArray ()));
The JComboBox shows me each element such as [1, Local Supplier], [2, External Supplier], [4, Other Supplier].
Capture 1
In NetBeans debug, i put a Watch on jcbSuppliers. The variables window shows me that each element of jcbSuppliers is an ArrayList with 2 Objects.
Capture 2
Now my question is:
How I can get these 2 items or the array of these 2 objects, but of the selected object or item?
I would appreciate the help. Thank you very much.
First I do have to give kudos to Flextra and his comment as Spring can help you decouple your code, making it much easier to enhance and debug.
Next I suggest that you re-think your current model set up. Rather than begin with nested ArrayLists, I suggest that you create a custom class to hold the two pieces of data each supplier will need.
Then create a non-nested ArrayList of this custom class.
Then you can tell the JComboBox how to display an object of your either by giving it a toString() method that displays the information as you'd like it,
or give the JComboBox a custom cell renderer that displays the information as you'd like it.
Then when the combo box is selected, if you get the selectedItem, it will be an object of your class, and it will be easy to extract the two pertinent pieces of information.
Okay, thus may seen kind of odd, but I wanted to get some suggestions from everyone here. I am a beginning Java developer (after 2 years of ASP.NET web development) and I have recently began working on my first Java project - a calculator. I realize that their are tons of calculators out there, but I thought it would be a good beginner project.
Anyway, here is what I need help with. Currently, I am using a Scrolling JTextArea for display (instead of a simple JTextField) that is approximately 5 rows tall. I want the user to be able to scroll through the list to see previous entries and such. The format of the box will be equation on one line and the program will generate the answer on the next and so on.
My real question is, how is the best way to implement this? My fist idea was to read through the JTextArea when equals is pressed, down to the last line and try to search that line for the operator (+, -, etc.) and the operands. Is this the best way to go about this? Although, this would work would work, I think it could get cumbersome and sounds very inefficient. I am open to any suggestions, even possibly replacing the JTextArea is some other component would work better.
Thanks!
There's no need to read through the JTextArea contents - use JTextArea.append() to add to the end. Here are some examples of JTextArea content manipulation:
JTextArea ta = new JTextArea("Initial Text");
// Insert some text at the beginning
int pos = 0;
ta.insert("some text", pos);
// Insert some text after the 5th character
pos = 5;
ta.insert("some text", pos);
// Append some text
ta.append("some text");
// Replace the first 3 characters with some text
int start = 0;
int end = 3;
ta.replaceRange("new text", start, end);
// Delete the first 5 characters
start = 0;
end = 5;
ta.replaceRange(null, start, end);
If you are open to different interfaces, you might want to try something like a JTextField at the top of your view, from which you can receive as input your 'new' inputted equation, and then below it with the same width a JList that would scroll to have all of the previous equations and their results. That would make parsing of the current formula much easier, and you would also have an easy time of keeping your previous formula and their results in a scrollable list, with the easy option of keeping the most recent on top.
your idea is interesting. so you would have a line such as.
2+2
then when pressing calculate would add the line
4
and so on then you could type in another equation.
it could work but as you said it wouldn't be the most efficient implementation... but that's just a tradeoff of getting the desired functionality.
If i were going to implement it the way you discribed (with a JTextArea) I'd use scanner, and scan the value string a line at a time.
if the line has +/- in it then do the calculation and add both the original line and the answer to a string.
the new string is the new value of the text field.
this method would get pretty cumbersom as you would be continually recalculating the users old entries more were added.
I guess if you continually stored the last line of the document, when you run out of lines, calculate the last stored and append the answer, then it wouldn't be so bad.
Here's what I would do:
use a JTextField to enter in the calculations, and a JList to display the old ones and their answers.
You could treat each line as a single operation. That way you could use the String array returned directly by:
String [] operations = textArea.getText().split("\n");
And then you'll know that exactly each one of them as a complete operation ( may be invalid, but that' another story )
Is this what you asked or do I totally misread you?
I think a simpler solution would actually use two components. A TextArea to hold the "history" of what's happened so far, and a textfield where the user inputs new entries.
Thanks to everyone who replied. You all gave me some ideas to think about. I think right now, I am going to go with my original idea of using a single JTextArea and try to find ways to optimize the process. If that gets too difficult (which is very possible), I will follow the majority's advice and use two separate fields. Thanks for replying everyone!