java tips

1 当前路径:
System.getProperty("user.dir");
启示:注意System的属性。
2 更多路径问题:
http://www.blogjava.net/ericwang/archive/2005/12/16/24301.aspx

3 Vector.toArray() 的使用和Object[]

 booleanadd(E o)
          Appends the specified element to the end of this Vector.
<T> T[]
toArray(T[] a)
          Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array.

    import org.htmlparser.NodeFilter;

Vector < NodeFilter >  filters  =   new  Vector();
filters.add( NodeFilter(aObject));
filters.add( NodeFilter(bObject));
..
NodeFilter[] reFilters 
=   new  NodeFilter[ filters.size()];
reFilters 
=  filters.toArray(reFilters);

下面的代码,不能实现Object[]到NodeList[]的转换,原因待考,
似乎Object[]比较特别。
import  java.util.Vector;

public   class  TestObjectArrayCast ... {

    
/** *//**
     * 
@param args
     
*/

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub

        Vector v 
= new Vector();
        v.add(
"1");
        v.add(
"2");
             
//toArray()返回Object[]
        String[] c =(String[]) v.toArray();//java.lang.ClassCastException
        System.out.println("c:"+c);

    }


}

 

4 文件读取基本技巧

http://www.roseindia.net/java/example/java/io/java-read-file-line-by-line.shtml

5 javabean的property

http://mindprod.com/jgloss/properties.html#JAVABEAN

JavaBean Properties

Property has a second meaning. In JavaBeans, components have associated persistent objects. You can modify various fields in those objects to configure them. The accessible fields of a JavaBean are called properties . They are accessible via public get/set methods. There need not be an actual field, just the get/set methods that simulate one. These are a completely separate mechanism.

6 JTree中使用弹出菜单

http://www.java-tips.org/java-se-tips/javax.swing/have-a-popup-attached-to-a-jtree.html
import  javax.swing. * ;
import  javax.swing.tree. * ;
import  java.awt.event. * ;
import  java.awt. * ;
import  java.util. * ;

public   class  TreeWithPopup  extends  JPanel  {
    
    DefaultMutableTreeNode root, node1, node2, node3;
    
    
public TreeWithPopup() {
        MyJTree tree;
        root 
= new DefaultMutableTreeNode("root"true);
        node1 
= new DefaultMutableTreeNode("node 1"true);
        node2 
= new DefaultMutableTreeNode("node 2" , true);
        node3 
= new DefaultMutableTreeNode("node 3"true);
        root.add(node1);
        node1.add(node2);
        root.add(node3);
        setLayout(
new BorderLayout());
        tree 
= new MyJTree(root);
        add(
new JScrollPane((JTree)tree),"Center");
    }

    
    
public Dimension getPreferredSize(){
        
return new Dimension(300300);
    }

    
    
public static void main(String s[]){
        JFrame frame 
= new JFrame("Tree With Popup");
        TreeWithPopup panel 
= new TreeWithPopup();
        
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setForeground(Color.black);
        frame.setBackground(Color.lightGray);
        frame.getContentPane().add(panel,
"Center");
        
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(
true);
        frame.addWindowListener(
new WindowCloser());
    }

}


class  WindowCloser  extends  WindowAdapter  {
    
public void windowClosing(WindowEvent e) {
        Window win 
= e.getWindow();
        win.setVisible(
false);
        System.exit(
0);
    }

}


class  MyJTree  extends  JTree  implements  ActionListener {
    JPopupMenu popup;
    JMenuItem mi;
    
    MyJTree(DefaultMutableTreeNode dmtn) 
{
        
super(dmtn);
        
// define the popup
        popup = new JPopupMenu();
        mi 
= new JMenuItem("Insert a children");
        mi.addActionListener(
this);
        mi.setActionCommand(
"insert");
        popup.add(mi);
        mi 
= new JMenuItem("Remove this node");
        mi.addActionListener(
this);
        mi.setActionCommand(
"remove");
        popup.add(mi);
        popup.setOpaque(
true);
        popup.setLightWeightPopupEnabled(
true);
        
        addMouseListener(
                
new MouseAdapter() {
            
public void mouseReleased( MouseEvent e ) {
                
if ( e.isPopupTrigger()) {
                    popup.show( (JComponent)e.getSource(), e.getX(), e.getY() );
                }

            }

        }

        );
        
    }

    
public void actionPerformed(ActionEvent ae) {
        DefaultMutableTreeNode dmtn, node;
        
        TreePath path 
= this.getSelectionPath();
        dmtn 
= (DefaultMutableTreeNode) path.getLastPathComponent();
        
if (ae.getActionCommand().equals("insert")) {
            node 
= new DefaultMutableTreeNode("children");
            dmtn.add(node);
            
// thanks to Yong Zhang for the tip for refreshing the tree structure.
            ((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);
        }

        
if (ae.getActionCommand().equals("remove")) {
            node 
= (DefaultMutableTreeNode)dmtn.getParent();
            
// Bug fix by essam
            int nodeIndex=node.getIndex(dmtn); // declare an integer to hold the selected nodes index
            dmtn.removeAllChildren();          // remove any children of selected node
            node.remove(nodeIndex);            // remove the selected node, retain its siblings
            ((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);       }

    }

}

7 JFrame.getFrames()静态方法

返回应用程序创建的Frame对象数组,对于Applet返回该applet可以访问的Frame数组。

8 让Hashtable具有排序的能力

How to sort a Java Hashtable.
http://www.basis.com/support/kb/kb01074.html
















































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值