user9579621
user9579621

Reputation:

How to add JTextField

I want to add JTextField. My JFrame contains a JButton and a JTextField . First, I have added the JButton, and it is working. But JTextField is not seen.

import java.awt.*;import java.awt.event.*import java.awt.geom.*;import javax.swing.*;import java.sql.*;import java.util.Random;import java.awt.geom.*;    
public class DrawMap extends JFrame{//private JLabel LabelTitle,MapNo;Private JTextField MapField;private final JButton Load,Back,Logout;private final JPanel DrawPanel;private String UserName,Password,City;public boolean check;private int r,g,b;private int flag =0;private Shape shape;private final int w = 20;
    

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.sql.*;
    import java.util.Random;
    import java.awt.geom.*;

    public class DrawMap extends JFrame{
    //private JLabel LabelTitle,MapNo;
    private JTextField MapField;
    private final JButton Load,Back,Logout;
    private final JPanel DrawPanel;
    private String UserName,Password,City;
    public boolean check;
    private int r,g,b;
    private int flag =0;
    private Shape shape;
    private final int w = 20;
    private final int h = 20;

    private Object lastButtonPressed;

    public DrawMap(String UserName,String Password,String City){

    setTitle("Draw Map");
    setSize(800,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Random p = new Random();
    //r = p.nextInt(255);
    //g = p.nextInt(255);
    //b = p.nextInt(255);

    this.UserName = UserName;
    this.Password = Password;
    this.City   = City;

    final Container contentPane = getContentPane();

    JPanel buttonPanel = new JPanel();
    JPanel buttonPanel1 = new JPanel();
    JPanel TextField = new JPanel();

    /*Draw = new JButton("Draw");
    Shape = new JButton("Shape");*/
    Load = new JButton("Load");
    Back = new JButton("Back");
    Logout = new JButton("Logout");
    
    MapField = new JTextField();
    //MapField.setBounds(130,130,100,30);
    
    TextField.add(MapField);

    //buttonPanel.add(Draw);
    //buttonPanel.add(Shape);
    buttonPanel1.add(Load);
    buttonPanel1.add(Back);
    buttonPanel1.add(Logout);

    contentPane.add(buttonPanel, BorderLayout.NORTH);
    contentPane.add(buttonPanel1, BorderLayout.SOUTH);
    contentPane.add(TextField, BorderLayout.CENTER);

    DrawPanel = new JPanel(){

    };

    contentPane.add(DrawPanel, BorderLayout.CENTER);

    final ActionListener buttonPressed = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                lastButtonPressed = event.getSource();
            }
        };

        //Draw.addActionListener(buttonPressed);
        //Shape.addActionListener(buttonPressed);
        Load.addActionListener(buttonPressed);
        Back.addActionListener(buttonPressed);
        Logout.addActionListener(buttonPressed);
        
        contentPane.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                int x = e.getX();
                int y = e.getY();

                 
                if (lastButtonPressed == Load){
                    //shape = new Rectangle2D.Double(x, y, w, w);
                    //echo("Square",x,y);
                    //color = new Color(0,0,0);
                    
                } 
                else if (lastButtonPressed == Back){
                    UserHome ush = new UserHome(UserName,Password,City);
                    ush.setVisible(true);
                    DrawMap.this.setVisible(false);
                } 
                else if (lastButtonPressed == Logout){
                    Login L = new Login();
                    L.setVisible(true);
                    DrawMap.this.setVisible(false);
                } 

                //DrawPanel.repaint();

                
            }

        });
    
    }
  
    }

Upvotes: 1

Views: 1310

Answers (5)

Abishek Stephen
Abishek Stephen

Reputation: 470

Use WindowBuilder for designing swing frames. It is way easier and simple drag and drop interface which eliminates these issues.

WindowBuilder

Upvotes: 0

PJacouF
PJacouF

Reputation: 19

There are 2 main problems in this piece of code:

  • You initialized the JTextField wrong it should be like this:
JTextField myTextField = new JTextField(20);

Where myTextField is the name of JTextField component and 20 is the number of characters it can hold.

Because JTextField is another type of component, you can't initialize it by using JPanel

  • As JTextField is another type of component, you should first put it in a container like JPanel to make it visible. Otherwise if you put it directly into your JFrame, it will only display the latest addition. So you could try to put it into the DrawPanel that you created:
DrawPanel.add(myTextField, BorderLayout.CENTER);
contentPane.add(DrawPanel);

However I noticed that you are using BorderLayout.CENTER for two of your components.

So on a side note:

  • After implementing the modifications I mentioned, use WEST or EAST for DrawPanel.

  • Use another layout manager if you don't want to put it to south or east.

  • Or just simply use null layout for all of your JPanel and add your components using setBounds to set the position and size of the components manually. For instance;

myTextField.setBounds(10, 10, 200, 100);

Where 10s are x and y coordinates respectively and 200 and 100 are width and height respectively.

Upvotes: 0

Jayden Collis
Jayden Collis

Reputation: 155

You have used JPanel TextField = new JPanel();. I believe that you need to make it a variable? Try JPanel TextField textfield1 = new JPanel(); or something like that.

Upvotes: 0

Ahmed
Ahmed

Reputation: 423

if you want a textfield -to enter text- it is defined using JTextField i.e JTextField inputField = new JTextField(10);

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347334

Basically, you've added two components to the same position...

contentPane.add(TextField, BorderLayout.CENTER);

DrawPanel = new JPanel() {

};

contentPane.add(DrawPanel, BorderLayout.CENTER);

Because of the way BorderLayout works, it will only manage the last component added to that position.

You probably want to consider using compounding layouts, that is, add you buttons and text fields to a a separate container, which can then be added to the frame along with the DrawPanel

Your lake of support of common Coding Conventions makes it really hard to reason about what your code is doing. The conventions exist to make it easier for you to understand other peoples code and for other people to understand yours, I'd recommend taking the time to try and learn them

Upvotes: 1

Related Questions