본문 바로가기

카테고리 없음

Round 23 JApplet 클래스와 JFC 고급 클래스 활용2 - JTree 클래스와 윈도우 탐색기

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

class Round23_Ex04_Sub extends JFrame {

    private JRootPane jrp;
    private Container con;
    // private JTree jt = new JTree();
    private String[] str = {"AAA", "BBB", "CCC"};
    private JTree jt = new JTree(str);
    private JScrollPane jsp = new JScrollPane(jt);

    public Round23_Ex04_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout());
        con.add("North", new JLabel("JTree !!", JLabel.CENTER));
        JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        jp.add(new Button("확인"));
        jp.add(new Button("취소"));
        con.add("South", jp);
        
        //jt.addSelectionInterval(0, 2);
        jt.addSelectionRow(1); // 기본 선택 Node
        jt.expandRow(1); //하위 내용을 가질 때 확장될 Node
        jt.setDragEnabled(true); // Drag 활성 여부
        jt.setEditable(true); // Node 수정 여부
        jt.setToggleClickCount(2); // Toggle 지정 시 클릭 수
        con.add("Center", jsp);
    }
    
    public void start() {
    
    }
}

public class Round23_Ex04 {
    public static void main(String[] args) {
        try {
            // 폼에 대한 LookAndFeel 을 지정한다.
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch( Exception e) {
        }
        
        new Round23_Ex04_Sub();
    }
}

 

JTree에 하위노드를 포함 표현

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import javax.swing.tree.*;

class Round23_Ex05_Sub extends JFrame {

    private JRootPane jrp;
    private Container con;
    
    private DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode("TOP");
    private DefaultMutableTreeNode dmtn1 = new DefaultMutableTreeNode("RED");
    private DefaultMutableTreeNode dmtn2 = new DefaultMutableTreeNode("GREEN");
    private DefaultMutableTreeNode dmtn3 = new DefaultMutableTreeNode("BLUE");
    private DefaultMutableTreeNode dmtn11 = new DefaultMutableTreeNode("11");
    private DefaultMutableTreeNode dmtn22 = new DefaultMutableTreeNode("22");
    private DefaultMutableTreeNode dmtn33 = new DefaultMutableTreeNode("33");
    
    private DefaultTreeModel dtm = new DefaultTreeModel(dmtn);
    private JTree jt = new JTree(dtm);
    private JScrollPane jsp = new JScrollPane(jt);

    public Round23_Ex05_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout(5, 5));
        con.add("North", new JLabel("JTree !!", JLabel.CENTER));
        JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        jp.add(new Button("확인"));
        jp.add(new Button("취소"));
        con.add("South", jp);
        
        dmtn.add(dmtn1);
        dmtn.add(dmtn2);
        dmtn.add(dmtn3);
        dmtn1.add(dmtn11);
        dmtn1.add(dmtn22);
        dmtn1.add(dmtn33);
        con.add("Center", jsp);
    }   
    public void start() {
    
    }
}
public class Round23_Ex05 {
    public static void main(String[] args) {
        new Round23_Ex05_Sub();
    }
}

 

JTree 색상 폰트 사용

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import javax.swing.tree.*;

class Round23_Ex06_Sub extends JFrame {

    private JRootPane jrp;
    private Container con;
    
    private DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode("TOP");
    private DefaultMutableTreeNode dmtn1 = new DefaultMutableTreeNode("RED");
    private DefaultMutableTreeNode dmtn2 = new DefaultMutableTreeNode("GREEN");
    private DefaultMutableTreeNode dmtn3 = new DefaultMutableTreeNode("BLUE");
    private DefaultMutableTreeNode dmtn11 = new DefaultMutableTreeNode("11");
    private DefaultMutableTreeNode dmtn22 = new DefaultMutableTreeNode("22");
    private DefaultMutableTreeNode dmtn33 = new DefaultMutableTreeNode("33");
    
    private DefaultTreeModel dtm = new DefaultTreeModel(dmtn);
    private JTree jt = new JTree(dtm);
    private JScrollPane jsp = new JScrollPane(jt);
    
    private DefaultTreeCellRenderer dtcr = new DefaultTreeCellRenderer();

    public Round23_Ex06_Sub() {
        super("Test");
        this.init();
        this.start();
        this.setSize(300, 200);
        this.setVisible(true);
    }
    
