Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to Add JButtons From Number of arrayList ?  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get Database Table names and how many tables my database have then add the same number of tables for example 7 tables = 7 buttons .
what am i trying to do is making a Database Tool which gets how many tables do i have in database using the username "HR" and then it creates to me when i connect to database it creates to me 7 buttons in jpanel .

Here is my code but it doesn't seem to work for me :

Variables :







here is the logic : when i connect to database using a connect button it goes to database and check for how many tables i have if they are 7 , it creates a 7 buttons in jpanel then these 7 buttons goes to another form .

here is a picture from what am i trying to do :



my problem is what is wrong with my btn method it doesn't seem to work .
 
Master Rancher
Posts: 3257
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it doesn't seem to work for me


What  does "doesnt" mean? What happens when the code is executed?
How many buttons are shown?
Where is jPanel4 supposed to be shown?   When is jPanel4 repainted to show the buttons that have been added to it?

Can you make a small, complete program that compiles and executes and shows the problem?    It only needs to have jPanel4 and a container to hold it.  No database or anything else.  Only the bare minimum that shows the problem.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

it doesn't seem to work for me


What  does "doesnt" mean? What happens when the code is executed?
How many buttons are shown?
Where is jPanel4 supposed to be shown?   When is jPanel4 repainted to show the buttons that have been added to it?

Can you make a small, complete program that compiles and executes and shows the problem?    It only needs to have jPanel4 and a container to hold it.  No database or anything else.  Only the bare minimum that shows the problem.



it doesn't show any button , also jPanel4 is the Database Table shown in picture , what does repaint have to do with this method ?

i have tried it in test class it doesn't work also , it doesn't show any error on compile , my container is jScrollPanel to hold the jPanel4 so i can scroll up and down .

can you try the code yourself and see if it works ?
 
Norm Radder
Master Rancher
Posts: 3257
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can you try the code


Yes, if you can make a small, complete program that compiles and executes and shows the problem.  The code should not include any database or anything not related to the problem:  adding multiple labels to a panel after the original window has been shown.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

can you try the code


Yes, if you can make a small, complete program that compiles and executes and shows the problem.  The code should not include any database or anything not related to the problem:  adding multiple labels to a panel after the original window has been shown.





i did something like this but it doesn't work at all , now i get error exception :



am i doing something wrong ?
 
Norm Radder
Master Rancher
Posts: 3257
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at test.Frame1.btn(Frame1.java:30)


At line 30 the program used an index of 1 with an array that did not have at least 2 elements.  Make sure the array is large enough and that the index value does not go past the end of the array.

Note:  The test for the end of the looping in the for statement should use < not <=.  The max index for an array is the length-1.

Also the btn array should be defined and given a length outside of the loop, not inside like is done on line 7.  Line 7 changes the contents of btn every iteration of the loop.  Anything put into the btn array following line 7 will be lost when a new btn array is created next time around on line 7.
 
omar tawfeek
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at test.Frame1.btn(Frame1.java:30)


At line 30 the program used an index of 1 with an array that did not have at least 2 elements.  Make sure the array is large enough and that the index value does not go past the end of the array.

Note:  The test for the end of the looping in the for statement should use < not <=.  The max index for an array is the length-1.

Also the btn array should be defined and given a length outside of the loop, not inside like is done on line 7.  Line 7 changes the contents of btn every iteration of the loop.  Anything put into the btn array following line 7 will be lost when a new btn array is created next time around on line 7.



The problem the same code worked for my friend but it's not working for me (i am so confused and got a headache thinking about this) . LOL

we have done the same steps but it's working for him and doesn't work for me .

i guess i'll leave it for now and make something else . ^^
 
Norm Radder
Master Rancher
Posts: 3257
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make your code work by removing line 7 that assigns a value to btn
and adding this line after line 5:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!