Hi!
Versuche mich gerade mal an Swing. Leider habe ich zwar ein lauffähiges Programm, doch wirft mir Eclipse beim speichern (also kompilieren) immer folgende Meldung aus:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Dimension.<init>(Unknown Source)
at javax.swing.plaf.basic.BasicProgressBarUI.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.GridBagLayout.GetLayoutInfo(Unknown Source)
at java.awt.GridBagLayout.getLayoutInfo(Unknown Source)
at java.awt.GridBagLayout.ArrangeGrid(Unknown Source)
at java.awt.GridBagLayout.arrangeGrid(Unknown Source)
at java.awt.GridBagLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at SimpleFileCopyApp.main(SimpleFileCopyApp.java:192)
Wenn ich die Fehlermeldung richtig verstehe, dann kann es nicht die Dimension (Größe) der ProgressBar ermitteln. Weiss leider aber nicht mehr weiter, deshalb poste ich mal mein Programm. Vielleicht kann mir jemand erklären, wie ich weiterkommen kann, da ich die ProgressBar in Eclipse einfach auf den JContentPane gezogen habe und deshalb nicht weiss, weshalb es jetzt die Probleme gibt.
Dass ich ein GridBagLayout verwendet habe, lag eher daran, dass ich dieses Layout ausprobieren wollte.
Auch so, zur Erklärung des Programms. Das Programm soll einen JFrame öffnen, der eine ProgressBar und einen Button enthält. Mit dem Button öffne ich einen FileChooser, um eine Datei auszuwählen, die ich kopieren möchte.
Eine zweite Frage habe ich auch noch: Das Kopieren der Datei wird mit einem FileChannel durchgeführt. Leider ist mir aufgefallen, dass das Kopieren von kleinen Dateien (<200MB) ohne Probleme funktioniert, doch bei grossen Dateien (hatte einen Film mit 800MB versucht), bricht der FileChannel mit einer IOException ab.
Gibt es beim FileChannel eine grössen Beschränkung? [/code]
Versuche mich gerade mal an Swing. Leider habe ich zwar ein lauffähiges Programm, doch wirft mir Eclipse beim speichern (also kompilieren) immer folgende Meldung aus:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Dimension.<init>(Unknown Source)
at javax.swing.plaf.basic.BasicProgressBarUI.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.GridBagLayout.GetLayoutInfo(Unknown Source)
at java.awt.GridBagLayout.getLayoutInfo(Unknown Source)
at java.awt.GridBagLayout.ArrangeGrid(Unknown Source)
at java.awt.GridBagLayout.arrangeGrid(Unknown Source)
at java.awt.GridBagLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at SimpleFileCopyApp.main(SimpleFileCopyApp.java:192)
Wenn ich die Fehlermeldung richtig verstehe, dann kann es nicht die Dimension (Größe) der ProgressBar ermitteln. Weiss leider aber nicht mehr weiter, deshalb poste ich mal mein Programm. Vielleicht kann mir jemand erklären, wie ich weiterkommen kann, da ich die ProgressBar in Eclipse einfach auf den JContentPane gezogen habe und deshalb nicht weiss, weshalb es jetzt die Probleme gibt.
Dass ich ein GridBagLayout verwendet habe, lag eher daran, dass ich dieses Layout ausprobieren wollte.
Code:
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.io.File;
import javax.swing.JProgressBar;
import javax.swing.JButton;
public class SimpleFileCopyApp extends JFrame {
/**
*
*/
private static final long serialVersionUID = -2642433080877673624L;
private JPanel jContentPane = null;
private JProgressBar jProgressBar = null;
private JButton jButton = null;
//Source and Destination File
private File scrFile = null, dstFile = null;
static SimpleFileCopy mycopy;
/**
* This method initializes jProgressBar
*
* @return javax.swing.JProgressBar
*/
private JProgressBar getJProgressBar() {
if (jProgressBar == null) {
jProgressBar = new JProgressBar();
jProgressBar.setStringPainted(true);
jProgressBar.setString(" ");
}
return jProgressBar;
}
/**
* Öffnet eine Datei mit dem Dialog JFileChooser
* @return Referenz der geöffneten Datei
*/
private File openFile(){
//Create a file chooser
final JFileChooser fc = new JFileChooser();
//In response to a button click:
int returnVal = fc.showOpenDialog(jContentPane);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File scrFile = fc.getSelectedFile();
//System.out.println("Source File Path:" + scrFile.getPath());
if(scrFile.exists()){
return scrFile;
} else {
JOptionPane.showMessageDialog(jContentPane,
"Error: The selected file not exists",
"Selection error",
JOptionPane.ERROR_MESSAGE);
}
}
if (returnVal == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(jContentPane,
"No file was selected");
//System.out.println("Keine Datei ausgewählt");
}
return null;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Select a file to copy");
jButton.setToolTipText("Select a file, which the programm should copy");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
/*
* ProgressBar in Aktion setzen
*/
jProgressBar.setString("Copying");
jProgressBar.setIndeterminate(true);
/*
* lokale Methode zum Kopieren, die dann das
* wirkliche kopieren aufruft
*/
copyFile();
/*
* ProgressBar aus Aktion (Interminate)
* zuruecksetzen
*/
jProgressBar.setIndeterminate(false);
jProgressBar.setString(" ");
}});
}
return jButton;
}
/**
* lokale Methode zum Kopieren, die dann das
* wirkliche kopieren (fileCopyFileChannel) aufruft
* @param scrFile Ursprungdatei
* @param dstFile Zieldatei
*/
public void copyFile() {
// Ursprungsdatei öffnen
scrFile = openFile();
// Weitere Überprüfung, ob Methode openFile erfolgreich
if (scrFile != null){
//System.out.println("Source File not null");
/* Erzeugt eine neue Datei mit dem Pfad des Scoure Files
* und angehängtem '.copy'
*/
dstFile = new File(scrFile.getPath() + ".copy");
if (!dstFile.exists()){
//System.out.println("Destination File" + dstFile.getPath());
if (mycopy.fileCopyFileChannel(scrFile, dstFile)){
//jProgressBar.setValue(100);
JOptionPane.showMessageDialog(jButton,
"Success: The file was copied!");
//jProgressBar.setValue(0);
} else {
JOptionPane.showMessageDialog(jContentPane,
"Error: Can't copy the file",
"Copy error",
JOptionPane.ERROR_MESSAGE);
}
} else {
//System.out.println("Destination File existes:" + dstFile.getPath());
JOptionPane.showMessageDialog(jButton,
"Error: The destination file already exists. No Copy!",
"Selection error",
JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//String WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
JFrame.setDefaultLookAndFeelDecorated(true);
//UIManager.setLookAndFeel(WINDOWS);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleFileCopyApp myapp = new SimpleFileCopyApp();
mycopy = new SimpleFileCopy();
myapp.setLocationRelativeTo(null);
myapp.setDefaultCloseOperation(EXIT_ON_CLOSE);
myapp.setVisible(true);
}
/**
* This is the default constructor
*/
public SimpleFileCopyApp() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("Simple File Copy Application");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.insets = new java.awt.Insets(2,2,2,2);
gridBagConstraints1.weightx = 1.0D;
gridBagConstraints1.weighty = 1.0D;
gridBagConstraints1.gridy = 0;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.insets = new java.awt.Insets(2,2,2,2);
gridBagConstraints.weightx = 1.0D;
gridBagConstraints.weighty = 1.0D;
gridBagConstraints.gridy = 1;
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(getJButton(), gridBagConstraints);
jContentPane.add(getJProgressBar(), gridBagConstraints1);
}
return jContentPane;
}
}
Auch so, zur Erklärung des Programms. Das Programm soll einen JFrame öffnen, der eine ProgressBar und einen Button enthält. Mit dem Button öffne ich einen FileChooser, um eine Datei auszuwählen, die ich kopieren möchte.
Eine zweite Frage habe ich auch noch: Das Kopieren der Datei wird mit einem FileChannel durchgeführt. Leider ist mir aufgefallen, dass das Kopieren von kleinen Dateien (<200MB) ohne Probleme funktioniert, doch bei grossen Dateien (hatte einen Film mit 800MB versucht), bricht der FileChannel mit einer IOException ab.
Gibt es beim FileChannel eine grössen Beschränkung? [/code]