    public void init() {
        jrp = this.getRootPane();
        con = jrp.getContentPane();
        con.setLayout(new BorderLayout(5, 5));
        con.add("North", new JLabel("JTree !!", JLabel.CENTER));
        JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        jp.add(new Button("확인"));
        jp.add(new Button("취소"));
        con.add("South", jp);
        
        dmtn.add(dmtn1);
        dmtn.add(dmtn2);
        dmtn.add(dmtn3);
        dmtn1.add(dmtn11);
        dmtn1.add(dmtn22);
        dmtn1.add(dmtn33);
        
        dtcr.setBackground(Color.red);
        dtcr.setBackgroundNonSelectionColor(Color.blue);
        dtcr.setBackgroundSelectionColor(Color.red);
        dtcr.setFont(new Font("굴림체", Font.BLUE, 20));
        jt.setCellRenderer(dtcr);
        con.add("Center", jsp);
    }   
    public void start() {
    
    }
}
public class Round23_Ex06 {
    public static void main(String[] args) {
        new Round23_Ex06_Sub();
    }
}

 

드래그 기능을 제외한 탐색기

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import javax.swing.tree.*;

class Round23_Ex07_Sub extends JFrame implements TreeWillExpandListener, ActionListener {

    private Container con;
    private JSplitPane sp = new JSpliPane();
    private JSplitPane sp1 = new JSpliPane(JSplitPane.VERTICAL_SPLIT);
    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("My 탐색기");
    private JTree tree_jt = new JTree(root);
    privaet JScrollPane tree_jsp = new JScrollPane(tree_jt);
    private Vector view_vc = new Vector();
    private JList view_li = new JList(view_vc);
    private JScrollPane view_jsp = new JScrollPane(view_li);
    private JButton view_bt = new JButton("VIEW");
    private JButton edit_bt = new JButton("EDIT");
    private JButton del_bt = new JButton("DELETE");
    private JButton end_bt = new JButton("EXIT");
    private JTextArea data_ta = new JTextArea();
    private JScrollPane data_jsp = new JScrollPane(data_ta);
    private Dimension screen;

    public Round23_Ex07_Sub() {
        super("My 탐색기");
        screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.init();
        this.start();
        this.setSize((int)screen.getWidth(), (int)screen.getHeight());
        this.setLocation(0, 0);
        this.setVisible(true);
    }
    
    public void init() {
        con = this.getContentPane();
        con.setLayout(new BorderLayout());
        con.add("Center", sp);
        tree_jsp.setPreferredSize(new Dimension(200, (int)screen.getHeight()));
        sp.setLeftComponent(tree_jsp);
        JPanel jp = new JPanel(new BorderLayout());
        view_jsp.setPreferredSize(new Dimension((int)screen.getWidth()-220, 300));
        jp.add("Center", view_jsp);
        JPanel jp1 = new JPanel(new GridLayout(1, 4, 5, 5));
        jp1.add(view_bt);
        jp1.add(edit_bt);
        jp1.add(del_bt);
        jp1.add(end_bt);
        jp.add("South", jp1);
        sp1.setTopComponent(jp);
        sp1.setBottomComponent(data_jsp);
        sp.setRightComponent(sp1);
        
        File[] file = File.listRoots();
        for(int i = 0; i < file.length; i++) {
            DefaultMutableTreeNode dmt = new DefaultMutableTreeNode(file[i]);
            dmt.add(new DefaultMutableTreeNode("EMPTY");
            root.add(dmt);
        }
    }   
    public void start() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tree_jt.addTreeWillExpandListener(this);
        view_bt.addActionListener(this);
        edit_bt.addActionListener(this);
        del_bt.addActionListener(this);
        end_bt.addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent e) {
        if( e.getSource() == view_bt ) {
            actionView();
        } else if( e.getSource() == edit_bt ) {
            actionEdit();
        } else if( e.getSource() == del_bt ) {
            actionDel();
        } else if( e.getSource() == end_bt ) {
            actionEnd();
        }
    }
    
