Okay ill try that FormMaker. Are there other things i need to know before using this?
EDIT: OOps I forgot that FormMaker is your own class
This post has been edited by HopelessDev: 02 September 2012 - 05:29 AM




Posted 02 September 2012 - 05:28 AM
This post has been edited by HopelessDev: 02 September 2012 - 05:29 AM
Posted 02 September 2012 - 05:34 AM
private final String NUMBER = "Student no."; private final String NAME = "Name"; ....
package net.proteanit.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;
public class FormMaker extends JPanel {
private int rowCount;
private Map<String, JComponent> fields;
private Insets spacer;
public FormMaker() {
super();
setLayout(new GridBagLayout());
fields = new HashMap<String, JComponent>();
spacer = new Insets(0, 10, 0, 0);
}
public FormMaker(String... fields) {
this();
for (String field : fields) {
this.addField(field);
}
}
public void addField(String label, String key) {
if (fields.containsKey(key)) {
throw new IllegalArgumentException(String.format(
"Key '%s' already used. Choose another value or use addField(String label, String key).",
key));
}
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = rowCount;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 0;
JLabel l = new JLabel(label);
add(l, gbc);
gbc.gridx = 1;
gbc.weightx = 1.0;
gbc.insets = spacer;
gbc.fill = GridBagConstraints.HORIZONTAL;
JTextField tf = new JTextField();
add(tf, gbc);
rowCount++;
fields.put(key, tf);
}
/**
* @param name
* The field label and key for the JTextField
*/
public void addField(String name) {
addField(name, name);
}
public void addFields(String fields) {
for (String s : fields.split("\\s*,\\s*")) {
addField(s);
}
}
public JComponent getField(String name) {
return fields.get(name);
}
public String getText(String name) {
return ((JTextComponent) fields.get(name)).getText();
}
}
This post has been edited by g00se: 02 September 2012 - 05:37 AM
Posted 02 September 2012 - 05:58 AM
Posted 03 September 2012 - 06:21 AM
Posted 03 September 2012 - 06:26 AM
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
