【发布时间】: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