Get current range of a spinner? - java

My question is quite simple, unfortunately I can't find an answer.
Basically, I have a Swing window, with 2 JSpinners. The second JSpinner has to define the high bound of the first JSpinner. So for example, if the user puts the value 4 on the second JSpinner, they mustn't be able to put a value higher than 4 in the first JSpinner. And if the value currently displayed in the first JSpinner is higher than 4 I'll set it to 4.
So I've built my two JSpinners with SpinnerNumberModel in the constructor to set the initial bounds:
// First spinner
SpinnerNumberModel spinnerLimits_sante = new SpinnerNumberModel(3, 0.25, 3, 0.25);
JSpinner spin_sante = new JSpinner(spinnerLimits_sante);
// Second spinner
JSpinner spin_sante_max = new JSpinner(spinnerLimits_sante_max);
SpinnerNumberModel spinnerLimits_sante_max = new SpinnerNumberModel(3, 3, 32767, 1);
To get the value of the second JSpinner when it changes, I thought about using a ChangeListener on it. So each time it changes, I can get the new value, recreate a SpinnerNumberModel object using the new bounds (the low-bound and the interval wouldn't change, however the high-bound will be updated and the value will be updated if it is higher than the high-bound).
ChangeListener listenerSanteMax = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int newBound = spin_sante_max.getValue();
// ...
}
};
spin_sante_max.addChangeListener(listenerSanteMax);
Unfortunately this is where I'm stuck: JSpinner has no getter that would return a SpinnerNumberModel object nor its values.
And even if there was one, it seems there's no setter either.
So, my question:
Is it possible to dynamically change the range and value of a spinner?
Thanks in advance!

In the ChangeListener, instead of creating a new SpinnerNumberModel, just update the maximum value of the existing model. Here's two ways to do it.
If the ChangeListener can see the spinner models, you can do the update directly.
ChangeListener listenerSanteMax = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
Number newMaximum = spinnerLimits_sante_max.getNumber().doubleValue();
spinnerLimits_sante.setMaximum((Comparable) newMaximum);
}
};
If the ChangeListener can't see the spinner models, you can do a cast.
JSpinner has a getModel() method the returns a SpinnerModel. You can cast it to a SpinnerNumberModel, but you should use instanceof to check that the cast will work.
ChangeListener listenerSanteMax = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
Object source = e.getSource();
if (source instanceof JSpinner) {
JSpinner spinner = (JSpinner) source;
SpinnerModel sm = spinner.getModel();
if (sm instanceof SpinnerNumberModel) {
SpinnerNumberModel model = (SpinnerNumberModel) sm;
Number newMaximum = model.getNumber().doubleValue();
spinnerLimits_sante.setMaximum((Comparable) newMaximum);
}
}
}
};
In either case, newMaximum is a Number, and all subclasses supported by SpinnerNumberModel are Comparable.

Use getMode() to obtain the model from the JSpinner and type-cast it to SpinnerModelNumber like this:
SpinnerNumberModel snm = (SpinnerNumberModel)spinner.getModel();

Related

Find the index of a Java JButton Array

I am writing a Java program in which I have an array of buttons (not a calculator!) and I'm looking for an efficient way to know which JButton was clicked. From what I know of Java so far, the only way to do it is to have them all in the same ActionListener and then loop through looking for a match.
Another solution I just thought of would be to extend JButton to include a unique ID number variable in the constructor. It seems that should work when the event object is cast to JButton after an instanceof check. Kind of like using VB's Tag property which is assigned to the index number.
Is there a better/more elegant way?
Is there a better/more elegant way?
yes to use (for almost JComponents) the put/getClientProperty, there you can to set endless number of properties, can be multiplied in contrast with setName / setActionCommand / etc
getClientProperty can be used as unique identificator for Swing Action or EventHandler (rather than to use ActionListener)
Links to the Javadocs: putClientProperty(), getClientProperty()
Here is an example from programm I writing last few months.
I have an enum called Position
public enum Position {
BB, SB, BU, CO, MP3, MP2, MP1, EP3, EP2, EP1;
}
and have some JToggleButtons each of them holding ist own Position.
public class PositionButton extends JToggleButton {
private final Position p;
public PositionButton(Position p) {
this.p = p;
setText(p.toString());
setActionCommand(p.toString());
}
public Position getPosition() {
return p;
}
}
This allows you to create Buttons in a Loop and get value direct from button without comparing:
ActionListener positionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
PositionButton b = (PositionButton )e.getSource();
Position p = b.getPosition();
//do something with Position
}
}
for (Position p : Position.values()) {
PositionButton b = new PositionButton (p);
b.addActionListener(positionListener);
}
The way I've done it before was using actionPerformed for different buttons. I like it more compared to some other ways I've seen.
public void actionPerformed(ActionEvent clicking)
{
if (clicking.getSource() == button[0])
// Do this
if (clicking.getSource() == button[1])
// Do something different
}
Since you built an array, you can throw the ID right where that 0 is and that's your unique ID.
Add a separate action listener for each button.