    private void actionView(){
        TreePath tp = tree_jt.getSelectionPath();
        //System.out.println("tp = " + tp);
        StringTokenizer stk = new StringTokenizer(tp.toString(), "[,]");
        stk.nextToToken();
        if( stk.hasMoreTokens() ) {
            String filepath = stk.nextToken().trim();
            while( stk.hasMoreTokens() ) {
                filepath += stk.nextToken().trim() + "/";
            }
            String filename = (String)view_li.getSelectedValue();
            filename = filename.substring(0, filename.indexOf("(")).trim();
            File f = new File(filepath, filename);
            try {
                BufferedReader in = new BufferedReader(new FileReader(f));
                data_ta.setText("");
                while(true) {
                    String str = in.readLine();
                    if( str == null ) {
                        break;
                    }
                    data.append(str + "\n");
                }
                in.close();
            } catch( IOException e) {
            }
        }
    }
    private void actionEdit() {
        TreePath tp = tree_jt.getSelectionPath();
        //System.out.println("tp = " + tp);
        StringTokenizer stk = new StringTokenizer(tp.toString(), "[,]");
        stk.nextToToken();
        if( stk.hasMoreTokens() ) {
            String filepath = stk.nextToken().trim();
            while( stk.hasMoreTokens() ) {
                filepath += stk.nextToken().trim() + "/";
            }
            String filename = (String)view_li.getSelectedValue();
            filename = filename.substring(0, filename.indexOf("(")).trim();
            File f = new File(filepath, filename);
            
            int xx = JOptionPane.showConfirmDialog(this,
                "정말 수정하시겠습니까?", "수정",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE
            );
            if( xx == 0 ) {
                try {
                    DataOutputStream dos = new DataOutputStream(
                        new BufferedOutputStream(new FileOutputStream(f))
                    );
                    byte[] data = data_ta.getText().trim().getBytes();
                    dos.write(data);
                    dos.close();
                } catch(IOException e) {}
            }
        }        
    }
    private void actionDel() {
        TreePath tp = tree_jt.getSelectionPath();
        //System.out.println("tp = " + tp);
        StringTokenizer stk = new StringTokenizer(tp.toString(), "[,]");
        stk.nextToToken();
        if( stk.hasMoreTokens() ) {
            String filepath = stk.nextToken().trim();
            while( stk.hasMoreTokens() ) {
                filepath += stk.nextToken().trim() + "/";
            }
            String filename = (String)view_li.getSelectedValue();
            filename = filename.substring(0, filename.indexOf("(")).trim();
            File f = new File(filepath, filename);
            
            int xx = JOptionPane.showConfirmDialog(this,
                "정말 삭제하시겠습니까?", "삭제",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE
            );
            if( xx == 0 ) {
                f.delete();
                view_vc.clear();
                view_li.setListData(view_vc);
                data_ta.setText("");
            }
        }                
    }
    private void actionEnd() {
        System.exit(0);
    }
    
    public void treeWillExpand(TreeExpansionEvent e) {
        if( e.getSource() == tree_jt ) {
            tree_jt.setSelectionPath(e.getPath());
            TreePath tp = tree_jt.getSelectionPath();
            //System.out.println("tp = " + tp);
            StringTokenizer stk = new StringTokenizer(tp.toString(), "[,]");
            stk.nextToToken();
            if( stk.hasMoreTokens() ) {
                String filepath = stk.nextToken().trim();
                while( stk.hasMoreTokens() ) {
                    filepath += stk.nextToken().trim() + "/";
                }
                File dir = new File(filepath);
                File[] data = dir.listFiles();
                
                if( data == null ) return;
                
                DefaultMutableTreeNode imsi = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                imsi.removeAllChildren();
                view_vc.clear();
                if( data.length == 0 ) {
                    imsi.add(new DefaultMutableTreeNode("EMPTY"));
                } else {
                    int count = -1;
                    for(int i = 0; i < data.length; i++) {
                        if(data[i].isDirectory()) {
                            DefaultMutableTreeNode dtm = new DefaultMutableTreeNode(data[i].getName());
                            dtm.add(new DefaultMutableTreeNode("EMPTY"));
                            imsi.add(dtm);
                            count++;
                        } else {
                            view_vc.add(
                                data[i].getName() 
                                + " (" + data[i].length() "byte, "
                                + new Data(data[i].lastModified()) + ")"
                            );
                        }
                    }
                    if( count == -1 ) {
                        imsi.add(new DefaultMutableTreeNode("EMPTY"));
                    }
                }
                view_li.setListData(view_vc);
            } 
        }
    }
    
    public void treeWillCollapse(TreeExpansionEvent e) {
    
    }
}
public class Round23_Ex07 {
    public static void main(String[] args) {
        try {
            // 폼에 대한 LookAndFeel 을 지정한다.
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch( Exception e) {
        }    
        
        new Round23_Ex07_Sub();
    }
}