- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
从如下创建的 AbstractListModel 检索数据时出现问题:
warmup1List.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "sleepIn", "monkeyTrouble", "sumDouble", "diff21", "parrotTrouble", "makes10", "nearHundred", "posNeg", "notString", "missingChar", "frontBack", "front3", "backAround", "or35", "front22", "startHi", "icyHot", "in1020", "hasTeen", "loneTeen", "delDel", "mixStart", "startOz", "intMax", "close10", "in3050", "max1020", "stringE", "lastDigit", "endUp", "everyNth" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
我想从这个列表中获取项目,使用这样的 switch 方法:
public CodingBatGUI() {
initComponents();
switch(warmup1List.getSelectedValue()) {
case "sleepIn":
descriptionTextArea.setText("This is a test.");
break;
case "monkeyTrouble":
descriptionTextArea.setText("This is another test");
break;
default:
descriptionTextArea.setText("Nothing selected");
}
}
但是每当我尝试这样做时,就会出现此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Paketti.CodingBatGUI.<init>(CodingBatGUI.java:20)
at Paketti.CodingBatGUI$6.run(CodingBatGUI.java:321)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我还尝试使用 switch(warmup1List.getSelectedIndex()) 但它只会显示默认的“case”,然后我有 case 1: case 2: 等,但我希望它们作为字符串,因为项目是 AbstractListModel 中的字符串。
那么,我如何真正从 switch 循环内的列表中获取值?提前致谢!
编辑:问题是我在 CodingBatGUI 类中添加了代码。这是更新的脚本:
public void addActionListener(final ActionListener al) {
warmup1List.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
switch(warmup1List.getSelectedValue()) {
case "sleepIn":
descriptionTextArea.setText("This is a test.");
break;
case "monkeyTrouble":
descriptionTextArea.setText("This is another test");
break;
case "sumDouble":
descriptionTextArea.setText("This is another test");
break;
case "diff21":
descriptionTextArea.setText("This is another test");
break;
case "parrotTrouble":
descriptionTextArea.setText("This is another test");
break;
case "makes10":
descriptionTextArea.setText("This is another test");
break;
case "nearHundred":
descriptionTextArea.setText("This is another test");
break;
case "posNeg":
descriptionTextArea.setText("This is another test");
break;
case "notString":
descriptionTextArea.setText("This is another test");
break;
case "missingChar":
descriptionTextArea.setText("This is another test");
break;
case "frontBack":
descriptionTextArea.setText("This is another test");
break;
case "front3":
descriptionTextArea.setText("This is another test");
break;
case "icyHot":
descriptionTextArea.setText("This is another test");
break;
case "in1020":
descriptionTextArea.setText("This is another test");
break;
case "hasTeen":
descriptionTextArea.setText("This is another test");
break;
case "loneTeen":
descriptionTextArea.setText("This is another test");
break;
case "delDel":
descriptionTextArea.setText("This is another test");
break;
case "mixStart":
descriptionTextArea.setText("This is another test");
break;
case "startOz":
descriptionTextArea.setText("This is another test");
break;
case "intMax":
descriptionTextArea.setText("This is another test");
break;
case "close10":
descriptionTextArea.setText("This is another test");
break;
case "in3050":
descriptionTextArea.setText("This is another test");
break;
case "max1020":
descriptionTextArea.setText("This is another test");
break;
case "stringE":
descriptionTextArea.setText("This is another test");
break;
case "lastDigit":
descriptionTextArea.setText("This is another test");
break;
case "endUp":
descriptionTextArea.setText("This is another test");
break;
case "everyNth":
descriptionTextArea.setText("This is another test");
break;
default:
descriptionTextArea.setText("Nothing selected");
break;
}
}
}
});
}
但是,它仍然不会显示任何内容。
最佳答案
通过此脚本解决:
public CodingBatGUI() {
initComponents();
warmup1List.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
switch(warmup1List.getSelectedValue()) {
case "sleepIn":
descriptionTextArea.setText("This is a test.");
break;
case "monkeyTrouble":
descriptionTextArea.setText("This is another test");
break;
case "sumDouble":
descriptionTextArea.setText("This is another test");
break;
case "diff21":
descriptionTextArea.setText("This is another test");
break;
case "parrotTrouble":
descriptionTextArea.setText("This is another test");
break;
case "makes10":
descriptionTextArea.setText("This is another test");
break;
case "nearHundred":
descriptionTextArea.setText("This is another test");
break;
case "posNeg":
descriptionTextArea.setText("This is another test");
break;
case "notString":
descriptionTextArea.setText("This is another test");
break;
case "missingChar":
descriptionTextArea.setText("This is another test");
break;
case "frontBack":
descriptionTextArea.setText("This is another test");
break;
case "front3":
descriptionTextArea.setText("This is another test");
break;
case "icyHot":
descriptionTextArea.setText("This is another test");
break;
case "in1020":
descriptionTextArea.setText("This is another test");
break;
case "hasTeen":
descriptionTextArea.setText("This is another test");
break;
case "loneTeen":
descriptionTextArea.setText("This is another test");
break;
case "delDel":
descriptionTextArea.setText("This is another test");
break;
case "mixStart":
descriptionTextArea.setText("This is another test");
break;
case "startOz":
descriptionTextArea.setText("This is another test");
break;
case "intMax":
descriptionTextArea.setText("This is another test");
break;
case "close10":
descriptionTextArea.setText("This is another test");
break;
case "in3050":
descriptionTextArea.setText("This is another test");
break;
case "max1020":
descriptionTextArea.setText("This is another test");
break;
case "stringE":
descriptionTextArea.setText("This is another test");
break;
case "lastDigit":
descriptionTextArea.setText("This is another test");
break;
case "endUp":
descriptionTextArea.setText("This is another test");
break;
case "everyNth":
descriptionTextArea.setText("This is another test");
break;
default:
descriptionTextArea.setText("Nothing selected");
break;
}
}
}
});
关于java - 在 switch 循环中获取选定的 AbstractListModel 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001710/
我目前有一个 QtAbstractListModel 子类(为清楚起见缩短): class HolidayTask; class HolidayTaskModel: public QAbstractL
我有一个简单的ListModel,它是可过滤的并在JList中使用... 它使用以下代码... public class FilteredListModel extends AbstractListM
我正在尝试从 AbstractListModel 的实现中获取所选项目。我应该使用什么类型的监听器?似乎只有 ListDataListener 类,但似乎没有我正在寻找的功能。 感谢您的帮助! 实现如
JComboBox 从数据库中检索信息。添加数据后该列表应立即更新。 我知道我需要 fireContentsChanged 来更新值。我不确定我应该在方法中做什么。任何指导将不胜感激! class C
我对 Java 比较陌生,尤其是 Swing。我使用 Netbeans 6.8 进行开发。 我有一个使用自定义 AbstractListModel 的 JList。此 AbstractListMode
我有一个 JList,其中每个元素都包含一些特殊数据。所以我已经为 JList 实现了 AbstractListModel。 当列表中有一个或多个元素时,一切都很好。 但是,在以下情况下不起作用: J
我有一个 JList,我想为它提供我自己的 ListModel。我有一个包含条目的 LinkedHashMap。我对 AbstractListModel 进行了子类化。我想要实现的是,每当我从 Map
从如下创建的 AbstractListModel 检索数据时出现问题: warmup1List.setModel(new javax.swing.AbstractListModel() {
情况 下面是一个 Qt 示例,其中包含一个 AbstractListModel 和两个链接到该模型的显示小部件(一个 ListView 和一个 LineEdit) : from PyQt5 impor
我想定义一个 Swing 组合框模型,如下所示: public class SchemaGroupModel extends AbstractListModel implements ComboBox
我如何在 java.swing.AbstractListModel 中使用 fireContentsChanged() 方法? ?当 ListModel 的某些元素的“内容”发生变化时,将调用此方法。
我是编程新手。看来我研究得越多,我自己就越困惑,我不得不过度思考需要做什么。抽象列表模型是使 Jlist 正常工作所需要的。我需要知道什么是对/错。如果需要使用 getselectedvalues()
我的“虚拟列表”在 swing 中运行良好,但当我超过某些特定数量的项目时它似乎失败了。 “失败”是指当项目数 > Nmax 时滚动条神奇地消失,当项目数 oldLength)
我是一名优秀的程序员,十分优秀!