-1

In my swing program I have 31 JSpinners, without modifies (JSpinner spinner = new JSpinner();). When I run my program and I go to manually write a value into the value field of the JSpinners, the more JSpinners I write values into, the more I can't write anymore. For example, in the first three or four JSpinners I can write 4 digits, then it allows me to only write 3, then 2, then 1 and at the end I can't even write anymore.

This is my code about the spinners:

static JSpinner[] spinners = new JSpinner[31];
...
spinners[i]= new JSpinner();
if(i==0)
   spinners[i].setEnabled(false);
spinners[i].setValue(0);
spinners[i].setName(Integer.toString(i)); //"i" is a "for" counter
spinners[i].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
((JSpinner.NumberEditor)spinners[i].getEditor()).
          getTextField().setBackground(Color.decode("#ababab"));
((JSpinner.NumberEditor)spinners[i].getEditor()).
          getTextField().setForeground(Color.BLACK);
          spinners[i].setUI(new MyUI());

JComponent comp = spinners[i].getEditor();
JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);

Deleting the formatter.setCommitsOnValidEdit(true); does so that if I write a value like 11500, returns to the previous value (for example 0), so for it it's not a valid value, even if it's an int.

MyUI() modifies what the buttons do, they modify a JLabel's text while modifing the JSpinner value.

This is a screenshot of what happens

As you can see I'm able to write 4 digits in the first JSpinner, then I can write only 3 on some of them and then 0 or 1 in the others.

There's nothing about this on the Internet, does anyone have experience with this issue? Thanks.

ServantGrunt
  • 35
  • 11

1 Answers1

2

It's hard to say where things go wrong, but I'm really suspicious of calling setUI() like this. One way to pin the problem down is to re-factor your code to create an isolated, labeled spinner and add it to the layout.

In this example, I used setColumns() to make each spinner have the same size in a GridLayout. I didn't have any trouble entering numbers in the last spinner.

image

private static final int N = 31;
…
List<JSpinner> list = new ArrayList<>();
JPanel p = new JPanel(new GridLayout(N, 0));
for (int i = 0; i < N; i++) {
    p.add(createSpinnerPanel(list, i));
}
…
private JPanel createSpinnerPanel(List<JSpinner> list, int i) {
    JSpinner s = new JSpinner();
    list.add(s);
    JFormattedTextField f = (JFormattedTextField) s.getEditor().getComponent(0);
    f.setColumns(4);
    JPanel p = new JPanel(new GridLayout(0, 2));
    p.add(new JLabel("Label " + String.valueOf(i + 1) + ": ", JLabel.RIGHT));
    p.add(s);
    return p;
}
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • I'll now try setting the columns for each JSpinner and let you know, thanks a lot! – ServantGrunt Aug 31 '15 at 18:11
  • Just tried but with no success, the problem persists. I added a screenshot to the question anyway, thanks again for your help! – ServantGrunt Aug 31 '15 at 18:17
  • 2
    *"..added a screenshot.."* Add a [mcve] rather than screenshots! – Andrew Thompson Sep 01 '15 at 00:52
  • @ServantGrunt: I changed my example to look more like your screenshot, and it still works OK. I'm guessing there's a problem somewhere else in your code. – Catalina Island Sep 01 '15 at 12:21
  • I couldn't add a Minimal, Complete, and Verifiable example so I added a screenshot... Catalina do you have any idea of what the problem might be? Something in MyUI may cause the problem? – ServantGrunt Sep 01 '15 at 15:49
  • @ServantGrunt: What happens when you comment out `setUI()`? – Catalina Island Sep 02 '15 at 11:29
  • @ServantGrunt: Well, we know it works in isolation. There's too many possibilities to be sure, but I'd try to pare it down and maybe look for an inadvertently static value. – Catalina Island Sep 03 '15 at 11:27
  • So do you suggest taking a look at static variables? That's what I needed to know... My JSpinners array is static because I need to access it from event listeners, do you believe being static may cause the issue? – ServantGrunt Sep 03 '15 at 16:41
  • @ServantGrunt: All I can say for sure is that my `List` is _not_ static. Your array shouldn't _need_ to be static. – Catalina Island Sep 04 '15 at 15:58
  • I need to work on it in MouseListeners and ChangeListeners so they require a static variable, what should I do to avoid keeping it static? Thanks a ton for your help! – ServantGrunt Sep 04 '15 at 19:38
  • No need for `static`; you can use `getSource()` like [this](http://stackoverflow.com/q/7340001/261156). – Catalina Island Sep 08 '15 at 10:13
  • I used getSource() obviously but the MouseListeners are on another array, I need to work on my spinners array from there and there's no way to reach it except from static, from what I was able to find... – ServantGrunt Sep 09 '15 at 22:31
  • You're going to need to re-factor the code. Without a [complete example](http://stackoverflow.com/help/mcve), it's hard to say how to fix it. – Catalina Island Sep 10 '15 at 07:35