Dzień dobry,
Mam następujący problem. Otóż piszę sobie prosty czat LAN. Wpadłem na pomysł, że dobrym pomysłem byłoby zaimplementowanie możliwości przesyłania plików. Napisałem klasę ClientFrame, z która zawiera GUI klienta czatu oraz klasę SendFileFrame służącą do wybierania pliku do wysłania. Poniżej wklejam kod klasy ClientFrame:
package com.jarek.chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.Socket;
/**
* Created by Jarek on 09.11.16.
*/
public class ClientFrame extends JFrame implements ActionListener{
private JPanel panelMain;
private JTextArea msgArea = new JTextArea(10, 32);
private JTextField msgText = new JTextField();
private JPanel btnPanel = new JPanel();
private JButton msgSend = new JButton("Send message!");
private JButton clearArea = new JButton("Clear message area!");
private JButton fileSend = new JButton("Send file!");
private File file;
private Container container = getContentPane();
private SendFileFrame sendFile;
static Socket s;
static DataInputStream dataInputStream;
static DataOutputStream dataOutputStream;
public ClientFrame() {
msgArea.setEditable(false);
msgArea.setWrapStyleWord(true);
msgArea.setLineWrap(true);
msgSend.addActionListener(this);
clearArea.addActionListener(this);
fileSend.addActionListener(this);
container.add(new JScrollPane(msgArea), BorderLayout.CENTER);
btnPanel.setLayout(new FlowLayout());
btnPanel.setPreferredSize(new Dimension(200,200));
btnPanel.add(msgSend, BorderLayout.NORTH);
btnPanel.add(clearArea, BorderLayout.SOUTH);
btnPanel.add(fileSend, BorderLayout.WEST);
container.add(btnPanel, BorderLayout.EAST);
container.add(msgText, BorderLayout.SOUTH);
setTitle("Client");
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int choice = JOptionPane.showConfirmDialog(container, "Exit?", "Close CHAT",
JOptionPane.OK_OPTION);
if (choice == JOptionPane.OK_OPTION)
System.exit(0); // do dopracowania
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == msgSend) {
try {
String msgOut;
msgOut = msgText.getText().trim();
dataOutputStream.writeUTF(msgOut);
msgText.setText("");
} catch (Exception i) {
JOptionPane.showMessageDialog(this, "Send message error! Check your " +
"connection or if server is closed!", "Send error!", JOptionPane.ERROR_MESSAGE);
}
}
else if(e.getSource() == clearArea) {
msgArea.setText("");
}
else if(e.getSource() == fileSend) {
sendFile = new SendFileFrame(this);
msgArea.append("Loaded file: " + file.getName());
}
}
private void listen() {
String msgIn = "";
try {
s = new Socket("127.0.0.1" , 1220);
dataInputStream = new DataInputStream(s.getInputStream());
dataOutputStream = new DataOutputStream(s.getOutputStream());
while(msgIn!="exit") {
msgIn = dataInputStream.readUTF();
System.out.println(msgIn);
msgArea.setText(msgArea.getText() + "\nServer: " + msgIn);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Receive message error! Check your " +
"connection!", "Receive error!", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
ClientFrame clientFrame = new ClientFrame();
clientFrame.listen();
});
}
public void setFile(File file) {
this.file = file;
}
}
Oraz SendFileFrame:
package com.jarek.chat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
/**
* Created by jarek on 12/2/16.
*/
public class SendFileFrame extends JPanel implements ActionListener {
private JPanel panelMain;
private JFileChooser fileChooser;
private JButton selectFile = new JButton("Select file to send!");
private JButton sendFile = new JButton("Send choosed file!");
private JButton cancel = new JButton("Cancel!");
private JTextField fileName;
private JTextField filePath;
private JPanel main;
public File file = null;
private static JFrame frame = new JFrame("Choose file to send!");
private ClientFrame clientFrame;
private void showGUI() {
fileName = new JTextField();
fileName.setEditable(false);
filePath = new JTextField();
filePath.setEditable(false);
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
selectFile.addActionListener(this);
sendFile.addActionListener(this);
cancel.addActionListener(this);
panelMain = new JPanel();
panelMain.add(selectFile);
panelMain.add(sendFile);
panelMain.add(cancel);
add(panelMain, BorderLayout.PAGE_START);
add(fileName,BorderLayout.CENTER);
add(filePath, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.pack();
frame.setVisible(true);
}
public SendFileFrame(ClientFrame client) {
super(new BorderLayout());
showGUI();
this.clientFrame = client;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == selectFile) {
int returnVal = fileChooser.showOpenDialog(SendFileFrame.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
fileName.setText("Name: " + file.getName());
filePath.setText("Path: " + file.getPath());
}
else
fileName.setText("Cancelled by user!");
}
else if(e.getSource() == sendFile)
{
clientFrame.setFile(this.file);
frame.dispose();
}
else if(e.getSource() == cancel){
frame.dispose();
}
// close
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SendFileFrame sf = new SendFileFrame(null);
});
}
public File getFile() {
return file;
}
}
Rozwiązanie to jednak wywołuje NullPointerException. Jakieś rady, możliwe sposoby rozwiązania?