textfiled, text area

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Caliraj

Thread Starter
Joined
Mar 22, 2006
Messages
145
Hi,
Given below is a simple program to make use of a label, a TextField and a button.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code=butntextlab.class width=400 height=400>
</applet>*/
public class butntextlab extends Applet implements ActionListener
{
Label l;
TextField t;
Button b1;
public void init()
{
l=new Label("Enter here");
t=new TextField(15);
b1=new Button("Click Me");
add(l);
add(t);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
l.setText(t.getText());
}
}

NOw, if i want to place the etxtfiled/label/ button in some other point x,y or in the center/left etc, how do i do it?
 
Joined
Jul 29, 2004
Messages
6,650
The three objects Button, Label, TextField inherit the getLocation() and setLocation() methods from the Component class.

Here's an example on how to get the location of a component :

Code:
[SIZE=2]
...
Button b = new Button("Ok");
this.add(b);
...
Point pt = b.getLocation();
double x = pt.getX();
double y = pt.getY();
...
[/SIZE]

Here's an example on to assign a new position to a component :

Code:
[SIZE=2]
...
Button b = new Button("Ok");
this.add(b);
...
int x = 200;
int y = 150;
b.setLocation(x,y);
b.repaint();           // to redraw the component
... 
[/SIZE]
Remark : in both examples, the coordinates x and y of the component are not related to the user's screen but to the top left corner of the object that contains the component, the frame of your applet in your case.

Also, upgrading the properties of a component during the process can't only be done while treating an event or by triggering a new thread when there's no special event to be treated.
 

Caliraj

Thread Starter
Joined
Mar 22, 2006
Messages
145
Hi chicon,
Thanks for the answer. I have remodified the program as below:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code=placement.class width=400 height=400>
</applet>*/
public class placement extends Applet implements ActionListener
{
Button b;
Label l;
TextField t;
int x,y;
public void init()
{
x=100;
y=100;
l=new Label("Enter here");
t=new TextField(10);
add(l);
add(t);
b=new Button("Click");
this.add(b);
b.setLocation(x,y);
b.repaint();
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
l.setText(t.getText());
}
}

But the result seems to be the same:confused: ...the button is not placed at position 100,100
 
Joined
Jul 29, 2004
Messages
6,650
By default, when nothing is specified in a frame, a FlowLayout is used.
Therefore, there's no possibilities to define absolute coordinates for the components as the latters are put in a sequential order.

The only way to define absolute values is using a null layout.

Remarks :
-------
1° There are some conventions to respect in Java :
- the first letter of a class is upper case and the others are lower case; example : Integer
- the first letter of a method is lower case; example : Integer.toString
- all the letters of a constant are upper case; example : Calendar.MONTH

2° When creating a GUI, personally I prefer using the JApplet extension as it provides a lot of useful tools and features coming from Java Swing
Also, it is always a good idea to use a pane (JPanel) to add some components.

Here's an example I written from your coding:

Code:
[SIZE=2]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Placement extends JApplet implements ActionListener {
    private JPanel panel = new JPanel();         [COLOR=Blue]// this is the container[/COLOR]
    private JLabel label = new JLabel();
    private JTextField text = new JTextField();
    private JButton button = new JButton();

    public void init() {
        this.label.setBounds(20,20,80,50);       [COLOR=Blue]// for each component,[/COLOR]
        this.text.setBounds(110,20,80,50);       [COLOR=Blue]// I define the coordinates x,y[/COLOR]
        this.button.setBounds(200,20,80,50);     [COLOR=Blue]// and their size width, heigth[/COLOR]
        this.label.setText("Enter here");
        this.text.setColumns(10);
        this.button.setText("Click");
        this.panel.setLayout(null);              [COLOR=Blue]// the null layout[/COLOR]
        this.panel.add(this.label);
        this.panel.add(this.text);
        this.panel.add(this.button);
        this.setContentPane(this.panel);         [COLOR=Blue]// add the container to the frame[/COLOR]
        this.button.addActionListener(this);
        this.setVisible(true);                   [COLOR=Blue]// not mandatory because it's the default value[/COLOR]
    }
    public void actionPerformed(ActionEvent ae) { [COLOR=Blue]// when the user clicks the button,[/COLOR] 
        this.label.setText(this.text.getText());  [COLOR=Blue]// the input will replace the text of the label[/COLOR]
        this.button.setLocation(100,90);          [COLOR=Blue]// and the button will jump to another place[/COLOR]
    }
}
[/SIZE]

Notice that I do not use the repaint method as it is not required in my example.
 

Caliraj

Thread Starter
Joined
Mar 22, 2006
Messages
145
Hi chicon,
Thanks again for the answer.Actually I have just started with swings.I just wanted to know how to place the labels,buttons etc in the desired locations.I have a program related to canvas, for which Im getting error.

import java.awt.*;
import java.applet.*;
/*<applet code=canny.class width=400 height=400>
</applet>*/
public class canny extends Applet
{
private canvaz obj;
public void init()
{
obj=new canvaz(200,200);
this.add(obj);
}
}
class canvaz extends Canvas
{
protected canvaz(int width,int ht)
{
super();
this.setSize(width,ht);
}
public void paint(Graphics g)
{
int width=this.getBounds().width;
int ht=this.getBounds().ht;
g.drawLine(0,0,width,ht);
g.drawLine(0,ht,width,0);
}
}

ERROR :
canny.java:24: cannot find symbol
symbol : variable ht
location: class java.awt.Rectangle
int ht=this.getBounds().ht;
Could you solve this?
Thanks,
Cali.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top