How to do a few ordinary things in Java and NetBeans

    Java is a vast landscape in which the tourist can easily become lost.  Fortunately the answer to almost any question can be found by searching Google.  Unfortunately the correct answer is sometimes masked by wrong answers.  Some Java Forums employ scoring systems for answers.  However, these too can be misleading, “that question has been asked before” rating higher than “the answer to your question is …”

    Questions and answers on this page can be found in other sources.  They are reproduced here because at some point in my own experimentation with Java, each was a puzzle that had to be resolved either by trial and error or by searching the Internet.  The items below are in no particular order.

    How do you ‘rewind’ a sound clip?

soundClip.setFramePosition(0);  // Similar to rewind

    Can a function have a variable number of arguments?

Yes.  Here is an example.

    public static String toChar(int... asciiList) {
        String result = "";
        for (int value : asciiList) {
            if (value == -1) continue;
            result = result + (char) value;
        return result;

    How do you pause execution, for example, insert a timed delay between successive image updates?

I don’t know.  The suggestions that I tried did not work.

    How do you make check-box menu items behave like radio buttons?

Create a button group  and add the menu items to it –

javax.swing.ButtonGroup myGroup = new javax.swing.ButtonGroup();

    How do you prevent visual components from jumping around in NetBeans?

 This is equivalent to disabling automatic component alignment.  Right-click the container that you want to fix.  Select 'Set Layout' and then 'Null Layout'

Null Layout

    How do you change the cursor in a JFrame?

import java.awt.Cursor;
. . .
Cursor spinningCursor = new Cursor(Cursor.WAIT_CURSOR);
. . .

   Don't forget to change it back!

    Is it possible to edit the properties of an invisible component in NetBeans?

Yes.  This situation arises when you delete the text from a label, for example, causing it to shrink to point size.

1.    In 'Design' view select the Inspector tab (a vertical tab near the top left corner of the IDE.
2.    Locate and expand the parent object (e.g. frame) that contains the invisible component.
3.    Right-click the component name and choose 'Properties'.

    Is there a way to retrieve (get) sequential lines (substrings terminated by EOL) from a JTextArea?

Yes, but there is no canned method for this.  You have to do it explicitly.  An example may be found at -

    How do you change the title of the startup form in NetBeans?

So easy, and yet unintuitive (to me)... Select the application's 'About' box in design mode.  Then edit the appTitleLabel component's text.

    How do you add a component to the Swing Controls palette in NetBeans?

A nicely illustrated answer is given here:

    How do you delete unused events in NetBeans?

This problem arises when you accidentally click the wrong event, when getting ready to code an event for a component.

1.    Right-click the component, choose 'Properties' and then select the 'Events' tab of the properties form.
2.    Find the spurious event and delete its method name.  This will cause Netbeans to remove the stub method from the source.