JFileChooser bloqueando botoes

2 respostas
getAndSet

Olá amigos estou com dificuldade em como bloquear o botao
de criar nova pasta e tambem como abrir o filechooser
em uma pasta especifica
DESDE JA AGRADEÇO :lol:

2 Respostas

Sami_Koivu

Olá,

Se você estiver usando Java 1.4 ou mais antiga, fica um pouco complicado:

http://forum.java.sun.com/thread.jspa?threadID=432596&messageID=1941105

Os caras começam falar em alemão no final, o começo está em inglês e tem alguns exemplos de como conseguir isto.

Caso você está usando Java 1.5, tem uma saída mais fácil:

http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=7ba16791048dc2ffffffffdee4e0cbaa65433:WuuT?bug_id=4525475

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;

public class ROFileChooser extends JFileChooser {
    private Boolean readOnly;

    public ROFileChooser(boolean readOnly) {
        this.readOnly = Boolean.valueOf(readOnly);
    }

    protected void setUI(ComponentUI newUI) {
        if (readOnly != null) {
            UIManager.put("FileChooser.readOnly", readOnly);
        }
        super.setUI(newUI);
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JFrame frame = new JFrame();
                final JButton normalButton   = new JButton("Normal");
                final JButton readOnlyButton = new JButton("Read Only");
                ActionListener action = new ActionListener() {
                    public void actionPerformed(ActionEvent ev) {
                        boolean readOnly = (ev.getSource() == readOnlyButton);
                        UIManager.put("FileChooser.readOnly",
                                      Boolean.valueOf(readOnly));
                        JFileChooser fc = new ROFileChooser(readOnly);
                        fc.showOpenDialog(frame);
                    }
                };
                normalButton.addActionListener(action);
                readOnlyButton.addActionListener(action);
                frame.getContentPane().add(normalButton, BorderLayout.WEST);
                frame.getContentPane().add(readOnlyButton, BorderLayout.EAST);
                frame.pack();
                frame.setLocation(200, 200);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

Obs: Não cheguei a testar nenhuma dessas abordagens.

[]s,
Sami

getAndSet

vlw

Criado 13 de dezembro de 2006
Ultima resposta 13 de dez. de 2006
Respostas 2
Participantes 2