Simple Java DeskTop Demo Program

by Vinoth 2009-02-27 19:38:50

The bellow Code will help us to show Desk top demo example.


import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class DeskTop{
static Desktop desktop;
public static void main(String[] args) {
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
} else {
System.out.println("Desktop class is not supported");
System.exit(1);
}
JMenuItem openItem = new JMenuItem("Open");
JMenuItem printItem = new JMenuItem("Print");
JMenu fileMenu = new JMenu("File");
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try {
desktop.open(chooser.getSelectedFile().getAbsoluteFile());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
fileMenu.add(openItem);
printItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try {
desktop.print(chooser.getSelectedFile().getAbsoluteFile());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
fileMenu.add(printItem);
JMenuBar jMenuBar = new JMenuBar();
jMenuBar.add(fileMenu);
JFrame frame = new JFrame();
frame.setSize(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(jMenuBar);
frame.setVisible(true);

}
}

Tagged in:

2034
like
0
dislike
0
mail
flag

You must LOGIN to add comments