如何通过将详细信息保存在属性文件中来填充组合框。非常困惑
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;
}
}
答案 0 :(得分:0)
您应该实现使用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();
}
}