Java Utililty Methods JFrame

List of utility methods to do JFrame

Description

The list of methods to do JFrame are organized into topic(s).

Method

voidcentreJustifyFrame(final JFrame frame, final int y_position)
Move the given JFrame to the centre of the screen.
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
final int x_position = (screen.width - frame.getSize().width) / 2;
frame.setLocation(new Point(x_position, y_position));
voidcentreWindow(Window c, JFrame frame)
centre Window
Dimension app = frame.getSize();
int x = frame.getX() + (app.width - c.getWidth()) / 2;
int y = frame.getY() + (app.height - c.getHeight()) / 3;
if (y < frame.getY()) {
    y = frame.getY();
c.setLocation(x, y);
voidchangeLookAndFeel(final String lookName, final JFrame frame)
change Look And Feel
try {
    UIManager.setLookAndFeel(lookName);
    SwingUtilities.updateComponentTreeUI(frame);
    frame.pack();
} catch (Exception e) {
FilechooseFile(String directoryPath, String dialogTitle, String selectedFileName, boolean saveDialog, String[] extensions, JFrame parentFrame)
choose File
File file = null;
JFileChooser fc = null;
if (directoryPath != null) {
    fc = new JFileChooser(directoryPath);
} else {
    fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
...
voidChooserDemo(JFrame jf, Component comp)
Chooser Demo
JMenuItem itemOpen, itemSave, itemDir, itemColor;
JMenuBar mnuBar = new JMenuBar();
jf.setJMenuBar(mnuBar);
JMenu mnu = new JMenu("File");
mnuBar.add(mnu);
itemOpen = new JMenuItem("Buka File");
mnu.add(itemOpen);
itemOpen.addActionListener(new ActionListener() {
...
voidcloseFrame(JFrame frame, JProgressBar progressBar)
close Frame
frame.setVisible(false);
frame.setCursor(java.awt.Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
voidcloseOnEsc(JFrame frame)
close On Esc
closeOnKeyStroke(frame.getRootPane(), ESC);
voidcloseOnEscape(final JFrame frame)
close On Escape
KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
Action actionListener = new AbstractAction() {
    public void actionPerformed(ActionEvent actionEvent) {
        frame.setVisible(false);
        frame.dispose();
};
JRootPane rootPane = frame.getRootPane();
...
voidcloseOnEscape(final JFrame frame)
Make the window close when the used presses escape.
closeOnEscape(frame, frame.getRootPane());
voidcloseOnKeyStroke(JFrame frame, KeyStroke keyStroke)
close On Key Stroke
closeOnKeyStroke(frame.getRootPane(), keyStroke);