[SOLVED] JTextfield and graphics

Ok so im currenly having a problem implementing a JTextField onto a graphics windows/frame, some source code:

//intialise variable
JTextField port_textbox = new JTextField(15);
//adding in to my menu class:
	public MenuManagement() {
		Main.f.add(port_textbox);
		port_textbox.setBounds(100,100,100,100);
		port_textbox.setVisible(false);
	}
	public void draw(Graphics2D g2d) {
			g2d.drawImage(getBgImg(), 0, 0, null);
			g2d.drawImage(getFog1(), fogX, fogY, null);
			g2d.setColor(Color.WHITE);
			g2d.drawString("IP: ", 100, 320);
			g2d.drawString("Port: ", 100, 350);
			g2d.drawString("Back", 100, 380);
			
			g2d.fillRect(0, 0, 500, 500);
			ip_textbox.setVisible(true);
			port_textbox.setVisible(true);

			g2d.drawRect(90, 300, 250, 30);
			g2d.drawImage(getTitleImage(), 60, 0, null);
             }

EDIT: forgot to say what the problem was, so what happens is…whenever i click a textbox to start typing information into it, the text box its starts a flickering effect. The text itself is being entered, i know this because when it flickers i can see the text for a short amount of time.

someone please?

What is the problem?.. You could try removing “.setVisible(true)” every time you draw something in that loop.

I have already tried that and it did nothing

You didn’t tell what the problem is? You just put down some NOT FULL code in front and asking for help. How are we supposed to know what the issue is? I can’t even run this code to see the problem for myself. If you ask for help, don’t forget to state the problem. That is very important.

If you want help here are some suggestions:

Try solving it yourself. Google is your friend.
Describe your problem in detail and what you have done to try and solve it.
Do not double post.
Be patient.

Remember that you are asking for someones time and effort so the least you can do is make it as easy as possibly for anybody that is willing to spend time on your problem.

No, you teach yourself to be patient. Don’t make excuses. When you post, use http://sscce.org/ as a guideline. In your code I don’t know what getFog1() or getBgImg() is, or Main.f.

You should try adding JPanel onto JFrame. Then, add components, such as JTextField, onto the JPanel. Here is a short example.

JFrame frame = new JFrame("JPanel Example.");
		JPanel panel = new JPanel();
		frame.add(panel, BorderLayout.CENTER);
		
		JTextField field = new JTextField(100);
		panel.add(field);
		
		frame.pack();
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);

Try doing that and see if it works. If it doesn’t, I’m sorry. I don’t have anymore ideas. I would suggest to remove “field.setVisible(boolean b);” command from draw function.

In addition to previous post:

The custom drawing is done by extending a JPanel and overrides the paintComponent() method.


@Override
public void paintComponent(Graphics g) {  // graphics subsystem passes a Graphis2D subclass object as argument
   super.paintComponent(g);           // paint parent's background
   Graphics2D g2d = (Graphics2D) g;   // downcast the Graphics object back to Graphics2D
   // Perform custom drawing using g2d handle
   ......
}