Reputation: 1
I'm using a jtextfield but it doesn't show up. From messing around with the code I think it's being drawn over. Another problem I've been having with the textfield is that when I revalidate it changes its size from what I need to the entire screen. I dealt with this by setting the size in a loop. I'd like it if there was a better way to do that but my priority is getting it visible first. Here is the code. Thanks ahead of time.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clientgui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
*
* @author Zzzz
*/
public class ClientGUI extends JFrame{
/**
* @param args the command line arguments
*/
Socket s ;
PrintWriter out;
String username;
BufferedReader in;
ListeningThread lt;
Image dbi;
Graphics dbg;
JTextField textField;
BufferedImage background;
public ClientGUI() throws IOException, InterruptedException
{
background = ImageIO.read(new File("Background.png"));
s = new Socket("127.0.0.1", 4382);
out = new PrintWriter(s.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
lt = new ListeningThread(s);
lt.start();
setTitle("Simple Chat");
setSize(500,500);
setDefaultCloseOperation(3);
setVisible(true);
textField = new JTextField();
textField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
out.println(textField.getText());
}
});
add(textField);
while(true)
{
textField.setBounds(100, 420, 325, 25);
textField.repaint();
Thread.sleep(20);
}
}
@Override
public void paint(Graphics g)
{
dbi = createImage(getWidth(), getHeight());
dbg = dbi.getGraphics();
paintComponent(dbg);
g.drawImage(dbi, 0, 0, rootPane);
}
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.drawImage(background, 0, 0, rootPane);
g.drawString((String) ListeningThread.messages.get(0), 50, 65);
repaint();
}
public static void main(String[] args) throws IOException, InterruptedException {
new ClientGUI();
}
}
Upvotes: 0
Views: 224
Reputation: 114310
There's a great article on how Swing and AWT do their painting on the Oracle site. I highly recommend you read it. In the section on the painting methods of Swing, it explains the errors you are facing.
paint()
(big mistake)Basically, paint()
is the method that repaints everything, while paintComponent()
is the place to put your custom code. paint()
actually calls these functions (in order):
protected void paintComponent(Graphics g)
protected void paintBorder(Graphics g)
protected void paintChildren(Graphics g)
When you overrode paint, you prevented your text box from ever being drawn at all by paintChildren()
. To quote the article:
Swing programs should override
paintComponent()
instead of overridingpaint()
repaint()
(little mistake)The same article also has a section on how repainting normally works. A very simplified way to look at it is that calling repaint()
schedules a call to paint()
in the near future, when the AWT Event Processing Thread gets to it. You do not need to call repaint()
inside paintComponent()
.
paint()
into paintComponent()
.repaint()
at the end of paintComponent()
.Upvotes: 2