Example usage for javax.swing JFrame getToolkit

List of usage examples for javax.swing JFrame getToolkit

Introduction

In this page you can find the example usage for javax.swing JFrame getToolkit.

Prototype

public Toolkit getToolkit() 

Source Link

Document

Returns the toolkit of this frame.

Usage

From source file:SizingWindowswithToolkit.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is the Window Title");
    Toolkit theKit = aWindow.getToolkit();
    Dimension wndSize = theKit.getScreenSize();
    aWindow.setBounds(wndSize.width / 4, wndSize.height / 4, // Position
            wndSize.width / 2, wndSize.height / 2); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setVisible(true);/*from   w  w w  .  j ava 2 s . com*/
}

From source file:CreatingCursors.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is the Window Title");
    Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
    Dimension wndSize = theKit.getScreenSize(); // Get screen size
    // Set the position to screen center & size to half screen size
    aWindow.setBounds(wndSize.width / 4, wndSize.height / 4, // Position
            wndSize.width / 2, wndSize.height / 2); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    aWindow.setVisible(true); // Display the window
}

From source file:PopupMenu.java

public static void main(String[] args) {
    JFrame frame = new JFrame("JPopupMenu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final Toolkit toolkit = frame.getToolkit();

    final JPopupMenu menu = new JPopupMenu();
    JMenuItem menuItemBeep = new JMenuItem("Beep");

    menuItemBeep.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toolkit.beep();/*  w ww  . j  a  v  a 2s.c om*/
        }
    });

    menu.add(menuItemBeep);

    JMenuItem menuItemClose = new JMenuItem("Close");
    menuItemClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    menu.add(menuItemClose);
    frame.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            if (e.getButton() == e.BUTTON3) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Clip Image");
    Container contentPane = frame.getContentPane();

    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();

    Icon icon = new ImageIcon("jaeger.jpg");
    final JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());

    JScrollPane pane = new JScrollPane(label);
    contentPane.add(pane, BorderLayout.CENTER);

    JButton copy = new JButton("Copy");
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TransferHandler handler = label.getTransferHandler();
            handler.exportToClipboard(label, clipboard, TransferHandler.COPY);
        }/*from  w ww .  j a v a 2  s. co m*/
    });

    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            label.setIcon(null);
        }
    });

    JButton paste = new JButton("Paste");
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Transferable clipData = clipboard.getContents(clipboard);
            if (clipData != null) {
                if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    TransferHandler handler = label.getTransferHandler();
                    handler.importData(label, clipData);
                }
            }
        }
    });

    JPanel p = new JPanel();
    p.add(copy);
    p.add(clear);
    p.add(paste);
    contentPane.add(p, BorderLayout.SOUTH);
    frame.setSize(300, 300);
    frame.show();
}

From source file:DragImage.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Clip Image");
    Container contentPane = frame.getContentPane();

    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();

    Icon icon = new ImageIcon("jaeger.jpg");
    final JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());

    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, e, TransferHandler.COPY);
        }// ww w  . j  a v a2 s  .  c o  m
    };
    label.addMouseListener(mouseListener);

    JScrollPane pane = new JScrollPane(label);
    contentPane.add(pane, BorderLayout.CENTER);

    JButton copy = new JButton("Copy");
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TransferHandler handler = label.getTransferHandler();
            handler.exportToClipboard(label, clipboard, TransferHandler.COPY);
        }
    });

    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            label.setIcon(null);
        }
    });
    clear.setTransferHandler(new TransferHandler("text"));
    mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            JComponent comp = (JComponent) e.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, e, TransferHandler.COPY);
        }
    };
    clear.addMouseListener(mouseListener);

    JButton paste = new JButton("Paste");
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Transferable clipData = clipboard.getContents(clipboard);
            if (clipData != null) {
                if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    TransferHandler handler = label.getTransferHandler();
                    handler.importData(label, clipData);
                }
            }
        }
    });

    JPanel p = new JPanel();
    p.add(copy);
    p.add(clear);
    p.add(paste);
    contentPane.add(p, BorderLayout.SOUTH);

    JTextField tf = new JTextField();
    tf.setDragEnabled(true);
    contentPane.add(tf, BorderLayout.NORTH);
    frame.setSize(300, 300);
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = (JPanel) frame.getContentPane();
    panel.setLayout(new BorderLayout());
    JTextField field = new JTextField(20);
    Main spinner = new Main();

    panel.add(field, "Center");
    panel.add(spinner, "East");

    Dimension dim = frame.getToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2 - frame.getHeight() / 2);
    frame.pack();//ww  w.  ja v  a2 s .c  o m
    frame.show();
}

