본문 바로가기

[JAVA][SWING]디렉토리만 선택


사용자로부터 디렉토리 즉 폴더 경로만 받아서 처리할 상황이 생겼다

예를들어 내가 만든 프로그렘이 뭔가 산출물을 떨궈주는데 그것을 어디로 받을지에대한 경로설정? 뭐 그런식이다

어쩨껀간에




소스투척


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
 
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
 
@SuppressWarnings("serial")
public class ExSelectDerectoriesChoisOnly extends JFrame {
 
    public ExSelectDerectoriesChoisOnly() {
        Container container = getContentPane();
 
        JLabel label = new JLabel("출력경로 : ");
        container.add(label);
        JTextField oututPathTF = new JTextField();
        oututPathTF.setEnabled(false);
        container.add(oututPathTF);
        JButton btn = new JButton("선택");
        btn.addActionListener(e -> {
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.showDialog(thisnull);
            File dir = jfc.getSelectedFile();
            oututPathTF.setText(dir==null?"":dir.getPath());
        });
        container.add(btn);
        
        BorderLayout outPathLayout = new BorderLayout();
        outPathLayout.addLayoutComponent(label, BorderLayout.WEST);
        outPathLayout.addLayoutComponent(oututPathTF, BorderLayout.CENTER);
        outPathLayout.addLayoutComponent(btn, BorderLayout.EAST);
        container.setLayout(outPathLayout);
        
        setSize(30060);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
 
    public static void main(String[] args) {
        new ExSelectDerectoriesChoisOnly();
    }
    
}
 
cs


출처 : [Tip]Swing 에서 디렉토리만 선택할때