INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Java problems

Java problems

(OP)
I need to create a class that will be used to produce a form which can then be used for user input.

The form won't be used like this it will b part of a GUI.

I know what i want to do but applying all this into code is some what puzzling for me and would very much appreciate some assistance please.

The attributes i would have are:

a GridBagConstraints c1 object for the field labels,
a GridBagConstraints c2 object for the data entry fields.

My method is
 
    

CODE

public void addField(String label, JComponent field) {
      form.add(new JLabel(label),c1);
      form.add(field,c2);
   }

I also need another method to make the window visible which i am struggling with.  

RE: Java problems

This sounds like homework.
Assuming that it's not, from the little you said, there's lots of ways to go.

        setLayout(new GridLayout(1,1));
will basically let you put 1 layout in the window.

        p1 =  new Panel();
        p1.setLayout(new GridLayout(0,2));
will put a panel in that layout

        l2 = new Label();
        l2.setForeground(Color.blue);
        l3 = new Label();
        l3.setForeground(Color.magenta);
l2 and l3 are labels

        t2 = new TextField(4);
        t2.addActionListener (new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                         try {calc();}
                         catch (IOException eio) {System.out.println(eio);}
               }
          });
t2 is a text field

        p1.add(l2);
        p1.add(t2);
        p1.add(l3);
adds them to the panel

        add(p1);
puts the panel in the layout

        show();
        pack();
makes it all visible.

_________________
Bob Rashkin

RE: Java problems

Just a small correction. show() is a deprecated method, use setVisible(true) instead.

RE: Java problems

(OP)

CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Form extends JPanel {

public Form () {
    super();
    
    JPanel pane = new JPanel(new GridBagLayout());
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c1 = new GridBagConstraints();
    GridBagConstraints c2 = new GridBagConstraints();

   addField("LiNo", new JTextField(8));
   addField("name", new JTextField(20));

   
    
}

public void addField(String label, JComponent field) {
      form.add(new JLabel(label),c1);
      form.add(field,c2);
}
   


     public static void main(String[] a) {
    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    f.setContentPane(new GridBagLayoutPane());
    f.pack();
    f.setVisible(true);
  }   
   
} // end MyForm class
    

This is the code i have and when i try compiling i get the error symbol not found c1! I am confused here would appreciate some pointers please!    
    

 

RE: Java problems

You need to pass c1 in as a parameter to addField()

RE: Java problems

(OP)
Sorry i don't understand could you show me an example possibly please?

RE: Java problems

CODE

public class Form extends JPanel {
GridBagConstraints c1
GridBagConstraints c2

public Form () {
    super();
    
    JPanel pane = new JPanel(new GridBagLayout());
    pane.setLayout(new GridBagLayout());
     c1 = new GridBagConstraints();
     c2 = new GridBagConstraints();

 

_________________
Bob Rashkin

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close