JFileChooser, 열기 창, 저장 창, 파일 선택 창, Swing > 오픈소스 | IOTsw_u2 U2 Project
오픈소스

JAVA JFileChooser, 열기 창, 저장 창, 파일 선택 창, Swing

본문

 

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileNameExtensionFilter;
 
public class FileOpen {
        public static void main (String [] args) {
                test_Frame tf = new test_Frame();
        }
}
                       
class test_Frame extends JFrame implements ActionListener{
       
        private JFileChooser jfc = new JFileChooser();
        private JButton jbt_open = new JButton("열기");
        private JButton jbt_save = new JButton("저장");
        private JLabel jlb = new JLabel(" ");
        public test_Frame(){
                super("test");
                this.init();
                this.start();
                this.setSize(400,200);
                this.setVisible(true);
        }
        public void init(){
                getContentPane().setLayout(new FlowLayout());
                add(jbt_open);
                add(jbt_save);
                add(jlb);
        }
        public void start(){
                jbt_open.addActionListener(this);
                jbt_save.addActionListener(this);
 
                jfc.setFileFilter(new FileNameExtensionFilter("txt", "txt"));
                // 파일 필터
                jfc.setMultiSelectionEnabled(false);//다중 선택 불가
        }
 
        @Override
        public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(arg0.getSource() == jbt_open){
                        if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
                                // showopendialog 열기 창을 열고 확인 버튼을 눌렀는지 확인
                                jlb.setText("열기 경로 : " + jfc.getSelectedFile().toString());
                        }
                }else if(arg0.getSource() == jbt_save){
                        if(jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
                                // showSaveDialog 저장 창을 열고 확인 버튼을 눌렀는지 확인
                                jlb.setText("저장 경로 : " + jfc.getSelectedFile().toString() + "." + jfc.getFileFilter().getDescription());
                        }
                }
        }
}

 

댓글목록

오픈소스 목록

Total 23건 2 페이지
게시물 검색

IOTsw_u2 정보

회사 . U2
주소 . 어느별 하늘 아래에 있것지요
사업자 등록번호 . 백수임 대표 . 김씨 전화 . 02-123-4567 팩스 . 팩스없음
통신판매업신고번호 . 낼할께 개인정보관리책임자 . 김씨가 알아서 함 부가통신사업신고번호 신고안함
Copyright © 2001-2013 U2. All Rights Reserved.
닫기