Ich brauche mal eure Hilfe. Ich habe gerade erst mit Programmieren angefangen, also kann es auch gut sein, dass ich mich einfach nur dumm anstelle.
Zu meinem Problem:
Wenn ich die Main Methode von "Receive" manuell ausführe, funktioniert die einwandfrei. Wenn ich sie Allerdings durch einen jButton starten möchte, wird sie nicht Ordentlich ausgeführt. Es öffnet sich dann nur ein leerer JFrame ohne jeglichen Inhalt.
[CODE lang="java" title="Aufruf"]jbReceive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Receive.main(null);
} catch (Exception error) {
error.printStackTrace();
}
}
});[/CODE]
Zu meinem Problem:
Wenn ich die Main Methode von "Receive" manuell ausführe, funktioniert die einwandfrei. Wenn ich sie Allerdings durch einen jButton starten möchte, wird sie nicht Ordentlich ausgeführt. Es öffnet sich dann nur ein leerer JFrame ohne jeglichen Inhalt.
[CODE lang="java" title="Aufruf"]jbReceive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Receive.main(null);
} catch (Exception error) {
error.printStackTrace();
}
}
});[/CODE]
Java:
public class Receive {
static ArrayList<MyFile> myFiles = new ArrayList<>();
public static void main(String[] args) throws Exception {
int fileId = 0;
ServerSocket serverSocket = new ServerSocket(6969);
JFrame jFrame = new JFrame("SquareShare");
jFrame.setSize(400, 400);
jFrame.setLayout(new BoxLayout(jFrame.getContentPane(), BoxLayout.Y_AXIS));
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
WindowListener exitListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
serverSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
};
jFrame.addWindowListener(exitListener);
JPanel jPanel = new JPanel();
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
JScrollPane jScrollPane = new JScrollPane(jPanel);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JLabel jlTitle = new JLabel("File Receiver");
jlTitle.setFont(new Font("Arial", Font.BOLD, 25));
jlTitle.setBorder(new EmptyBorder(20, 0, 0, 0));
jlTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel ip4 = new JLabel("Your adress: " + getOwnerIp());
ip4.setFont(new Font("Arial", Font.BOLD, 10));
ip4.setBorder(new EmptyBorder(10, 0, 0, 0));
ip4.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton jbToClipboard = new JButton("Copy");
jbToClipboard.setPreferredSize(new Dimension(20, 20));
jbToClipboard.setFont(new Font("Arial", Font.BOLD, 20));
jbToClipboard.setAlignmentX(Component.CENTER_ALIGNMENT);
jFrame.add(jlTitle);
jFrame.add(ip4);
jFrame.add(jbToClipboard);
jFrame.add(jScrollPane);
jFrame.setVisible(true);
jbToClipboard.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(getOwnerIp()), null);
}
}
);
while (true)
{
try {
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
int fileNameLength = dataInputStream.readInt();
if (fileNameLength > 0) {
byte[] fileNameBytes = new byte[fileNameLength];
dataInputStream.readFully(fileNameBytes, 0, fileNameBytes.length);
String fileName = new String(fileNameBytes);
int fileContentLength = dataInputStream.readInt();
if (fileContentLength > 0) {
byte[] fileContentBytes = new byte[fileContentLength];
dataInputStream.readFully(fileContentBytes, 0, fileContentLength);
JPanel jpFileRow = new JPanel();
jpFileRow.setLayout(new BoxLayout(jpFileRow, BoxLayout.Y_AXIS));
JLabel jlFileName = new JLabel(fileName);
jlFileName.setFont(new Font("Arial", Font.BOLD, 20));
jlFileName.setBorder(new EmptyBorder(10, 0, 10, 0));
jlFileName.setAlignmentX(Component.CENTER_ALIGNMENT);
if (getFileExtension(fileName).equalsIgnoreCase("txt")) {
jpFileRow.setName(String.valueOf(fileId));
jpFileRow.addMouseListener(getMyMouseListener());
jpFileRow.add(jlFileName);
jPanel.add(jpFileRow);
jFrame.validate();
} else {
jpFileRow.setName(String.valueOf(fileId));
jpFileRow.addMouseListener(getMyMouseListener());
jpFileRow.add(jlFileName);
jPanel.add(jpFileRow);
jFrame.validate();
}
myFiles.add(new MyFile(fileId, fileName, fileContentBytes, getFileExtension(fileName)));
fileId++;
}
}
} catch (IOException error) {
error.printStackTrace();
}
}
}
public static MouseListener getMyMouseListener() {
return new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JPanel jPanel = (JPanel) e.getSource();
int fileId = Integer.parseInt(jPanel.getName());
for (MyFile myFile : myFiles) {
if (myFile.getId() == fileId) {
JFrame jfPreview = createFrame(myFile.getName(), myFile.getData(), myFile.getFileExtension());
jfPreview.setVisible(true);
}
}
}
public static JFrame createFrame(String fileName, byte[] fileData, String fileExtension) {
JFrame jFrame = new JFrame("SquareShare");
jFrame.setSize(800, 400);
JPanel jPanel = new JPanel();
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
JLabel jlTitle = new JLabel("File Receiver");
jlTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
jlTitle.setFont(new Font("Arial", Font.BOLD, 25));
jlTitle.setBorder(new EmptyBorder(20, 0, 10, 0));
JLabel jlPrompt = new JLabel("Are you sure you want to download" + fileName);
jlPrompt.setFont(new Font("Arial", Font.BOLD, 20));
jlPrompt.setBorder(new EmptyBorder(20, 0, 10, 0));
jlPrompt.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton jbYes = new JButton("Yes");
jbYes.setPreferredSize(new Dimension(150, 75));
jbYes.setFont(new Font("Arial", Font.BOLD, 20));
JButton jbNo = new JButton("No");
jbNo.setPreferredSize(new Dimension(150, 75));
jbNo.setFont(new Font("Arial", Font.BOLD, 20));
JLabel jlFileContent = new JLabel();
jlFileContent.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel jpButtons = new JPanel();
jpButtons.setBorder(new EmptyBorder(20, 0, 10, 0));
jpButtons.add(jbYes);
jpButtons.add(jbNo);
if (fileExtension.equalsIgnoreCase("txt")) {
jlFileContent.setText("<html>" + new String(fileData) + "</html>");
} else {
// jlFileContent.setIcon(new ImageIcon(fileData));
// Picture size must be cropped!!!
}
jbYes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("SquareShare");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();
String destination = fileToSave.getAbsolutePath();
File fileToDownload = new File(destination + "." + getFileExtension(fileName));
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileToDownload);
fileOutputStream.write(fileData);
fileOutputStream.close();
jFrame.dispose();
} catch (IOException error) {
error.printStackTrace();
}
}
}
});
jbNo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.dispose();
}
});
jPanel.add(jlTitle);
jPanel.add(jlPrompt);
jPanel.add(jlFileContent);
jPanel.add(jpButtons);
jFrame.add(jPanel);
return jFrame;
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
};
}
public static String getFileExtension(String fileName) {
// Would not work with .tar.gz
int i = fileName.lastIndexOf('.');
if (i > 0) {
return fileName.substring(i + 1);
} else {
return "No extension found.";
}
}
public static String getOwnerIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
}