Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 04:08 AM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-02-2007, 12:59 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,054
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need an example of JSR179 ((Location based Ser)implementation for CDC based device talk_to_vivekmishra CDC and Personal Profile 1 06-22-2008 10:45 PM
Hashtable example Java Tip Java Tips 0 02-15-2008 09:43 AM
Hashtable-put method new_1 New To Java 1 12-23-2007 08:07 PM
Introduction to Hashtable JavaForums Java Blogs 1 11-20-2007 10:13 PM
difference between code based security and role based security boy22 New To Java 1 07-24-2007 12:59 AM


All times are GMT +3. The time now is 08:32 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org