Off The Top Of My Head

The GreekLetterWidget

Posted in Uncategorized by waltermilner on June 17, 2009

I’m currently writing some maths software in which I wanted to be able to use Greek letters as well as Roman. Since Java uses Unicode, this was no problem internally. But how could the user input Greek as well as Roman characters, through a standard US/UK keyboard, and without obscure and slow character codes?

So I wrote a widget which had 25 buttons, labelled with the lower case Greek letters. It has an instance variable which is a reference to a textfield. When the user clicks a button, the corresponding character is appended to that textfield, and focus is placed on that textfield, ready for the next keystroke.

Here it is:

/**
 * This is a UI widget supporting the input of Greek letters
 * It produces a 5 X 5 grid of buttons, labelled with lower case Greek letters
 * When a button is clicked, the corresponding letter is appended to a supplied
 * text field
 * @author Walter Milner
 */
public class GreekLetterWidget extends JComponent implements ActionListener {
    private MyButton[] buttons = new MyButton[25];
    private MyTextField textField;
/**
 * make an instance, of size 100 by 100
 * @param textField The textfield to which the letter will be appended
 */
    public GreekLetterWidget(MyTextField textField) {
        this.textField = textField;
        setLayout(new GridLayout(5, 5));
        setSize(100, 100);
        // Greek letter buttons
        char[] c = new char[1];
        c[0] = ‘\u03B1′;
        String s = new String(c);
        Insets inset = new Insets(1, 1, 1, 1);
        for (int i = 0; i < 25; i++) {
            buttons[i] = new MyButton(s);
            buttons[i].setToolTipText(s);
            c[0]++;
            s = new String(c);
            buttons[i].setMargin(inset);
            buttons[i].addActionListener(this);
            add(buttons[i]);
        }
    }
    public void setField(MyTextField target)
    {
        textField=target;
    }
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        textField.setText(textField.getText() + ((MyButton) obj).getText());
        textField.requestFocus();
    }
}

This looks like:

screenshot

screenshot

The widget is used (in a sub-classed JDialog using a GridBagLayout) with code like this
glw = new GreekLetterWidget(funcTextField);
c.gridx = 3;
c.gridy = 0;
c.gridheight = 2;
add(glw, c);

The setTextField() method is provided so that this can be used with more than one text field in a dialog. The textfields need to have a focuslistener, and in the gainFocus, setTextField is called with that text field as the parameter. That means that when the user clicks on a textfield, from then on GreekLetter button clicks go into that text field.

Padding a JLabel

Posted in Uncategorized by waltermilner on June 17, 2009

Googling for this was pretty fruitless, but I eventually got a hint, which worked out OK. The hack is to have an ‘empty’ border around the text. If you also want a visible border, you can combine that with the empty border in a compound border. So you have something like:

public class MyLabel extends JLabel {
    static Font labelFont = new Font(“SansSerif”, Font.PLAIN, 12);
    static Border gap = BorderFactory.createEmptyBorder(4, 2, 4, 2);
    static Border blueline = BorderFactory.createLineBorder(Color.blue);
    static Border compound = BorderFactory.createCompoundBorder(blueline, gap);
    MyLabel(String text) {
        super(text);
        setFont(labelFont);
        setBorder(compound);
    }
}

which looks like:

screenshot

screenshot

Tagged with: , , , ,