- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 swing 中的 jcombobox 并从 getSelectedIndex 方法中获得一些意外的输出。当我选择第一个 JComboBox 时,第二个组合框将被启用,并且我正在使用 getSelectedIndex() 方法,该方法显示一些意外的输出。我的意思是它应该显示“0”,但它显示“0”,然后“-1”,然后是异常,最后是“0”。我无法找出异常的原因。我在这里只展示了部分代码希望你们能理解我......
public Tester() {
super("Utility for publish_date");
Container contentpane = getContentPane();
contentpane.setLayout(new BorderLayout());
try{
conn = new MyConn().getMyConnection();
s = conn.createStatement();
rs = s.executeQuery("select distinct state_name , state_code from state_master where state_code in (select statecode from pdf_detail) order by state_name");
statelist.add(0,"Select state");
while(rs.next())
{
statelist.add(rs.getString(1));
statecodelist.add(rs.getString(2));
}
System.out.println("the content of statelist"+ statelist);
System.out.println("the content of statecodelist"+statecodelist);
list_state = new String [statelist.size()];
statelist.toArray(list_state);
}catch(Exception e)
{
System.out.println(e);
log(e.getMessage());
}
combo = new JComboBox(list_state);
combo.setBounds(50, 50, 100, 20);
combo.addActionListener(this);
combo1 = new JComboBox();
combo1.addActionListener(this);
combo1.setEnabled(true);
combo1.setBounds(50, 100, 100, 20);
combo2 = new JComboBox();
combo2.addActionListener(this);
combo2.setEnabled(true);
combo2.setBounds(50, 100, 100, 20);
JPanel panel1 = new JPanel(new FlowLayout());
panel1.add(combo);
panel1.add(combo1);
panel1.add(combo2);
contentpane.add(panel1 , BorderLayout.NORTH);
label = new JLabel();
label.setText("Choose publish Date by selecting below.");
final SimpleDateFormat dateformater = new SimpleDateFormat("yyyy-MM-dd");
datepicker = new JXDatePicker();
datepicker.setFormats(dateformater);
datepicker.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//label.setText(datepicker.getDate().toString());
System.out.println("new date is "+dateformater.format(datepicker.getDate()));
publish_date = dateformater.format(datepicker.getDate());
}
});
JPanel panel2 = new JPanel(new FlowLayout());
panel2.add(label);
panel2.add(datepicker);
contentpane.add(panel2 , BorderLayout.CENTER);
pbutton = new JButton("Submit");
pbutton.addActionListener(this);
JPanel panel3 = new JPanel();
panel3.add(pbutton);
contentpane.add(panel3 , BorderLayout.SOUTH);
setSize(600,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent a)
{
if (a.getSource() == combo) {
statename = (String)combo.getSelectedItem();
System.out.println("statename======="+statename);
q = combo.getSelectedIndex();
System.out.println("....q....."+q);
System.out.println(",,,,,,,,,,"+statecodelist.get(q-1));
try{
districtcodelist.clear();
ResultSet rs2 =s.executeQuery("select distinct district_name , district_code from district_master where state_code= '"+statecodelist.get(q-1)+"' AND district_code in (select districtcode from pdf_detail where statecode = '"+statecodelist.get(q-1)+"') order by district_name ");
//districtlist.add(0, "Select district");
while(rs2.next())
{
districtlist.add(rs2.getString(1));
districtcodelist.add(rs2.getString(2));
}
System.out.println("the content of districtlist"+ districtlist);
System.out.println("the content of districtcodelist"+districtcodelist);
list_district = new String [districtlist.size()];
districtlist.toArray(list_district);
}catch(Exception f)
{
f.printStackTrace();
System.out.println(f);
log(f.getMessage());
}
//combo1.setEnabled(true);
combo1.removeAllItems();
for (int i = 0; i < list_district.length; i++) {
combo1.removeItem(list_district[i]);
combo1.addItem(list_district[i]);
}
districtlist.clear();
}
else if (a.getSource() == combo1)
{
// System.out.println("combo1");
districtname = (String)combo1.getSelectedItem();
System.out.println("districtname========"+districtname);
w = combo1.getSelectedIndex();
if(w == -1)
{
System.out.println("hre is the prob");
System.out.println(combo1.getSelectedItem());
}
System.out.println(".....w...."+w);
System.out.println(combo1.getSelectedItem());
//System.out.println(",,,,,,,,,,"+districtcodelist.get(w));
try{
//tehsillist.add(0,"Select tehsil");
ResultSet rs3 = s.executeQuery("select distinct tehsil_name , tehsil_code from tehsil_master where state_code= '"+statecodelist.get(q-1)+"' AND district_code='"+districtcodelist.get(w)+"' AND tehsil_code in ( select tehsilcode from pdf_detail where statecode = '"+statecodelist.get(q-1)+"' AND districtcode = '"+districtcodelist.get(w)+"') order by tehsil_name");
while(rs3.next())
{
tehsillist.add(rs3.getString(1));
}
list_tehsil = new String [tehsillist.size()];
tehsillist.toArray(list_tehsil);
}catch(Exception g)
{
g.printStackTrace();
log(g.getMessage());
System.out.println(g);
}
//combo2.setEnabled(true);
combo2.removeAllItems();
for (int i = 0; i < list_tehsil.length; i++) {
combo2.removeItem(list_tehsil[i]);
combo2.addItem(list_tehsil[i]);
}
tehsillist.clear();
}
if(a.getSource() == pbutton){
tehsilname = (String)combo2.getSelectedItem();
e = combo2.getSelectedIndex();
System.out.println("....e....."+e);
System.out.println("....w....."+w);
if((String)combo.getSelectedItem()== "Select state" || (String)combo1.getSelectedItem()== "Select district" || (String)combo2.getSelectedItem()== "Select tehsil" || publish_date == null )
{
JOptionPane.showMessageDialog(this, "Select all four items first.");
}
else
{
int c = JOptionPane.showConfirmDialog(this,"Do you really want to continue?","update publish date?",JOptionPane.YES_NO_OPTION);
if(c==0)
{
log("State is "+statename+" \nDistrict is "+districtname+" \nTehsil is "+tehsilname);
try{
Properties pr =new Properties();
pr.load(new FileInputStream("config/config.properties"));
//String publish_date = pr.getProperty("publish_date");
ResultSet rs4 = s.executeQuery("select publish_date from pdf_detail where statecode = (select state_code from state_master where state_name = '"+statename+"') AND districtcode = (select district_code from district_master where district_name = '"+districtname+"') AND tehsilcode = (select tehsil_code from tehsil_master where tehsil_name = '"+tehsilname+"') ");
while(rs4.next())
{
testvalue = rs4.getString(1);
}
if(testvalue != null)
{
n = JOptionPane.showConfirmDialog(this,"Publish_date is already assigned to this tehsil. Do you want to update it?","update publish date?",JOptionPane.YES_NO_OPTION);
System.out.println("the value of n is "+n);
}
if(testvalue == null || n == 0)
{
JOptionPane.showMessageDialog(this, "Publish_date updated");
s.executeUpdate("Update pdf_detail set publish_date = '"+publish_date+"'where statecode=(select state_code from state_master where state_name='"+statename+"') AND districtcode=(select district_code from district_master where district_name='"+districtname+"') AND tehsilcode=(select tehsil_code from tehsil_master where tehsil_name='"+tehsilname+"')");
log("publish date of "+tehsilname+" is updated from ("+testvalue+") to ("+publish_date+")");
}
}catch(Exception e){
log(e.getMessage());
System.out.println(e);}
if(n==1)
{
JOptionPane.showMessageDialog(this, "Pulish_date updation canceled");
log("publish_date process canceled as it is already done");
}
JOptionPane.showMessageDialog(this, "Proces completed");
log("process completed at");
log("------------------------------------------------------------------------");
excelLog();
}
else{
setVisible(false);
(new Publish_Date()).setVisible(true);
}
}
}
}
public static void main(String args[]) {
log("process start time");
(new Tester()).setVisible(true);
}
}
输出:
....q.....1
.....w....0
....q.....1
.....w....-1
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(Unknown Source)
at com.Publish_Date.itemStateChanged(Publish_Date.java:215)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeElementAt(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeElement(Unknown Source)
at javax.swing.JComboBox.removeItem(Unknown Source)
at com.Publish_Date.itemStateChanged(Publish_Date.java:190)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.ArrayIndexOutOfBoundsException: -1
.....w....0
最佳答案
当没有选择任何项目时,JComboBox 的选定索引为 -1。如果选择了第二个复选框中的某个项目,并且更改了第一个复选框中的选择,则第二个复选框中的所有项目都会被 combo1.removeAllItems();
删除,这将导致该复选框更改其选择为 -1 并触发一个事件。
您很可能可以检测到 -1 值并忽略该事件或进行一些清理。
关于java - swing jcombobox 中的意外输出 getSelectedIndex 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11445899/
我正在尝试组合 2 个 jcomboboxes。 1 组合框用于显示费用类别。第二个组合框正在从文本文件中读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框会根据用户在第一个组合框中选
我希望一个 JComboBox 的值根据另一个 JComboBox 的值进行更改。在一个 JComboBox 中,我显示了从数据库检索的医生名称。根据该名称,我想在另一个 JComboBox 中显示医
我有课 public class Speciality { String specName; String eduProgram; } 和 TreeMap Map> treeM
我有一个 JComboBox。我希望它能够工作,以便如果选择某个项目(“其他”),立即在同一个组合框中显示更多项目(类似于子菜单,但在组合框内)。我很难让这个工作发挥作用。 有人对如何做到这一点有任何
如何从 JComboBox 中进行选择,删除或添加选择到另一个 JComboBox? 更具体地说,我有一个 JComboBox,其中包含一年中的月份作为选择。我还有另一个,里面有这个月的日子。虽然 3
我有一个用例(在 Java Swing 中),其中某个 JTable 列必须可以通过 JComboBox 编辑器进行编辑,但可选择的值列表取决于该行的域对象(我使用自定义 TableModel)。 C
我们可以在执行 jFrame 后根据点击另一个 jcomboBox 的项目来启用 jcomboBox 吗?这可能吗? 最佳答案 JComboBox comboBox_1 = new JComboBox
我有 7 个 jcombobox,它们都以相同的默认选择开头。如果在前面的 jcombobox 中选择了默认值以外的内容,如何启用下一个? if ( ! (custData1.equa
我有 2 个 JComboBox,在第一个 JComboBox 上选择一个项目后,第二个会从数据库中填充。问题是,每次我输入一个字母时,第二个 jcombobox 都会填充。我想让第二个 jcombo
您好,我正在尝试将一个对象从一个组合框传输到另一个组合框,反之亦然。为了实现这一目标,我使用 actionListeners 或 ItemListeners,不幸的是它们没有回答我的问题,或者可能只是
当我从 JComboBox A 中选择项目时,我尝试在 JComboBox B 中显示值。到目前为止,当我从 JComboBox A 中选择一个值时,没有任何反应。这是我的数据和代码。例如,如果我从
我写了这个简单的程序: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tab
我正在制作一个医疗药物软件程序,我希望用户能够从由类别数组填充的 jcombo 框中选择疾病类别(呼吸系统疾病、心血管疾病等)。 选择此类别后,我希望另一个 jcombobox 仅填充与该类别相关的药
这段代码很草率,我也欢迎一些反馈。 我正在尝试根据另一个 JComboBox 的值更改 JComboBox 的值。还有一个额外的复杂性,因为我使用一个额外的类来确定要返回的字符串数组(请参阅我之前的问
我已经加载了对象的 JComboBox,这些对象有自己的“toString”方法。该组合框用于选择并通过该组合框上的“ActionListener”返回该对象。一切工作正常,直到我决定添加通过在此组合
所以我的问题是 JComboBoxs 和 ActionListeners。我将制作一个新的简化代码来尝试代表我的原始代码中的问题。我想要一个 JComboBox 添加一个 JComboBox,然后 J
我的转换器程序中有 3 个 JCombobox,其中一个用于类别,两个用于选择要执行的功能;我希望程序做的是,当用户在一个 JCombobox 中选择一个类别时,两个 JCombobox 将自动更改与
所以,这就是问题所在:我需要 3 个或更多 jComboBox(都具有相同的项目)相互连接,这样当我在任何 jComboBox 中选择一个项目时,该项目将在所有其他 jComboBox 中消失。具体例
我用这样的东西制作了一个图形用户界面: String[] days29 = {"1",....."29"}; String[] days30 = {"1",....."30"}; String[] d
我有一个小应用程序,它通过 JPA 从 MySQL 数据库生成统计图表。为了选择要包含在统计信息中的数据库组件,我安装了 2 个 JComboBoxes。第一个 JComboBox 填充了 Categ
我是一名优秀的程序员,十分优秀!