This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Change first JSpinner influences contents second JSpinner  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I want the user to be able to add records. I was thinking about using coupled JSpinner's. My problem is that I can not find how to couple two (or more) JSpinner's, so the value showing in the first JSpinner influences the content of the second one.

To be a bit more specific, imagine working for several clients. For each client one can work in several project. It must be possible to add new project for existing customers, or even add new customers with new projects. When a customer is displayed in the first JSpinner, only project numbers of that customer may be displayed in the second one. If another customer is chosen, the customer specific project numbers need to be displayed.

First question: is the usage of JSpinner's a viable approach, or is there a better one?
Second question, how can I get a change in one JSpinner to influence the contents of the second one?

What I have now is:

In How to Use Spinners in the paragraph Detecting Spinner Value Changes changes in one JSpinner influence the contents of the second one. But in the SpinnerModel only the previous, current, and next value can be given to the second JSpinner. Do I need to keep a pointer to the current value of customer values to know what the previous and next values are?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got so many fields?
Why are your fields not private?
Why is the frame marked static?

It seems very peculiar design to have one spinner influence another. Please explain why that would help.
Why have you got the Strings in arrays rather than a Company object?
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I( can think of many instances where it is proper for the selected value of one spinner (or combobox) will influence the contents of another one. For example, select a state from one instance, then the second instance gets filled with the names of all counties in that state. Or select a school course in the first and the second now gets filled with all of the students currently taking that course.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently you still haven't read the answer from your last posting: http://www.coderanch.com/t/656867/GUI/java/JComboBoxes-DropDown-buttons

You still haven't replied.

You still haven't posted a SSCCE in this question.



 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Apparently you still haven't read the answer from your last posting: http://www.coderanch.com/t/656867/GUI/java/JComboBoxes-DropDown-buttons

You still haven't replied.


If I did not read the answer posted, it follows I still haven't replied. Two assumptions from you. I have read your answer. I was quite upset with it. If I had answered directly I might have been a bit terse in my answer. It follows I am struggling with the SSCCE concept. If you measure the temperature you influence the results. If I remove too much of my logic it might work, but it might also be very difficult to get that working in my original problem. Instead of answering I decided to try it with another approach. I have been busy for two days to solve the problem I post here.

And to answer the points you made in my previous post:
Rob Camick wrote:1) the OK, Escape buttons are irrelevant and should not be included
2) the data in the combo boxes is irrelevant so get rid of the complex logic
3) the first, third and fourth combo boxes are irrelevant. (if you figure out why the second doesn't work then you likely have the same problem with the third), so get rid of the code related to those items
4) the ProjectDef class is irrelevant.


1. Both buttons are removed in this example
2. The logic to get to the combo boxes is essential for my program, and might be the cause for the missing drop down buttons I showed in my captures. Remove the complex logic and I have no problem. How do I go from that situation to the situation I want to accomplish?
3. The first, third and fourth combo boxes are removed
4. If I worked a bit harder I might have come up with an alternative to the ProjectDef bean. Caveat about the temperature measurement, but 'mea maxima culpa'.

I am going to try a third approach.


 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why have you got so many fields?
Why are your fields not private?
Why is the frame marked static?

It seems very peculiar design to have one spinner influence another. Please explain why that would help.
Why have you got the Strings in arrays rather than a Company object?


Why have you got so many fields? ==> Yes, I might have gone a bit overboard with the number of fields.
Why are your fields not private? ==> Because I did not think to hide from other classes in my package.
Why is the frame marked static? ==> If I remember correctly it was enforced by the compiler (from Eclipse).
Why have you got the Strings in arrays rather than a Company object? ==> A choice I made. I used the String array to fill my version of a Company object, the ProjectDef object.

It seems very peculiar design to have one spinner influence another. Please explain why that would help. ==> Fred Kleinschmidt gave a nice explanation. Thanks Fred.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:. . .
Why have you got so many fields? ==> Yes, I might have gone a bit overboard with the number of fields.
You can probably change most of those fields to local variables in the constructor/setUpGUI method. If they are used in an anonymous class or λ they should be marked final. That last part was relaxed slightly in Java8.

. . .
Why is the frame marked static? ==> If I remember correctly it was enforced by the compiler (from Eclipse).
. . .
Never believe the compiler when it tries to force something to be static. It means you have failed to create a reference somewhere.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the Stackoverflow link you posted earlier wrote:. . .
C++ defines an additional level called "friend" and the less you know about that the better.


…and later,
. . .
So, rule of thumb is to make things only as visible as they have to be. Start with private and only add more visibility as needed..
Why do you need access outwith your class?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . . For example, select a state from one instance, then the second instance gets filled with the names of all counties in that state. . . .
That doesn't tell me anything about spinners.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Fred Kleinschmidt wrote:. . . . For example, select a state from one instance, then the second instance gets filled with the names of all counties in that state. . . .
That doesn't tell me anything about spinners.


And it does not help me finding a solution to my original problem. How can I get the "value showing in the first JSpinner influence the content of the second one. "
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the other Spinner's setXXX methods?
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:With the other Spinner's setXXX methods?


That would be setEditor(JComponent editor), setModel(SpinnerModel model), setUI(SpinnerUI ui) or setValue(Object value)? I do not see how I can make that work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!