Swing:自定义JSpinner模型导致StackOverflowError

时间:2015-12-07 13:20:20

标签: java swing model stack-overflow jspinner

我想为AbstractSpinnerModel写一个JSpinner来显示我的自定义类Time的值。我已经扩展JSpinner,看起来像这样:

TimeSpinner.java

import javax.swing.AbstractSpinnerModel;
import javax.swing.JSpinner;

public class TimeSpinner extends JSpinner {

    public TimeSpinner() {
        super();
        setModel(new TimeSpinnerModel());
        ((JSpinner.DefaultEditor) getEditor()).getTextField().setEditable(true);
    }

    public class TimeSpinnerModel extends AbstractSpinnerModel {

        private Time t = new Time(0);

        @Override
        public Object getValue() {
            return t;
        }

        @Override
        public void setValue(Object o) {
            try {
                t = Time.parseTime(o.toString());
                fireStateChanged();
            } catch (NumberFormatException e) {}
        }

        @Override
        public Object getNextValue() {
            // next 10 seconds step
            return new Time(((t.getSeconds() + 10) / 10) * 10);
        }

        @Override
        public Object getPreviousValue() {
            if (t.getSeconds() > 0) {
                // previous 10 seconds step
                return new Time(((t.getSeconds() - 1) / 10) * 10);
            }
            return t;
        }
    }
}

当我运行代码时,我得到一个StackOverflowError,因为似乎有一个Listeners循环通知对方。但在我查看的所有代码示例中,我完全按照我的方式完成,即在fireStateChanged()中调用setValue为什么会这样?

我的堆栈跟踪(直到它开始重复的点):

Exception in thread "main" java.lang.StackOverflowError
    at sun.awt.SunHints$Value.hashCode(SunHints.java:163)
    at java.awt.font.FontRenderContext.hashCode(FontRenderContext.java:352)
    at sun.font.FontDesignMetrics$MetricsKey.init(FontDesignMetrics.java:217)
    at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:286)
    at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1113)
    at javax.swing.JComponent.getFontMetrics(JComponent.java:1626)
    at javax.swing.text.PlainView.calculateLongestLine(PlainView.java:639)
    at javax.swing.text.PlainView.updateDamage(PlainView.java:574)
    at javax.swing.text.PlainView.removeUpdate(PlainView.java:464)
    at javax.swing.text.FieldView.removeUpdate(FieldView.java:307)
    at javax.swing.plaf.basic.BasicTextUI$RootView.removeUpdate(BasicTextUI.java:1624)
    at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.removeUpdate(BasicTextUI.java:1884)
    at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:259)
    at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:622)
    at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:590)
    at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:666)
    at javax.swing.text.JTextComponent.setText(JTextComponent.java:1669)
    at javax.swing.JFormattedTextField$AbstractFormatter.install(JFormattedTextField.java:948)
    at javax.swing.text.DefaultFormatter.install(DefaultFormatter.java:125)
    at javax.swing.JFormattedTextField.setFormatter(JFormattedTextField.java:464)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:788)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:501)
    at javax.swing.JSpinner$DefaultEditor.stateChanged(JSpinner.java:717)
    at javax.swing.JSpinner.fireStateChanged(JSpinner.java:458)
    at javax.swing.JSpinner$ModelListener.stateChanged(JSpinner.java:386)
    at javax.swing.AbstractSpinnerModel.fireStateChanged(AbstractSpinnerModel.java:119)
    at TimeSpinner$TimeSpinnerModel.setValue(TimeSpinner.java:26)
    at javax.swing.JSpinner.setValue(JSpinner.java:354)
    at javax.swing.JSpinner$DefaultEditor.propertyChange(JSpinner.java:752)
    at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
    at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
    at java.awt.Component.firePropertyChange(Component.java:8430)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:798)
    at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:501)
    at javax.swing.JSpinner$DefaultEditor.stateChanged(JSpinner.java:717)
    at javax.swing.JSpinner.fireStateChanged(JSpinner.java:458)

编辑:

这是我的Time.java

public class Time {

    private int sec;

    public Time() {
        sec = 0;
    }

    public Time(int sec) {
        this.sec = sec;
    }

