Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java problems 3

Status
Not open for further replies.

messi121

Programmer
Oct 17, 2010
7
0
0
GB
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.
 
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
 
Just a small correction. show() is a deprecated method, use setVisible(true) instead.
 
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!


 
You need to pass c1 in as a parameter to addField()
 
Sorry i don't understand could you show me an example possibly please?
 
Code:
public class Form extends JPanel {
[red]GridBagConstraints c1
GridBagConstraints c2[/red]
public Form () {
    super();
    
    JPanel pane = new JPanel(new GridBagLayout());
    pane.setLayout(new GridBagLayout());
     c1 = new GridBagConstraints();
     c2 = new GridBagConstraints();



_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top