Referring to a Dynamically Created JButton

I have a JPanel on which I've dynamically added quite a few JButtons. All of this is working perfectly. Later on in my program execution, I need to refer back to these buttons and pull out the button text. I'm having trouble figuring out how to refer back to them.
When I created each button, I gave it a unique name. Let's say this is the code where I created the button:
public void createButton(Container parent, String btnName) {
JButton btn = new JButton("xyz");
btn.setName(btnName);
btn.addActionListner(new ActionListner() {
//code
}
parent.add(btn);
}
In another method, I'm trying to retrieve the label on the button since it may have changed at run time. Do I need to keep an array of these buttons as they are created? Or is there a way that I can refer back to them directly?
This is what I was working on, but it's stupid. Can anyone suggest a correct approach?
public String getBtnLabel(String btnName) {
JButton btn = (JButton) btnName;
return btn.getText();
}
If the answer is that I just need to create the array and then iterate over it, that's fine. Just looking for other options.
You need to use a Map<String, JButton> so when you create your dynamic buttons you give them some sort of unqiue name:
//somewhere at the top of your class
private final Map<String, JButton> myButtonMap = new HashMap<>();
public void createButton(Container parent, String btnName) {
JButton btn = new JButton("xyz");
btn.setName(btnName);
btn.addActionListner(new ActionListner() {
//code
}
parent.add(btn);
myButtonMap.put(btnName, btn);
}
And then simply get from the map
public String getBtnLabel(String btnName) {
return myButtonMap.get(btnName).getText();
}
This will obviously throw an NPE if the button isn't defined...
Also you will need to delete from your map when you're done with it otherwise you're asking for a memory leak...
I suggest you to use a Map< String, JButton >.
At creation time you put new button into it with buttons.put( name, btn )
In event handler you use JButton btn = buttons.get( name )
Yes you need to keep references to the buttons. An array would be an option, but since arrays are awkward to use, you should prefer a List.
If you have a a reference to the JPanel containing the buttons, you could get them from it. but that is likely to be rather bothersome.
I would recommend keeping a list of your buttons or a reference to them in a map, however you could do this:
for (Component i : parent.getComponents()) {
if (i.getName().equals(btnName)) {
JButton b = (JButton) i;
// do stuff..
}
}
Using the parent component and iterating over the added components.

Java - Updating JList after changing an object

I have a JList which uses a DefaultListModel.
I then add values to the model which then appear in the JList. I have created a MouseListener which (when double clicked) allows the user to edit the current user number of that person they have selected.
I have checked that the actual object of that record is being changed, and it is. The only issue I'm having is getting the actual Jlist to update to show the new values of that object.
Snippets of the current code I have are:
Creating the JList and DefaultTableModel:
m = new DefaultListModel();
m.addListDataListener(this);
jl = new JList(m);
jl.addMouseListener(this);
Updating the object:
String sEditedNumber = JOptionPane.showInputDialog(this, "Edit number for " + name, number);
if (sEditedNumber != null) {
directory.update (name, sEditedNumber);
}
And (when jl is the JList and m is the DefaultTableModel):
public void contentsChanged(ListDataEvent arg0) {
jl.setModel(m);
}
Instead of setModel(), update your existing model using one of the DefaultListModel methods such as setElementAt(), which will fireContentsChanged() for you.
You need to call fireContentsChanged() on the ListModel.
You need to call DefaultListModel.fireContentsChanged(). But since this method is protected (I really wonder why), you can't do that directly. Instead, make a small subclass:
class MinoListModel<T> extends DefaultListModel<T>
{
public void update(int index)
{
fireContentsChanged(this, index, index);
}
}
Use it as your list model:
m = new MinoListModel<>();
jl = new JList(m);
After updating a user number, update the corresponding entry: m.update(theIndex);
Alternatively, if you don't want a subclass, you can just replace the JList element after the user number changed: m.setElementAt(theSameElement, theIndex);. Though this is somewhat cumbersome and having a subclass seems the cleaner approach.

Help in automatically changing value upon ItemChange in JComboBox

I have a program in which I am using 3 things, a checkbox, a combobox and a textfield. The logic works like this if checkbox is enable then combobox and textfield are enable unless not.
Then set some value in the textfield by mulitplying it with the item in combobox.
From the frame - The value of Final Price is Price * Quantity.
Now the issue when I click purchase everything went fine. But when I change the value from Jcombobox it doesn't automatically change the value in final price and remains to be 1200 as in first case. For the value to be changed I have uncheck and then check the Checkbox.
What could be the problem. I have used ItemListner for both checkbox and combobox.
#Override
public void itemStateChanged(ItemEvent e){
Object get = e.getSource();
int multiplier;
int ftotal;
if (e.getStateChange()==ItemEvent.SELECTED){
if(get==chkbox1){
qntbox1.setEnabled(true);
size1.setEnabled(true);
multiplier = Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()));
ftotal = Integer.parseInt(price1.getText()) * multiplier;
fprice1.setText(String.valueOf(ftotal));}
You have to implement ActionListener for your JComboBox:
private static final String command_cbo1 = "ComboBox1";
// ...
public class YourClass implements ItemListener, ActionListener
{
// ...
public YourClass()
{
// ...
qntbox1.addActionListener(this);
qntbox1.setActionCommand(command_cbo1);
// ...
}
// ...
public void itemStateChanged(ItemEvent e)
{
// ...
}
// ...
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox) e.getSource();
String s = (String) cb.getSelectedItem();
if(e.getActionCommand().equals(command_cbo1))
{
fprice1.setText("" + (Integer.parseInt(price1.getText()) * Integer.parseInt(s)));
}
// ...
}
// ...
}
not directly to your question
1/ JCheckBox is totally useless, that will be really needed for final calculation(s)
2/ consider that JComponents for Price and Final Price would be only JFormattedTextField, then you can pretty to forgot for Parse#Whatever
3/ consider that JComponents for Quantity would be only JSpinner, but workaround for Number Instance would be litte bit complicated as for JFormattedTextField example here
4/ for nice output put everything to the JTable
5/ for JComboBox I preferred ItemListener not ActionListener, because your problems isn't with proper Listener but with parsing Numbers correct way
Ok got it working. The ActionListner made it work (JComboBox). I guess using ItemListner for too many components made parsing a little confusing, add to that I used too many clauses in the ItemListner scope. Thanks a lot everyone for helping.
#mKorbel : I'll be using your suggestion asap :) and will check JTable and said components. have to go through them since I haven't used it.
#Eng.Fouad : Thanks man for the help.
Just one issue. When I typecast getSelectedItem() to integer it gives NumberFormatException error (runtime). So I have to first change the object to String and then parseit into integer. Any clue why direct conversion is throwing error ?
Here is the working code for the project.
public void itemStateChanged(ItemEvent e){
Object get = e.getSource();
if (e.getStateChange()==ItemEvent.SELECTED){
if(get==chkbox1){
qntbox1.setEnabled(true);
size1.setEnabled(true);
fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(String.valueOf(qntbox1.getSelectedItem()))));
}
#Override
public void actionPerformed (ActionEvent ae)
{
Object toggel = ae.getSource();
String check;
if (toggel == qntbox1)
{
check = (String) qntbox1.getSelectedItem();
fprice1.setText(String.valueOf(Integer.parseInt(price1.getText()) * Integer.parseInt(check)));
}

Java/Swing: Update data on stateChange

I have a list of Map.Entry<String,Integer>s that I am looping through, and for each one, making a JLabel/JSpinner representing that particular entry.
How can I make it so that when the ChangeListener fires on the JSpinner, it updates that entry to reflect the new value?
My code looks like
for (Map.Entry<String,Integer> entry : graph.getData()) {
SpinnerNumberModel model = new SpinnerNumberModel(
entry.getValue(),(Integer)0,(Integer)100,(Integer)5);
JSpinner spinner = new JSpinner(model);
JLabel label = new JLabel(entry.getKey());
spinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
entry.setValue((Integer)((JSpinner)e.getSource()).getValue());
}
});
// more stuff
}
However, this does not work, because entry either needs to be final or object-level, and it is neither.
Is there an easier way, like C#'s dataSource that directly binds the object to the spinner? Or how can I make this work?
There are several options if you want to stick with individual JLabel and JSpinner. Use #trashgod's answer if the map could get large.
WRONG Per #Suresh Kumar's comment: final Map.Entry<String,Integer> entry
WRONG Add spinner.putClientProperty("MyEntryKey", entry), then in your ChangeListener get the entry with spinner.getClientProperty.
Add spinner.putClientProperty("MyEntryKey", entry.getKey()), then in your ChangeListener graph.put(spinner.getClientProperty(), ...).
From #Michael's answer (not sure if this is ethical): Add final String key = entry.getKey(), then in your ChangeListener graph.put(key, ...).
Let your enclosing class implement ChangeListener to avoid one ChangeListener per JSpinner:
public void stateChanged(ChangeEvent e) {
JSpinner spinner = (JSpinner)e.getSource();
graph.put((String)spinner.getClientProperty("MyEntryKey"), (Integer)spinner.getValue());
}
BTW graph.getData() looks a bit odd. If graph implements Map, you should use graph.entrySet().
Particularly if the number of Map entries is large, consider using a JTable, with your Map as the core of the data model and a custom render/editor extending JSpinner. There's a related example here.
#Suresh is right about using final in the for loop, but since Map.Entry values aren't viable outside of the iterator, this would lead to trouble. Another possibility for this one-way binding would be
for (Map.Entry<String, Integer> entry : graph.getData()) {
...
final String key = entry.getKey();
spinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
graph.put(key, (Integer)((JSpinner)e.getSource()).getValue());
}
});
For a more complete data binding, you may want a more complex object to bind against. See the jgoodies binding API's ValueModel and SpinnerAdapterFactory, for example.

Categories

Resources