[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

could not view components....

this is a code which i wrote using swing ....
CODE:
----------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextFieldDemo1 extends JFrame  {
    public Container container;
    JTextField textfield1,textfield2;

    public TextFieldDemo1()  {
        container= this.getContentPane();
container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));
textfield1=new JTextField("",20);
textfield1.addActionListener(new TextFieldListener());
textfield1.setBorder(BorderFactory.createLineBorder(Color.black));

 textfield2=new JTextField();
textfield2.setBorder(BorderFactory.createLineBorder(Color.black));

        JButton button=new JButton("Clear");
        button.addActionListener(new ButtonListener());

        container.add(Box.createVerticalGlue());
        container.add(textfield1);
        container.add(Box.createVerticalGlue());
        container.add(textfield2);
        container.add(Box.createVerticalGlue());
        container.add(button);
        container.add(Box.createVerticalGlue());
    }//end constructor

    class TextFieldListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            textfield2.setText(e.getActionCommand());
        }
    }

    class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            textfield1.setText("");
            textfield1.requestFocus();
            textfield2.setText("");
        }
    }


    public static void main(String args[]){

        TextFieldDemo1 text=new TextFieldDemo1();
text.addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
        text.setTitle("Text Field Demo");
        text.setSize(300,400);
        text.setVisible(true);
    }//end main
}
----------------------------------
now the problem is that even if i have given the
createVerticalGlue() ...... it does not give any space between the textfields .. which results in a  very wide textfields ....
how can i leave some space in between the textfields and button...???
    thanx ..subhro
and
0
subhro
Asked:
subhro
  • 3
1 Solution
 
vladi21Commented:
       container.add(textfield1);
        container.add(Box.createRigidArea(new Dimension(0,5)));
        container.add(textfield2);
        container.add(Box.createRigidArea(new Dimension(0,5)));

or specify size for textfields
0
 
subhroAuthor Commented:
thanx ... actually that is what i did after i had posted the question .. it did work ... but the question still remains why .. createVerticalGlue does not work ....
anyways thanx .... subhro
0
 
vladi21Commented:
its work when textarea have fixed size
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now