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