Magazine
 
Tips & Tricks
 

JButton button = new JButton(“Capture Screen Shot”);
button.addActionListener(new MyAction());
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); }
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent ae){
try{
String fileName = JOptionPane.showInputDialog(null, “Enter file
name : “, “Roseindia.net”, 1);
if (!fileName.toLowerCase().endsWith(“.gif”)){
JOptionPane.showMessageDialog(null, “Error: file name must
end with \”.gif\”.”, “Roseindia.net”, 1);
}
else{
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(new
Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, “gif”, new File(fileName));
JOptionPane.showMessageDialog(null, “Screen captured
successfully.”, “Roseindia.net”, 1);
}}
catch(Exception e){}
}
}}

Output:


 

Know to add a Node to the JTree Component

Here is a program that demonstrates how to insert a node to the JTree.

Description of the code:

getModel(): This method returns a data model. A list of items that needs to be displayed by the JList component is stored in this data model.

getNextMatch(String prefix, int startIndex, Position.Bias bias): This method returns the index of the next list element> started with a given prefix otherwise would start with ‘1’. It takes the following arguments:

prefix: This is the string to test for matching the JTree component.

startIndex: This is an index to start the search.

bias: This indicates the search direction that can be either Position.Bias.Forward or Position.Bias.Backward.

Forward: This field indicates to bias toward the next character in model.

Backward: This field indicates to bias toward the previous character in model.

getLastPathComponent():
This method returns the last component of tree path.

insertNodeInto(MutableTreeNode nNode, MutableTreeNode node, int index):This method is used to insert a new child at specified location.

A graphical layout appears on the screen once you run this program. It displays a tree with root node, child of root node and one command button (Add Tree). After clicking the button an input box comes up that asks for a node name to be inserted in the JTree. However if the input box is empty or blank then click the “OK” button. None of the nodes is added after clicking on the “OK” button and a message will be displayed as “Node is not added in the tree” in message box. But then if you again click on the “Add Tree” command button then an input box comes up again that asks a node name to be inserted. Hence the given node is added to the JTree when you click the “OK” button. And it displays a message “Node is added in the tree”.

import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.text.*;
public class AddNodes{
public static DefaultTreeModel model;
public static TreePath path; public static JTree tree;
public static DefaultMutableTreeNode nNode;
public static MutableTreeNode node;
String nodeName;
public static void main(String[] args) {

August 2007 | Java Jazz Up | 34
 
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