2014-10-31 140 views
0

我有以下四个字段作为服务器名,端口号,用户名,密码添加值从组合框文本框

登录表单我有除此之外的登录表单应该允许用户选择最后一个组合框成功的用户名(电子邮件地址)能够将(服务器名,端口号,用户名,密码)的值提取到我的文本字段中。我完成了在组合框中输入用户名的工作。

一旦用户在组合框中选择用户名,我需要将关于该用户名的值放入我的四个文本框中。我需要关于能够制定actionListener和Performed函数的帮助。我不完全确定下面的那些。任何帮助,将不胜感激。谢谢!!

//Two lines down below, those are my values that I want to assign to its respective text field 

String user_name, server_name, port_number; // I have got the values 
Char [] password; // I have got its value 


JTextField serverText, portText, usernameText; // Those are my JTextField names to set values in it 
JTextField passwordText; 


    final JComboBox<String> comboBox = new JComboBox<String>(); 
    comboBox.addItem(user_name); // Putting that username in the combo-box 
    comboBox.setBounds(500, -13, 230, 70); 
    panel.add(comboBox); 

    comboBox.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent event){ 

      procedure = (String) comboBox.getSelectedItem(); 
      private void itemStateChanged(ItemEvent e) { 
       if(e.getStateChange()) == ItemEvent.SELECTED){ 
        usernameText.setText(user_name); 
       ....... 
       } 

     } 
     } 
}); 
+0

有一些关于你的代码的东西,没有任何意义,可以考虑提供一个[可运行示例](https://stackoverflow.com/help/mcve),它可以说明你的问题。这会减少混淆和更好的响应 – MadProgrammer 2014-10-31 03:21:34

+0

没有可运行的示例,因为我无法这样做。我只想学习如何做这个动作。放在那里的代码是一种模板。 – 2014-10-31 04:00:44

+0

那么,你不需要'ActionListener'中的'itemStateChanged'监听器。如果你想学习如何做到这一点,我建议写一些代码并尝试一下 – MadProgrammer 2014-10-31 04:02:14

回答

0

这种方式适用于以后有人需要它的情况。由于这是一个动作,我不想镜像组合框本身的任何更改,所以我决定使用ActionListener而不是ItemListener

comboBox.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e){ 
      { 
       serverText.setText((String) item.getServerName()); 
       portText.setText((String) item.getPortNumber()); 
       usernameText.setText((String) item.getUserName()); 
       passwordText.setText((String) item.getPassword()); 
     } 
    } 
});