Results 1 to 2 of 2
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Create UI based on HashTable

    Hi, For development of an application, i have to create a stub for another application. The interface for this application is predefined, so can't touch that. Everything has to run in a Windows environment, i use Eclipse as an IDE.

    What have is the following. On several places in my application, the stub is to be called. Data-transfer between the applications is done by a packed Hashtable. Every object in the table, is a String.
    On a certain moment within the interfacing, a go() method is called.

    What i need to do, is give an implementation to this method so it opens a Window where i can edit the values in this Hashtable. The contents of this Hashtable is not the same on every call so i have to create a UI on the fly, where every key in the Hashtable is displayed by a label and every value in the Hashtable is displayed as a textbox with content.
    After this i have to refill the Hashtable with the values entered or changed in the UI.

    All interactions with the Hashtable i can manage myself, but creating a UI is another piece of cake. Which package do i use, awt or swing? How do i create a window which i fill with the neccesary labels and textboxes? My knowledge of creating UI in Java is not sufficient to do it in a little amount of time, don't have the time to study all i need to know either, so.... Any help is welcome!
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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);
        }
    }

Similar Threads

  1. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  2. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM
  3. Hashtable-put method
    By new_1 in forum New To Java
    Replies: 1
    Last Post: 12-23-2007, 07:07 PM
  4. Replies: 1
    Last Post: 07-23-2007, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •