applet problem

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2220 Views - Last Post: 03 September 2012 - 06:26 AM Rate Topic: -----

#16 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 05:28 AM

Yes it worked. But with the hyperlinks so I removed it and its running fine. thanks.

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

Was This Post Helpful? 0
  • +
  • -

#17 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 05:34 AM

I'll post it here for you. I'm not going to support it, but i would advise you to do the following:

private final String NUMBER = "Student no.";
private final String NAME = "Name";
....

This is by no means production-ready, but will save a lot of hassle:
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

Was This Post Helpful? 1
  • +
  • -

#18 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 02 September 2012 - 05:58 AM

I will study this later. Thank you for posting. I also would like to show you something, GroupLayouts. http://docs.oracle.c...roupLayout.html

I found it just now and it fixed my problem. It aligned the labels and the fields also leveled the height of the textfields to match the labels. The code was so easy to implement even for a novice like me.
Was This Post Helpful? 0
  • +
  • -

#19 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • Joined: 20-September 08

Re: applet problem

Posted 02 September 2012 - 09:43 AM

Yes, i was going to mention GroupLayout too. Did you use a gui builder or code it by hand?
Was This Post Helpful? 0
  • +
  • -

#20 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 136
  • Joined: 10-August 12

Re: applet problem

Posted 03 September 2012 - 06:21 AM

I code it by hand. Those gui builders, are they worth learning?
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,367
  • Joined: 27-December 08

Re: applet problem

Posted 03 September 2012 - 06:26 AM

No. Avoid the GUI Builders. They're more hassle than they're worth, as the code they produce is horrible.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2