Java shortcuts

Java shortcuts, a notebook for myself.

[Back home]

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Shortcuts {
    
    public static void main(String[] args) {
        
        //Quick assignment
        Object[] o = new Object[]{"A", "B", "C"};
        
        //Loop with abbreviated if (without the braces)
        for (Object i : o) {
            if (i.equals("A"))
                System.out.println("A");
            else if (i.equals("B"))
                System.out.println("B");
            else
                System.out.println("Neither A nor B");
        }
        
        //Must specify size at initiation
        Object[] p = new Object[o.length + 2];
        p[0] = "a";
        p[1] = "b";
        p[2] = "c";
        p[3] = "d";
        p[4] = "e";
        System.out.println(p[4]);
        
        for (int i = 0; i < p.length; i++) {
            GUI.popup("Index " + i + "'s value is:", p[i].toString() );
        }
    }
}

abstract class GUI {
    // Show a popup during code execution, handy for test automation,
    // without having to build a whole GUI framework :-)
    public static void popup(String title, String message) {
        String markup = "" + message + "";
        JLabel l = new JLabel(markup);
        l.setHorizontalAlignment(SwingConstants.CENTER);
        JPanel p = new JPanel(new java.awt.GridLayout(0, 1));
        p.add(l);
        JFrame f = new JFrame(title);
        f.setContentPane(p);
        f.setSize(250, 100);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() < (startTime + 1000)) {
            f.setVisible(true);
        }
        f.setVisible(false);
        f.dispose();
    }
}


© 2001-2023 ou-ryperd.net