View Single Post
  #2 (permalink)  
Old 08-02-2007, 01:59 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class EditDialog implements ActionListener { JDialog dialog; Hashtable<String,String> hashtable; List<JLabel> labelList = new ArrayList<JLabel>(); List<JTextField> fieldList = new ArrayList<JTextField>(); public EditDialog(Hashtable<String,String> hashtable) { this.hashtable = hashtable; showDialog(); } private void showDialog() { boolean modal = false; dialog = new JDialog(new JFrame(), "Edit Dialog", modal); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); Container cp = dialog.getContentPane(); cp.add(getDataFields()); cp.add(getButtonPanel(), "Last"); dialog.setSize(500,400); dialog.setLocationRelativeTo(null); dialog.setVisible(true); } public void actionPerformed(ActionEvent e) { saveEdits(); dialog.dispose(); } private void saveEdits() { Set keys = hashtable.keySet(); Iterator it = keys.iterator(); while(it.hasNext()) { String key = toString(it.next()); String value = getValueFor(key); hashtable.put(key, value); } } private String getValueFor(String s) { for(int j = 0; j < labelList.size(); j++) { JLabel label = (JLabel)labelList.get(j); if(label.getText().equals(s)) { return ((JTextField)fieldList.get(j)).getText().trim(); } } return null; } private JScrollPane getDataFields() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weighty = 1.0; Set keys = hashtable.keySet(); Iterator it = keys.iterator(); while(it.hasNext()) { Object key = it.next(); Object value = hashtable.get(key); addRow(key, value, panel, gbc); } return new JScrollPane(panel); } private void addRow(Object key, Object value, Container c, GridBagConstraints gbc) { int alignment = JLabel.RIGHT; // LEFT, CENTER JLabel label = new JLabel(toString(key), alignment); labelList.add(label); JTextField tf = new JTextField(toString(value)); fieldList.add(tf); gbc.weightx = 0.10; // relative width of labels gbc.fill = GridBagConstraints.NONE; gbc.gridwidth = GridBagConstraints.RELATIVE; gbc.anchor = GridBagConstraints.EAST; c.add(label, gbc); gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.anchor = GridBagConstraints.WEST; c.add(tf, gbc); } private String toString(Object o) { return (o instanceof String) ? (String)o : o.toString(); } private JPanel getButtonPanel() { JButton button = new JButton("save edits"); button.addActionListener(this); JPanel panel = new JPanel(); panel.add(button); return panel; } public static void main(String[] args) { Hashtable<String,String> ht = new Hashtable<String,String>(); Random r = new Random(); for(int j = 0; j < 12; j++) ht.put(getString(r), getString(r)); new EditDialog(ht); } private static String getString(Random r) { String s = "abcdefghijklmnopqrstuvwxyz"; int length = 5 + r.nextInt(11); int start = r.nextInt(s.length() - length); return s.substring(start, start+length); } }
Reply With Quote