    public void add(int sec) {
        this.sec += sec;
    }

    public void add(Time other) {
        this.sec += other.sec;
    }

    public int getSeconds() {
        return this.sec;
    }

    @Override
    public String toString() {
        if (this.sec < 3600)
            return String.format("%d:%02d", sec/60, sec%60);
        return String.format("%d:%02d:%02d", sec/3600, (sec%3600)/60, sec%60);
    }

    public static Time parseTime(String str) throws NumberFormatException {
        try {
            int i = Integer.parseInt(str);
            return new Time(i);
        } catch (NumberFormatException e) {}

        try {
            double d = Double.parseDouble(str.replace(',', '.'));
            return new Time((int)(d * 60.0));
        } catch (NumberFormatException e) {}

        String[] strs = str.split(":");
        int l = strs.length;
        int h, m, s;
        if (l == 3) {
            h = Integer.parseInt(strs[0]);
            m = Integer.parseInt(strs[1]);
            s = Integer.parseInt(strs[2]);
        }
        else if (l == 2) {
            h = 0;
            m = Integer.parseInt(strs[0]);
            s = Integer.parseInt(strs[1]);
        } else {
            throw new NumberFormatException();
        }

        return new Time(3600 * h + 60 * m + s);
    }
}

1 个答案:

答案 0 :(得分:2)

这是经过一些修改后的工作代码

TimeSpinnerModel

package com.test;

import javax.swing.AbstractSpinnerModel;
import javax.swing.JSpinner;

public class TimeSpinner extends JSpinner {

    public TimeSpinner() {
        super();
        setModel(new TimeSpinnerModel());
        ((JSpinner.DefaultEditor) getEditor()).getTextField().setEditable(true);
    }

    public class TimeSpinnerModel extends AbstractSpinnerModel {

        private Time t = new Time(0);

        @Override
        public Object getValue() {
            return t;
        }

        @Override
        public void setValue(Object o) {
            try {
                t.set(Time.parseTime(o.toString()));
                fireStateChanged();
            } catch (NumberFormatException e) {}
        }

        @Override
        public Object getNextValue() {
            // next 10 seconds step
            return new Time(((t.getSeconds() + 10) / 10) * 10);
        }

        @Override
        public Object getPreviousValue() {
            if (t.getSeconds() > 0) {
                // previous 10 seconds step
                return new Time(((t.getSeconds() - 1) / 10) * 10);
            }
            return t;
        }
    }
}

时间

public class Time {



    private int sec;

    public Time() {
        sec = 0;
    }

    public Time(int sec) {
        System.out.println(Com.cnt.getAndIncrement());
        this.sec = sec;
    }
    public void set(int sec){
        this.sec = sec;
    }
    public void add(int sec) {
        this.sec += sec;
    }

    public void add(Time other) {
        this.sec += other.sec;
    }

    public int getSeconds() {
        return this.sec;
    }

    @Override
    public String toString() {
        if (this.sec < 3600)
            return String.format("%d:%02d", sec / 60, sec % 60);
        return String.format("%d:%02d:%02d", sec / 3600, (sec % 3600) / 60,
                sec % 60);
    }

    public static int parseTime(String str) throws NumberFormatException {

        try {
            String[] strs = str.split(":");
            if (strs.length == 1) {
                return Integer.parseInt(str);

            } else if (strs.length == 2) {
                return Integer.parseInt(strs[0]) * 60
                        + Integer.parseInt(strs[1]);
            } else {

                int h, m, s;

                h = Integer.parseInt(strs[0]);
                m = Integer.parseInt(strs[1]);
                s = Integer.parseInt(strs[2]);
                return 3600 * h + 60 * m + s;
            }

        } catch (NumberFormatException e) {
            throw e;
        }
    }
}

当您创建Time的新实例时,实例数超出限制并导致StackOverflowError

其他方式

sergiy-medvynskyy所述, 您可以将equals and hashCode覆盖到Time类,并仅在值真正更改时生成状态更改事件(如果旧时间等于new - 则没有更改状态)。

示例代码:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + sec;
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Time)) {
        return false;
    }
    Time other = (Time) obj;
    if (sec != other.sec) {
        return false;
    }
    return true;
}