Ian Hollis
Ian Hollis

Reputation: 21

Adding multiple buttons to a Java frame efficiently using NetBeans

Good Evening all

I'm attempting to add multiple buttons to a frame on the press of a previous button so lets say I have a go button on a frame when I press that I want to be able to display a number of buttons that represent months while removing the Go button previously (I'd post an image but not enough reputation points).

I've tried putting them in a panel and making them visible as the panel becomes visible but that doesn't seem to work.

Once I've pressed the month button I then want a bunch of buttons to be shown that correspond to dates

Any clues as to how to do this

You may have guessed I'm fairly new to Java I haven't posted any code as I'd rather get a rough idea as to how this can be achieved then attempt to do it myself so to speak

Many Thanks

Upvotes: 0

Views: 463

Answers (2)

Ian Hollis
Ian Hollis

Reputation: 21

That is how I did it initially, added buttons for go all of the months and the dates to the initial frame made all the buttons I didn't want to see invisible when the fram was first made then as I clicked a button made each individual button visible or not depending on what I wanted to show.

that seems a fairly inefficient way to go about it though for instance when I select the Month I have a line of code to such as

jButtonJan.setVisible(false);

so I have 12 of these

then I have

jButtonDay1.setVisible(true);

so I have upto 31 of these.

Isn't there a more efficient way of doing this a for loop maybe

I tried to set up a string variable that would increment the last number so

private String month = "jButtonMonth" + i;

Where i is determined by the for loop but

if I then try

month.setVisible(false);

I get an error ???

Ideas?

Upvotes: 0

Ajak6
Ajak6

Reputation: 747

add all the buttons at the time of frame initialization. In the actionlistners of each but set the new buttons which you require as visible.

Upvotes: 1

Related Questions