Intermittent error accessing Java constant

2 次查看(过去 30 天)
Short question up front: How do I reliably access a constant in a Java class, such as javax.swing.JCheckBox.TRAILING?
I am using MATLAB's Java integration to build a GUI. Frequently, when setting up a a GUI during unit tests, MATLAB will give this error:
??? No appropriate method, property, or field TRAILING for class
javax.swing.JCheckBox.
However, I have never been able to reproduce this in the debugger, nor can I find a sequence of operations that will consistently cause this error. At the moment, I have two copies of MATLAB open, one showing the error and one that works as expected:
>> javax.swing.JCheckBox.TRAILING
ans =
11
versus
>> javax.swing.JCheckBox.TRAILING
??? No appropriate method, property, or field TRAILING for class
javax.swing.JCheckBox.
I can show that MATLAB knows what JCheckBox is:
>> javax.swing.JCheckBox
ans =
javax.swing.JCheckBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@108b647,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=]
I can see that during the error condition, the SwingConstants interface is missing by using struct(javax.swing.JCheckBox()) to enumerate all the fields:
>> struct(javax.swing.JCheckBox)
ans =
BORDER_PAINTED_FLAT_CHANGED_PROPERTY: [1x1 java.lang.String]
MODEL_CHANGED_PROPERTY: [1x1 java.lang.String]
TEXT_CHANGED_PROPERTY: [1x1 java.lang.String]
MNEMONIC_CHANGED_PROPERTY: [1x1 java.lang.String]
MARGIN_CHANGED_PROPERTY: [1x1 java.lang.String]
VERTICAL_ALIGNMENT_CHANGED_PROPERTY: [1x1 java.lang.String]
HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY: [1x1 java.lang.String]
VERTICAL_TEXT_POSITION_CHANGED_PROPERTY: [1x1 java.lang.String]
HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY: [1x1 java.lang.String]
BORDER_PAINTED_CHANGED_PROPERTY: [1x1 java.lang.String]
FOCUS_PAINTED_CHANGED_PROPERTY: [1x1 java.lang.String]
ROLLOVER_ENABLED_CHANGED_PROPERTY: [1x1 java.lang.String]
CONTENT_AREA_FILLED_CHANGED_PROPERTY: [1x1 java.lang.String]
ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
PRESSED_ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
SELECTED_ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
ROLLOVER_ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
DISABLED_ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
DISABLED_SELECTED_ICON_CHANGED_PROPERTY: [1x1 java.lang.String]
WHEN_FOCUSED: 0
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: 1
WHEN_IN_FOCUSED_WINDOW: 2
UNDEFINED_CONDITION: -1
TOOL_TIP_TEXT_KEY: [1x1 java.lang.String]
TOP_ALIGNMENT: 0
CENTER_ALIGNMENT: 0.5000
BOTTOM_ALIGNMENT: 1
LEFT_ALIGNMENT: 0
RIGHT_ALIGNMENT: 1
WIDTH: 1
HEIGHT: 2
PROPERTIES: 4
SOMEBITS: 8
FRAMEBITS: 16
ALLBITS: 32
ERROR: 64
ABORT: 128
Note that TRAILING and all the other SwingConstants are missing. Frequently, in the course of attempting to debug this, I'll do something that causes the SwingConstants to magically appear, but which is not sufficient to prevent the problem from occurring if I put it in my code prior to using the constant.
I have tried setting breakpoints or using "keyboard" to stop when this error occurs, but when I get control, things behave normally. The remaining tests pass, and the entire test suite passes when I rerun it. I have observed this on R2010b and R2012a, both Windows 32 and 64-bit.
Is there an explanation for this behavior, and how do I fix it?

回答(1 个)

Malcolm Lidierth
Malcolm Lidierth 2013-11-17
Strange. What happens if you look it up as:
javax.swing.SwingConstants.TRAILING

类别

Help CenterFile Exchange 中查找有关 Call Java from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by