So I have two JComboBoxes, and what I am trying to do is be able to compare the value in one, with the value in the second and then figure out the difference or the total. I want to be able to input a time in the first box (let's say 8:00 A.M) and then I want to be able to input 5:00 P.M in the second box, and have it display 9.
Is there a way to do this?
I don't have any working code, just need to be steered in the right direction.
you can add itemListener on both the comboboxes and in those item listeners, check if there is something selected in other combobox...accordingly you can perform your calculations....
Related
I have created an array of buttons (JButton[] jb = new JButton[100]) and I want to add the same action listener for all the buttons in the jb array instead of adding them one by one.
Just imagine every time I click one of the buttons displayed on the screen and it will print out the index of that button in the jb array.
The trivial answer is yes, you can add the same listener to many different buttons, and via the parameter the listener will be able to figure out which Button instance clicked it.
The challenge is that you won't be able to know which index in the array matches the button, so you'll need to either:
Figure out which button it was based on something in the button, like its getLabel value
Scan the array each click to figure out the index for a button instance, which is slow (O(n)), but for a hundred buttons it might not matter that much
Store a mapping from button instance to array index in a different faster data structure like a HashMap (O(1)), which might get ugly if buttons aren't well defined to be hashable
Use the same ActionListener class for all the buttons, but store a field on each instance of the listener that tells it what index it maps to
I'm sorry if the title is vague. It's because I don't know exactly what it is called.
I am creating a program for a school project and I'm trying to figure out a shorter version of a code that I already got.
The interface looks like this:
Interface
The other parts of the program are not really necessary to put on here. They just input texts/strings in the first row textfields and shifts down one column every time new info is put in the first row.
The buttons on the right (the ones with the [x]) are the ones that are coded. They remove the text/string in the row next to them and shifts up the texts from below (if there are any) by one column.
The code for the first button is such:
CC01.setText(CC02.getText());
SC01.setText(SC02.getText());
SU01.setText(SU02.getText());
SD01.setText(SD02.getText());
SR01.setText(SR02.getText());
CC02.setText(CC03.getText());
SC02.setText(SC03.getText());
SU02.setText(SU03.getText());
SD02.setText(SD03.getText());
SR02.setText(SR03.getText());
CC03.setText(CC04.getText());
SC03.setText(SC04.getText());
SU03.setText(SU04.getText());
SD03.setText(SD04.getText());
SR03.setText(SR04.getText());
CC04.setText(CC05.getText());
SC04.setText(SC05.getText());
SU04.setText(SU05.getText());
SD04.setText(SD05.getText());
SR04.setText(SR05.getText());
CC05.setText(CC06.getText());
SC05.setText(SC06.getText());
SU05.setText(SU06.getText());
SD05.setText(SD06.getText());
SR05.setText(SR06.getText());
CC06.setText(CC07.getText());
SC06.setText(SC07.getText());
SU06.setText(SU07.getText());
SD06.setText(SD07.getText());
SR06.setText(SR07.getText());
CC07.setText(CC08.getText());
SC07.setText(SC08.getText());
SU07.setText(SU08.getText());
SD07.setText(SD08.getText());
SR07.setText(SR08.getText());
CC08.setText(CC09.getText());
SC08.setText(SC09.getText());
SU08.setText(SU09.getText());
SD08.setText(SD09.getText());
SR08.setText(SR09.getText());
CC09.setText(CC10.getText());
SC09.setText(SC10.getText());
SU09.setText(SU10.getText());
SD09.setText(SD10.getText());
SR09.setText(SR10.getText());
CC10.setText("");
SC10.setText("");
SU10.setText("");
SD10.setText("");
SR10.setText("");
So, yes, I have ten of these buttons. And the code for each button reduces by one 5-line code. For example, Button1's code is the one you see up top, then Button2's code starts from the [CC02...], the Button3 starts from [CC03], etc.
I think I used the brute force method of this code which I don't think is efficient and makes my code too long (the code for the ten [x] buttons alone is around 400 lines).
I asking of there is a much shorter way of doing this method.
Thanks.
The situation is:
I want to use two date values obtained from two jDateChoosers and calculate the number of days between two selected days.
The question is: what should I do that after choosing both dates a calculated value would appear in a label (or Text Field, which one is better?) without any additional submission (in other words, without clicking on any buttons ect.)?
Have an actionListener checking for if both dates have been input when the jDateChoosers close and then have a function such as update() to perform the calculations and update your label
I need to create a number of dialogs that are of the same basic structure that looks like this:
There will be a varying number of rows, each with a labeled checkbox and two combo boxes that have integers, the range of which varies. The check box just enables the combo boxes. When the selection in the first combobox is changed, the second one gets initialized and enabled.
Since I have to do over 50 over these I'd like automate the programming. I believe some of the code can be handled with loops, selecting combobox names from preset string arrays. What I can't figure out is how to parameterize things like action listeners.
First question is can this be done at all. If it can, how?
Ed
First create a notional RowModel containing a Boolean value for the checkbox, a String for the label and two instances of ComboBoxModel, one for each of the combos. Handle the combo dependency as shown here. Let your program maintain a List<RowModel> for each distinct dialog. You can manage an arbitrary number of rows in a suitable TableModel and display them is a JTable as shown here.
Actually, I just want to say that MadProgrammer provided an answer that worked for me in his Comment.
So I have a method that graphs data from an arraylist, which contains names and there ranking for different decades. Every time the user types in a different name and pushes the "Graph" button on the GUI, the name will be graphed according to it's ranking for each decade. However, when I want to go clear the graph I want to be able to clear one graph line(each graph line is just one name's info) at a time when I push "Clear One" button. I have a clear all method, which looks likes this,
public void clearAll()
{
//Adds code to clear all names from the graphArray
graphArray.clear();
//Calls repaint() to update the graph
repaint();
}
and this belongs to my "Clear All" button. This works to clear all the different graph lines at the same time, but how can I clear just one at a time, specifically I want to remove the first graph line they entered first. So if the user enters 4 names(like sam, bill, bob, john), I want my clear one button to remove the oldest named entered which would be sam. Then the next time the button is hit, it would remove bill. So is there something like the
.clear()
that can do this or what do I need to do?
As your data is stored in an ArrayList called graphArray, you can call graphArray.remove(0) to delete the first element in that list, assuming that the first element is the oldest value.
This can easily be found using the API for ArrayList found here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html