【问题标题】:How to populate the combobox by keeping the details in properties file如何通过将详细信息保留在属性文件中来填充组合框
【发布时间】:2017-06-30 09:56:36
【问题描述】:

如何通过将详细信息保留在属性文件中来填充组合框..非常困惑

static void config(String component, String environment) {     //Function for config file

    Properties prop = new Properties();
    InputStream input = null;

    try {



        input = new FileInputStream("C://Users//");
        prop.load(input);                                  // load a properties file
        Set<Object> keys = prop.keySet();
        String    key;
        Iterator<Object> iter = keys.iterator();


        while (iter.hasNext()) {
            key = (String) iter.next();

            System.out.println("Key: " + key);

            if (key.equals(component + "." + "basefilename")) 
            {
                filename = prop.getProperty(key);
            }

            if (key.startsWith(component + "." + environment + "." + "url"))
            {
                urls.add(prop.getProperty(key));
            }

        }

        System.out.println("Config Values");
        System.out.println("     Filename:" + filename);
        System.out.println("  URL's   :" +    urls.toString());

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 

}

static Hashtable<String, String> Validation() {   //function for UI    details

    {
        Hashtable<String, String> logininformation = new    Hashtable<String, String>();

        JPanel panel = new JPanel(new BorderLayout(5, 5));   //UI Creation
        JPanel label = new JPanel(new GridLayout(0, 1, 2, 2));
        label.add(new JLabel("User", SwingConstants.RIGHT));
        label.add(new JLabel("Password", SwingConstants.RIGHT));
        label.add(new JLabel("Component", SwingConstants.RIGHT));
        label.add(new JLabel("Environment", SwingConstants.RIGHT));
        panel.add(label, BorderLayout.WEST);

        JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
        JTextField username = new JTextField();
        controls.add(username);
        JPasswordField password = new JPasswordField();
        controls.add(password);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ***String[] Componentstrings = { "dog", "lion", "tiger",    "elephant", };                //components
        String[]    Environmentstrings = { "Produktiv", "prelife","};     //environments
        ***these contents to be in properties file and to be    populated during execution**
        @SuppressWarnings("unchecked")
        JComboBox componentList = new JComboBox(Componentstrings);   //combo box for components
        controls.add(componentList);
        panel.add(controls,    BorderLayout.CENTER);

        @SuppressWarnings("unchecked")
        JComboBox EnvironmentList = new    JComboBox(Environmentstrings);   //combo box for environments
        controls.add(EnvironmentList);
        panel.add(controls,    BorderLayout.CENTER);

        Component frame1 = null;
        JOptionPane.showMessageDialog(frame1,    panel, "login", JOptionPane.QUESTION_MESSAGE);

        logininformation.put("user", username.getText());
        logininformation.put("pass", new String(password.getPassword()));
        logininformation.put("component", (String)    componentList.getSelectedItem());
        logininformation.put("environment", (String)    EnvironmentList.getSelectedItem());

        System.out.println("UI values:" + logininformation);

        return logininformation;

    }
}

【问题讨论】:

  • 不要在每一行使用“>”。只需选择您的代码,然后使用{} 按钮来保持您发布的代码的格式。
  • 您只需遍历属性文件,然后对找到的每个属性使用JComboBox.addItem(...) 方法。

标签: java arrays swing properties-file


【解决方案1】:

您应该实现一个使用Properties 作为其数据容器的ComboBoxModel

编辑

试试这样的:

public class PropertyComboModel extends AbstractListModel<Object>
{
  private static final long serialVersionUID    = -4409288126984703346L;
  private Properties            data;
  public void setData(Properties data)
  {
    this.data = data;
  }
  public Properties getData()
  {
    return data;
  }
  @Override
  public int getSize()
  {
    return data.size();
  }
  @Override
  public Object getElementAt(int index)
  {
    final Enumeration<Object> e = data.keys();
    for (int i = index; i > 0; --i)
      e.nextElement();
    return (Object) e.nextElement();
  }
}

【讨论】:

  • 请问怎么做
猜你喜欢
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2011-05-14
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多