当我尝试从Select事件的组合框中获取选定值时,它没有返回任何值。
ProcessFile1()
{
Process=new Button("Process File");
Process.setBackground(new Color(164,108,153));
File_Name=new Label("Enter FileName");
TxtFileName = new TextField(20);
String FileName =new String(TxtFileName.getText().trim());
setSize(600,600);
setLocation(400,100);
addWindowListener(new ProcessFile1.WindowEventHandler());
setLayout(new GridLayout(8,1));
Panel p=new Panel();
p.add(File_Name);p.add(TxtFileName);add(p);
String fileName= File_Name.getText().trim();
Panel panel = new Panel();
panel.add(new JLabel("Please make a selection:"));
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement("Please Select One Application");
model.addElement("GPS");
model.addElement("IBS");
model.addElement("BVT Tool");
JComboBox File_Combo = new JComboBox(model);
File_Combo.setBackground(new Color(181,81,129));
panel.add(File_Combo);
add(panel);
System.out.println(value);
Panel p1= new Panel();
add(p1);
p1.add(Process);
Process.addActionListener(this);
File_Combo.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String name;
System.out.println("Inside actionevent");
if(ae.getSource().equals(Process))
{
System.out.println("Working properly");
ItemSelectable is;
is = (ItemSelectable)ae.getSource();
name = selectedString(is).trim();
System.out.println(name);
switch (name) {
case "IBS":
System.out.println("Inside IBS");
/*try
{
String[] command = new String[] {"mv /bgw/feeds/ibs/FileName /bgw/feeds/ibs/incoming/"};
Runtime.getRuntime().exec("command");
}catch(Exception e)
{
System.out.println("execption is :"+ e);
e.printStackTrace();
}*/break;
case "BVT Tool":
System.out.println("Inside BVT Tool");
/*try
{
String[] command1 = new String[] {"mv /bgw/feeds/ibs/FileName /bgw/feeds/ibs/incoming/"};
Runtime.getRuntime().exec("command1");
}catch(Exception e)
{
System.out.println("execption is :"+ e);
e.printStackTrace();
}*/break;
default:
System.out.println("Inside GPS");
try
{
String[] command2 = new String[] {"mv /bgw/feeds/ibs/$File_Name /bgw/feeds/ibs/incoming/"};
Runtime.getRuntime().exec(command2);
}catch(Exception e)
{
System.out.println("execption is :"+ e);
e.printStackTrace();
}
break;
}
}
}
但是点击组合框无法从组合框中获取所选值。请帮助我找到我失踪的地方以获得所选值。
答案 0 :(得分:2)
你应该在组合框上使用ItemListener
而不是动作监听器
JComboBox c = new JComboBox ();
c.addItem ("Up");
c.addItem ("Down");
c.addItem ("Strange");
c.addItem ("Charm");
c.addItem ("Top");
c.addItem ("Bottom");
c.addItemListener (this);
/** Get the combobox item events here. **/
public void itemStateChanged (ItemEvent e) {
String command = e.getItem ().toString ();
if (command.equals ("Charm") )
System.out.println(command);;
}
答案 1 :(得分:1)
在actionPerformed
方法中,您只处理由ActionEvent
执行的来自Process
的来源的操作。您可以在自己的下方添加另一个条件:
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(Process)){
...
} else if (ae.getSource().equals(File_Combo){
...
}
}