1
votes

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();


        }

    });

}

}
4
The lack of java naming conventions here make this a little difficult to read (i.e. naming a JPanel TextField). What is your error? Does this even compile?achAmháin
Private JTextField MapField ; is that a typo?MadProgrammer
what is private in Private JTextField MapField;? starts with Upper case P. How is this even compiled?Yohannes Gebremariam

4 Answers

1
votes

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

1
votes

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

0
votes

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.

0
votes

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.