- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有组合框的面板。我正在向组合框添加 ID 号。但在添加任何 id 之前,我添加一个空字符串来知道没有选择 id。
JComboBox jcPatientIds;
JPanel patientPanel;
public void updateIdsInComboBoxes(ArrayList<Integer> items)
{
getJcPatientIds().addItem("");
if (items.size() > 0)
{
for (Integer item : items)
{
getJcPatientIds().addItem(item);
}
}
getPatientPanel().add(getJcPatientIds());
}
public JComboBox getJcPatientIds()
{
if(jcPatientIds == null)
{
jcPatientIds = new JComboBox();
jcPatientIds.setBounds(250, 20, 100, 20);
}
return jcPatientIds;
}
public JPanel getPatientPanel()
{
if(patientPanel == null)
{
patientPanel = new JPanel();
}
return patientPanel;
}
现在,我有这样的监听器类。而且它工作正常,没有出现任何错误
@Override
public void itemStateChanged(ItemEvent e)
{
/*The event triggers for both deselected and selected item
Here only the selected event will be triggered with value 1
deselected event has a value 2*/
if (e.getStateChange() == 1)
{
if (e.getItem().equals(""))
{
// Some stuff
}
else
{
System.out.println((int) e.getItem());
// Some other stuff
}
}
}
但是,如果我在 Combobox 上使用 getSelectedItem。我收到 ClassCastException。这里有什么问题
@Override
public void itemStateChanged(ItemEvent e)
{
if (getJcPatientIds.getSelectedItem().equals(""))
{
// Some stuff
}
else
{
System.out.println((int) getJcPatientIds.getSelectedItem()); // Here I am getting ClassCastException
// Some other stuff
}
}
}
错误
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at hospitalApp.common.PatientController.itemStateChanged(PatientController.java:71)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1225)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1273)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:578)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:835)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:499)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
最佳答案
同样,我会避免用整数和字符串填充组合框。坚持使用 Integer,实际上将 JComboBox 声明为使用 Integer 作为其通用类型的通用对象:JComboBox<Integer>
。为空元素添加 null,然后检查 null:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class Foo2 extends JPanel {
DefaultComboBoxModel<Integer> jcPtIdModel = new DefaultComboBoxModel<>();
JComboBox<Integer> jcPtIdCombo = new JComboBox<>(jcPtIdModel);
public Foo2(List<Integer> ids) {
if (ids != null) {
updateIds(ids);
}
add(jcPtIdCombo);
jcPtIdCombo.addItemListener(new MyItemListener());
}
public void updateIds(List<Integer> ids) {
jcPtIdModel.removeAllElements();
jcPtIdModel.addElement(null);
for (Integer id : ids) {
jcPtIdModel.addElement(id);
}
}
private class MyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
//!! if (jcPatientIds.getSelectedItem().equals("")) {
if (jcPtIdCombo.getSelectedItem() == null) {
// Some stuff
} else {
// Here I am getting ClassCastException
System.out.println((int) jcPtIdCombo.getSelectedItem());
// Some other stuff
}
}
}
private static void createAndShowGui() {
ArrayList<Integer> items = new ArrayList<>();
for (int i = 0; i < 10; i++) {
items.add(i);
}
Foo2 mainPanel = new Foo2(items);
JFrame frame = new JFrame("Foo2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
我还看到您使用 setBounds(...)
我强烈建议你不要这样做。而空布局和 setBounds()
对于 Swing 新手来说,Swing GUI 似乎是创建复杂 GUI 的最简单、最好的方法,您创建的 Swing GUI 越多,在使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,放置在滚动 Pane 中时它们完全失败,在所有平台或与原始分辨率不同的屏幕分辨率上查看时,它们看起来非常糟糕.
关于java - 转换对象时 event.getItem() 和comboBox.getSelectedItem() 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31204613/
我是 Java 新手,所以如果这是一个愚蠢的问题,请原谅我。我得到了这门课: public class CustomDrawerAdapter extends ArrayAdapter { Conte
我知道还有其他几篇关于此主题的帖子,但我想粘贴我的代码,因为我相信其中可能存在导致我出现问题的错误。我的 FragmentStatePagerAdapter 返回了错误的位置,我不确定为什么。 这是我
因为我正在为我的应用程序使用自定义 listView 适配器,所以一些默认方法被覆盖了。当我调用适配器的 getItem(position) 方法时出现问题。由于此方法被重写,我试图找出所选项的结果返
我正在尝试为菜单设置事件项目 cookie。 所以我使用本地存储来完成此任务。当菜单项处于事件状态时,它会像这样存储在本地存储中 - $(function () {
我正在编写一个简单的优惠券应用程序,但最近我更新了一些依赖项和我的 Fragment不允许我 return null 错误照片: package com.szakes1.makdolannative.
我正在为我的单选按钮使用 ItemListener。我看到了很多 ItemListener 函数,但我的似乎工作方式不同。 ... jUserButton2.addItemListene
我试图从特定的 SWT 树中获取所有项目(并从它们创建一个 zk 树 - 并不重要)。问题是我只能获得树的第一个项目,而不能更深入。我使用 tree.getItems() 获取第一个项目,但是当我使用
我想覆盖在文件\administrator\components\com_content\models\article.php 中找到的方法 getItem() 第 257 行 public func
我为自定义 ListView 创建了一个自定义数组适配器,其中包含 TextView 和复选框。在我的自定义适配器中,我创建了一个名为 isChecked() 的方法 - static cla
我正在尝试在 View 页面中显示一些 fragment 。我使用从 NavigationDrawer 启动 PlanFragment FragmentTransaction transaction
我使用以下代码行将一些值存储到本地存储中; localStorage.setItem(event.lastEventId, JSON.stringify(data)); 查看浏览器控制台,存储如下所示
我目前正在学习基本的 JavaScript 类(class),有一个例子,这让我很困惑,因为 getItem() 函数似乎可以有两种返回类型。 if(!localStorage.getItem('na
设置:我想编写一个方法,该方法将采用嵌套数据对象和路径字符串,并尝试使用路径组件取消引用数据对象内的位置。 例如,您有一个类似 /alpha/bravo/0/charlie 的路径,该方法将返回 da
我试图从 ListView 中获取项目的值,但是当我尝试“adapter.getItem(position)”时收到转换错误。 根据日志,我假设 getItem 函数是发生错误的地方,但我不确定我还能
我有一个 MainActivity控制四个 fragment ,每个 fragment 都是一个选项卡。当我的主要 Activity 开始时,我在日志中打印了一行,以显示正在实例化的 fragment
我很难尝试让我的 ArrayAdapter 工作 - 之前使用自定义 ArrayAdapter 没有问题,但这次我不确定发生了什么。 我尝试使用适配器创建 ListView,它将显示每个 WeekVi
我在public View getView(int position, View convertView, ViewGroup parent) 中有一个带有以下(测试)代码的适配器: Curs
我的代码中有一个列表 ListView ,其中包含 2 个按钮和 2 个 TextView。我想通过单击按钮访问每个 textView 的文本。这是适配器的代码... public class Tra
这是我的代码 public static class TestPagerAdapter extends FragmentPagerAdapter { Context mContext;
这个问题已经有答案了: What do querySelectorAll and getElementsBy* methods return? (12 个回答) 已关闭 8 年前。 我的页面上有多个输
我是一名优秀的程序员,十分优秀!