[Swing] abrir diretorio ou arquivo em uma janela [RESOLVIDO]

4 respostas
P

Olá Pessoal,

Me tira uma dúvida, tem alguma forma que eu possa abrir um diretorio? por exemplo, eu sei q existe o diretorio: C:/guj/teste, como eu faço para abrir este diretorio para o usuario?

A minha intenção é mostrar para o usuario o seguinte: Veja mais detalhes no arquivo de log LOG.TXT em c:/LOG. Adicionar 2 botoes em baixo, um Abrir Diretorio e Outro Abrir Arquivo.

Obrigado pela força.

4 Respostas

T

Já deu uma olhada aqui: http://www.guj.com.br/article.show.logic?id=13

P

Então cara, dei uma olhada sim, mas neste artigo tem como manipular o arquivo, abrir, alterar e etc… o q eu to querendo é diferente, to querendo que qdo o usuario aperte o botão abra o diretorio para ele com todos os arquivos dentro, abrir um windows explorer da vida no caminho que eu passar entendeu?

Obrigado.
Abraço

T

Vê se é isso ajuda! 8)

P

É isso mesmo cara!!

Obrigadão, vou tentar adapta-lo pra minha necessidade aqui.

Só pra dar um norte caso alguem veja o tópico e nao entenda muito bem, pra testar a classe que o ToBack enviou, basta criar um main com o seguinte comando:

Explorer explorer = new Explorer(); explorer.setVisible(true);

Classe enviada:

package teste;

import java.awt.Font;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Explorer extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton btAbrir = null;	

	/**
	 * This is the default constructor
	 */
	public Explorer() {
		super();
		initialize();
		informacoes();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("Log");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBtAbrir(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes btAbrir
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtAbrir() {
		if (btAbrir == null) {
			btAbrir = new JButton();
			btAbrir.setBounds(new Rectangle(60, 49, 166, 69));
			btAbrir.setFont(new Font("Dialog", Font.BOLD, 24));
			btAbrir.setText("Abrir");
			btAbrir.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {					
					abrir();
				}
			});
		}
		return btAbrir;
	}

	private void abrir() {
		try {			
			Runtime.getRuntime().exec("explorer.exe " + "C:\\Logs\\");
		} catch (IOException e1) {			
			e1.printStackTrace();
		}
	}
	
	private void informacoes(){
		Properties p = new Properties(System.getProperties());	
		StringBuilder log = new StringBuilder();
		log.append("Diret�tio da aplica��o:"+p.getProperty("user.dir"));
		log.append("\nUsu�rio: "+p.getProperty("user.name"));
		log.append("\nOS Vers�o: "+p.getProperty("os.version"));
		log.append("\nNome OS: "+p.getProperty("os.name"));
		log.append("\nJava Vers�o: "+p.getProperty("java.version"));		
		salvarLog(log);
	}

	private void salvarLog(StringBuilder log) {
		try {			
			File dir = new File("C:\\Logs");
			dir.mkdir();
			File file = new File(dir,"log.txt");			
			file.createNewFile();			
			FileWriter fw = new FileWriter(file);
			fw.write(log.toString());
			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	

}
Criado 31 de outubro de 2009
Ultima resposta 1 de nov. de 2009
Respostas 4
Participantes 2