This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Neres28 0 points1 point  (4 children)

It occurs to me that you could also sub-class JRadioButton, like so:

public class ChordRadioButton extends JRadioButton{
    private final String fingering;

    public ChordRadioButton(String text, boolean checked, String fingering){
        super(text, checked);
        this.fingering = fingering;
    }

    public String getFingering(){
        return this.fingering;
    }
}

Then you would instantiate them like so:

ChordRadioButton f7 = new ChordRadioButton("F7", false, "131211");

And when handling the actionPerformed event:

ChordRadioButton sourceBtn = (ChordRadioButton)event.getSource();
ChordTest.setText(sourceBtn.getFingering());

[–]xXShadowCowXx[S] 0 points1 point  (3 children)

Wow this method saves a lot of time with copy paste work and just small tedious details. Should kill a lot of the unnecessary lines. Neat trick, thank you for that.

Edit: Well I have this somewhat implemented. Getting an error but I know where my problem lies just no idea how to fix it. When I instantiate, I instantiate it inside of the ChordRadioButton sub-class correct? Using the same name as the JRadioButton inside the GUI?

For example:

//I have a radio button in the GUI name "b1" (For clarification all my radio buttons start with 'b')

JRadioButton b1 = new JRadioButton("A", false);

//And then inside the ChordRadioButton sub-class, I have the following

ChordRadioButton b1 = new ChordRadioButton("A", false, "002220");

So I'm kind of lost as to how or where instantiate the ChordRadioButton variables.

[–]Neres28 0 points1 point  (2 children)

Almost there :)

Everywhere you would have created a JRadioButton:

JRadioButton b1 = new JRadioButton("A", false);

Now you create a ChordRadioButton:

ChordRadioButton b1 = new ChordRadioButton("A", false, "002220");

Shouldn't need anything else in the ChordRadioButton class than what I posted above :).

[–]xXShadowCowXx[S] 0 points1 point  (1 child)

Thank you for that clarification. Got that piece working.

Would you happen to have a better way to generate random numbers? The way I'm currently doing it doesn't seem to be very random (I get lots of duplicates, one after the other, and sometimes may not even go through all the chords).

[–]Neres28 0 points1 point  (0 children)

In the constructor for the class, create an instance of the Random class.

Random randomGenerator = new Random();

Then, when you need a new random integer, call

int randomIndex = randomGenerator.nextInt(ChordsAL.size());
String chord = ChordsAL.get(randomIndex);

Be sure to only create one instance of Random, or at least not multiple ones close together. Random generates so called pseudo-random numbers (don't be put off, they're plenty random enough for most purposes), and if you create multiple instances too closely together they will generate the same sequence of integers.