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

intaskSave(JFrame frame)
ask Save
return JOptionPane.showOptionDialog(frame, SAVE_QUESTION_MESSAGE, SAVE_QUESTION_TITLE,
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, JOptionPane.YES_OPTION);
voidattachAccelerator(Action action, JFrame frame)
attach Accelerator
KeyStroke stroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
String name = (String) action.getValue(Action.NAME);
JComponent component = (JComponent) frame.getContentPane();
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, name);
component.getActionMap().put(name, action);
addStrokeToName(action);
voidbtnMnemonicAndToolTip(JFrame frame, JButton a, JButton b, JButton c, JButton e, JButton h, JButton i, JButton l, JButton m, JButton o, JButton p, JButton s, JButton t, JButton enter, JButton altEnter)
btn Mnemonic And Tool Tip
a.setMnemonic(KeyEvent.VK_A);
b.setMnemonic(KeyEvent.VK_B);
c.setMnemonic(KeyEvent.VK_C);
e.setMnemonic(KeyEvent.VK_E);
h.setMnemonic(KeyEvent.VK_H);
i.setMnemonic(KeyEvent.VK_I);
l.setMnemonic(KeyEvent.VK_L);
m.setMnemonic(KeyEvent.VK_M);
...
JButtonbuildButton(String btnName, JFrame frame, int x, int y, int w, int h)
build Button
JButton btn = new JButton(btnName);
btn.setLayout(null);
btn.setBorderPainted(true);
frame.add(btn);
btn.setLocation(x, y);
btn.setSize(w, h);
btn.setForeground(Color.RED);
btn.setOpaque(true);
...
JLabelbuildLabel(JFrame frame, String txt, int x, int y, int w, int h)
build Label
JLabel lbl = new JLabel();
lbl.setText(txt);
lbl.setBounds(x, y, w, h);
lbl.setForeground(Color.yellow);
lbl.setFont(new Font("Canadra", Font.BOLD, 22));
frame.add(lbl);
return lbl;
JTextFieldbuildTxtField(JFrame frame, int x, int y, int w, int h)
build Txt Field
JTextField txt = new JTextField();
txt.setLayout(null);
txt.setBounds(x, y, w, h);
txt.setFont(new Font("Calibri", Font.BOLD, 20));
txt.setForeground(Color.BLACK);
txt.setBackground(Color.yellow);
txt.setOpaque(true);
txt.setLayout(null);
...
PointcalcLocation(JFrame main, JDialog dialog)
calc Location
int mainWidth = main.getWidth();
int mainX = main.getX();
int dialogWidth = dialog.getWidth();
int x;
if ((mainX + mainWidth + dialogWidth) > SCREEN_WIDTH) {
    x = mainX - dialogWidth - 50;
} else {
    x = mainX + mainWidth + 50;
...
intcalculateMaxMenuItems(JFrame window)
calculate Max Menu Items
UIDefaults def = UIManager.getDefaults();
Font itemFont = def.getFont("MenuItem.font");
Font barFont = def.getFont("MenuBar.font");
int itemHeight = 16;
int barHeight = 24;
FontMetrics fm = itemFont == null ? null : window.getFontMetrics(itemFont);
if (fm != null) {
    itemHeight = fm.getHeight();
...
intcalculateMaxMenuItems(JFrame window)
calculate Max Menu Items
UIDefaults def = UIManager.getDefaults();
Font itemFont = def.getFont("MenuItem.font");
Font barFont = def.getFont("MenuBar.font");
int itemHeight = 16;
int barHeight = 24;
FontMetrics fm = window.getFontMetrics(itemFont);
if (fm != null) {
    itemHeight = fm.getHeight();
...
voidcentre(JFrame frame)
centre
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = frame.getSize();
screenSize.height = screenSize.height / 2;
screenSize.width = screenSize.width / 2;
size.height = size.height / 2;
size.width = size.width / 2;
int y = screenSize.height - size.height;
int x = screenSize.width - size.width;
...