Java - Como hacer el JFileChooser

 
Vista:
sin imagen de perfil

Como hacer el JFileChooser

Publicado por maribajar (18 intervenciones) el 06/11/2007 18:44:09
Buenas tardes
Estoy intentando hacer el codigo en java para hacer un boton abrir y cuando de en el se me habra los ficheros del disco duro, y para ello uso JFileChooser, pero cuando doy al boton abrir no hace nada, que hago mal. El código que tengo es el siguiente:

package paquete;

import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.lang.*;
import javax.swing.filechooser.*;

public class examinar extends JApplet implements MouseListener{

JTextArea log;

String newline = System.getProperty("line.separator");

private JFileChooser fc = new JFileChooser();

public void init() {
JButton abrirButton = new JButton("Abrir");
abrirButton.addActionListener(new abrir());


JPanel buttonPanel = new JPanel();
buttonPanel.add(abrirButton);
Container contentPane = getContentPane();
contentPane.add(buttonPanel, BorderLayout.NORTH);

log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(log);
getContentPane().add(buttonPanel, java.awt.BorderLayout.CENTER);

}

private class abrir implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(examinar.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
}
}//fin de abrir


public void mouseClicked(MouseEvent e) {
this.init();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}
}//fin de examinar

El problema es saber que poner en la línea:
int returnVal = fc.showOpenDialog(examinar.this);
porque yo creo que el examinar.this no funciona.
No puedo tener un main, porque esta clase no es la principal, sino que tengo otra que tiene el main.
Entonces como podria hacer para que me salga el JFileChooser cuando doy al boton abrir.
Muchas gracias de antemano.
Mari
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Como hacer el JFileChooser

Publicado por talia  (1 intervención) el 28/04/2009 19:30:43
package Tamira;

import java.io.IOException;
import javax.swing.*;

public class Principal extends JFrame
{
private String TextoCopiado = "";
private jFontChooser fc= new com.l2fprod.common.swing.JFontChooser1();

public Principal()
{
try {
initComponents();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);

} catch(Exception e){}
}
@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

// me da error aqui-
jFontChooser1 = new com.l2fprod.common.swing.JFontChooser();
jPopupMenu1 = new javax.swing.JPopupMenu();
jMenuItem11 = new javax.swing.JMenuItem();
jMenuItem12 = new javax.swing.JMenuItem();
jMenuItem13 = new javax.swing.JMenuItem();
jSeparator5 = new javax.swing.JSeparator();
jMenuItem14 = new javax.swing.JMenuItem();
jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jSeparator4 = new javax.swing.JToolBar.Separator();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jSeparator6 = new javax.swing.JToolBar.Separator();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jToolBar2 = new javax.swing.JToolBar();
jTabbedPane1 = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JSeparator();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
jMenuItem7 = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JSeparator();
jMenuItem8 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem9 = new javax.swing.JMenuItem();
jMenuItem10 = new javax.swing.JMenuItem();

jMenuItem11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Copy.png"))); // NOI18N
jMenuItem11.setText("Copiar");
jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Copiar(evt);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar