Magazine
 
Tips & Tricks
 

JFrame frame = new JFrame(“Adding a Node to a JTree
Component!”);
JPanel panel = new JPanel();
DefaultMutableTreeNode myComputer = new
DefaultMutableTreeNode(“My Computer”);
DefaultMutableTreeNode c = new
DefaultMutableTreeNode(“Local Disk(C:)”);
DefaultMutableTreeNode vinod = new
DefaultMutableTreeNode(“Vinod”);
DefaultMutableTreeNode swing = new
DefaultMutableTreeNode(“Swing”);
DefaultMutableTreeNode tr = new
DefaultMutableTreeNode(“Tree”);
DefaultMutableTreeNode a = new
DefaultMutableTreeNode(“3½ Floppy(A:)”);
DefaultMutableTreeNode e = new
DefaultMutableTreeNode(“New Volume(E:)”);
c.add(vinod); vinod.add(swing); swing.add(tr);
myComputer.add(c);
myComputer.add(a);
myComputer.add(e);
tree = new JTree(myComputer);
JButton button = new JButton(“Add Tree”);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
model = (DefaultTreeModel)tree.getModel();
String nodeName = JOptionPane.showInputDialog(null, “Enter
the node name:”);
if(nodeName.equals(“”)){
JOptionPane.showMessageDialog(null, “Node is not added in
the tree!”);
}
else{
// create a new node
nNode = new DefaultMutableTreeNode(nodeName);
path = tree.getNextMatch(“M”, 0, Position.Bias.Forward);
node = (MutableTreeNode)path.getLastPathComponent();
model.insertNodeInto(nNode, node, node.getChildCount());
JOptionPane.showMessageDialog(null, “Node is added in the
tree!”); } } });
panel.add(tree);
panel.add(button);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindow
DecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
}}

 

Output:


Know to Make a Non Resizable Frame in Java

The setResizable(): This method is also a part of JFrame class. This method is used to disable or enable the size of the frame. It takes the Boolean value i.e. true or false. If you pass the value false then the frame or window will be non-resizable and on the contrary if you pass true then it will be resizable.

import javax.swing.*;
public class SwingFrameNonResizable{
public static void main(String[] args){
JFrame frame = new JFrame(“Non Resizable Frame”);
frame.setResizable(false);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

Here is the Output

August 2007 | Java Jazz Up | 35
 
previous
index
next
 
Pages: 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            Download PDF