Study Area Facebook粉絲團http://www.facebook.com/sataiwan
0 會員 與 1 訪客 正在閱讀本文。
/* * Jdirchosser_tar.java * * Created on 2005年8月14日, 下午 3:23 */package layouttools;import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.*;import java.io.*;/** * * @author yplin */public class Jdirchosser_tar extends javax.swing.JDialog { public Jdirchosser jr; /** Creates new form Jdirchosser_tar */ public Jdirchosser_tar(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); jr=new Jdirchosser(new javax.swing.JFrame(),false,jTextField1,jList1); jr.treeModel(jScrollPane2); setTitle("選擇一個目錄或多個檔案TAR:"); setSize(800,600); this.setLocation(100, 50); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList(); jScrollPane2 = new javax.swing.JScrollPane(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jScrollPane3 = new javax.swing.JScrollPane(); jTextField1 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); getContentPane().setLayout(null); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jScrollPane1.setViewportView(jList1); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(390, 60, 310, 360); getContentPane().add(jScrollPane2); jScrollPane2.setBounds(20, 60, 330, 360); jButton1.setText("\u6253\u5305"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { jButton1MousePressed(evt); } }); getContentPane().add(jButton1); jButton1.setBounds(70, 480, 90, 25); jButton2.setText("\u53d6\u6d88"); getContentPane().add(jButton2); jButton2.setBounds(580, 480, 88, 25); jScrollPane3.setViewportView(jTextField1); getContentPane().add(jScrollPane3); jScrollPane3.setBounds(70, 423, 630, 40); jLabel1.setFont(new java.awt.Font("Dialog", 1, 18)); jLabel1.setForeground(new java.awt.Color(255, 0, 255)); jLabel1.setText("\u76ee\u9304:"); getContentPane().add(jLabel1); jLabel1.setBounds(20, 430, 50, 30); jLabel2.setFont(new java.awt.Font("Dialog", 1, 18)); jLabel2.setForeground(new java.awt.Color(0, 51, 255)); jLabel2.setText("\u6a94\u6848\u7e3d\u7ba1"); getContentPane().add(jLabel2); jLabel2.setBounds(35, 15, 130, 30); jLabel3.setFont(new java.awt.Font("Dialog", 1, 18)); jLabel3.setForeground(new java.awt.Color(0, 153, 255)); jLabel3.setText("\u6a94\u6848"); getContentPane().add(jLabel3); jLabel3.setBounds(405, 15, 120, 30); jButton3.setText("\u6253\u5305\u4e26\u58d3\u7e2e"); getContentPane().add(jButton3); jButton3.setBounds(320, 480, 100, 25); pack(); } // </editor-fold> private void jButton1MousePressed(java.awt.event.MouseEvent evt) { } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Jdirchosser_tar(new javax.swing.JFrame(), true).setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JList jList1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JTextField jTextField1; // End of variables declaration }************************************************** * Jdirchosser.java * * Created on 2005年8月7日, 下午 6:18 */package layouttools;import java.io.File;import javax.swing.*;import javax.swing.tree.*;import javax.swing.event.*;import java.awt.*;import java.util.*;/** * * @author yplin * * */public class Jdirchosser extends javax.swing.JDialog { public JTree tree; public static String dirname; public JTextField jf; public JList jl; /** Creates new form Jdirchosser */ //建購元 public Jdirchosser(java.awt.Frame parent, boolean modal,JTextField jjf,JList jll) { super(parent, modal); initComponents(); setTitle("選擇一個目錄:"); setSize(350,500); this.setLocation(250, 150); jf=jjf;//有新TextField使用 jl=jll; } //另一建購元 public Jdirchosser(java.awt.Frame parent, boolean modal,JTextField jjf) { super(parent, modal); initComponents(); setTitle("選擇一個目錄:"); setSize(350,500); this.setLocation(250, 150); jf=jjf;//有新TextField使用 } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { getContentPane().setLayout(null); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); pack(); } // </editor-fold> /** * @param args the command line arguments */ // Variables declaration - do not modify // End of variables declaration public void treeModel(JScrollPane jp) { //tree 由home目錄展 File start = new File(System.getProperty("user.home"));//+"/layout"); TreeModel model = new DefaultTreeModel(new Node(start)); tree = new JTree(model); //把tree tree.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent e) { treeMousePressed(e, jf); } }); jp.setViewportView(tree); } //實做滑鼠事件 public void treeMousePressed(java.awt.event.MouseEvent e,JTextField jf){ JTree jtr = (JTree)e.getSource(); int row = jtr.getRowForLocation(e.getX(),e.getY()); if(row != -1){ dirname=gettreepath(tree,row); jf.setText(dirname); jl.setListData(listfile().toArray()); }} //取得path public static String gettreepath(JTree jt,int row){ String st=System.getProperty("user.home"); for(int i=1;i<jt.getPathForRow(row).getPathCount();i++) { st=st+"/"+jt.getPathForRow(row).getPathComponent(i).toString(); } return st; } //檔案列表 private ArrayList listfile(){ File dir=new File(dirname); String[] list = dir.list(); ArrayList newlist=new ArrayList(0); int j=0; if( list == null ){ return null; } for(int i=0; i<list.length; ++i){ File child = new File(dir, list[i]); if (child.isFile()){ newlist.add(list[i]); j++; } } return newlist;//回傳檔案列表 } //class node 是一個 繼承 DefaultMutableTreeNode 並OVERWRITE NODE()static class Node extends DefaultMutableTreeNode{ Node(File dir){ super( dir.getName() // dir.getParent() ); fillChildren(dir); } //目錄列表NODE public void fillChildren(File dir){ String[] list = dir.list(); if( list == null ){ return; } for(int i=0; i<list.length; ++i){ File child = new File(dir, list[i]); if(child.isDirectory() && !child.isHidden()){ //if(child.isFile()){ add(new Node(child)); } } } } }