From source file:DateTimeEditor.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = (JPanel) frame.getContentPane();
    panel.setLayout(new BorderLayout());
    JTextField field = new JTextField(20);
    Spinner spinner = new Spinner();

    panel.add(field, "Center");
    panel.add(spinner, "East");

    Dimension dim = frame.getToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2 - frame.getHeight() / 2);
    frame.pack();//www  .j  ava2s.com
    frame.show();
}

From source file:DateTimeEditor.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);// w w w .j  a va2 s. c  o  m
        }
    });

    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(new EmptyBorder(5, 5, 5, 5));
    frame.setContentPane(panel);
    final DateTimeEditor field = new DateTimeEditor(DateTimeEditor.DATETIME, DateFormat.FULL);
    panel.add(field, "North");

    JPanel buttonBox = new JPanel(new GridLayout(2, 2));
    JButton showDateButton = new JButton("Show Date");
    buttonBox.add(showDateButton);

    final JComboBox timeDateChoice = new JComboBox();
    timeDateChoice.addItem("Time");
    timeDateChoice.addItem("Date");
    timeDateChoice.addItem("Date/Time");
    timeDateChoice.setSelectedIndex(2);
    timeDateChoice.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            field.setTimeOrDateType(timeDateChoice.getSelectedIndex());
        }
    });
    buttonBox.add(timeDateChoice);

    JButton toggleButton = new JButton("Toggle Enable");
    buttonBox.add(toggleButton);
    showDateButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            System.out.println(field.getDate());
        }
    });
    toggleButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            field.setEnabled(!field.isEnabled());
        }
    });
    panel.add(buttonBox, "South");

    final JComboBox lengthStyleChoice = new JComboBox();
    lengthStyleChoice.addItem("Full");
    lengthStyleChoice.addItem("Long");
    lengthStyleChoice.addItem("Medium");
    lengthStyleChoice.addItem("Short");
    lengthStyleChoice.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            field.setLengthStyle(lengthStyleChoice.getSelectedIndex());
        }
    });
    buttonBox.add(lengthStyleChoice);

    frame.pack();
    Dimension dim = frame.getToolkit().getScreenSize();
    frame.setLocation(dim.width / 2 - frame.getWidth() / 2, dim.height / 2 - frame.getHeight() / 2);
    frame.show();
}

From source file:org.gridchem.client.GridChem.java

public static void main(String[] args) {
    Trace.entry();/* w w w . j a  v a2 s.  c o  m*/

    try {
        setCommandLineOptions();
        parseCommandLine(args);
    } catch (Exception e) {
        System.out.println("Invalid command line option given: " + e.getMessage());
    }

    //JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();

    // Edited by Shashank & Sandeep @ CCS,UKY April 10 2005
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    /*MetalTheme theme = new G03Input.ColorTheme();  
    MetalLookAndFeel.setCurrentTheme(theme);
    try {
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        SwingUtilities.updateComponentTreeUI(frame);
    } catch(Exception e) {
        System.out.println(e);
    }*/

    // Use the native look and feel since Java's is pretty lame.
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        // Ignore exceptions, which only result in the wrong look and feel.
        System.out.println("GridChem.main: an exception related to the" + " look and feel was ignored.");
    }

    init();

    frame.setTitle("GridChem " + Env.getVersion());
    frame.getContentPane().add(oc);
    frame.addWindowListener(new WindowListener() {

        public void windowActivated(WindowEvent arg0) {
        }

        public void windowClosed(WindowEvent arg0) {
        }

        public void windowClosing(WindowEvent arg0) {
            if (Settings.authenticated) {
                GMS3.logout();
            }
        }

        public void windowDeactivated(WindowEvent arg0) {
        }

        public void windowDeiconified(WindowEvent arg0) {
        }

        public void windowIconified(WindowEvent arg0) {
        }

        public void windowOpened(WindowEvent arg0) {
        }

    });
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();

    // Centering the frame on the screen
    Toolkit kit = frame.getToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenWidth = screenSize.width;
    int screenHeight = screenSize.height;
    Dimension windowSize = frame.getSize();
    int windowWidth = windowSize.width;
    int windowHeight = windowSize.height;
    int upperLeftX = (screenWidth - windowWidth) / 2;
    int upperLeftY = (screenHeight - windowHeight) / 2;
    frame.setLocation(upperLeftX, upperLeftY);
    frame.setVisible(true);

    Trace.exit();
}