Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old December 20th, 2012, 06:43 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Can you explain what you are trying to do and post some code to help us understand what your problem is?

Reply With Quote
  #17  
Old December 20th, 2012, 04:37 PM
fox_news fox_news is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 fox_news User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Can you explain what you are trying to do and post some code to help us understand what your problem is?

i want to make a method that adds a JButton to a Tabbed Pane so i don't have to do this:
Code:
panel1.add(Tabs.r1);
          panel1.add(Tabs.r2);
          
          panel2.add(Tabs.r3);
          panel2.add(Tabs.r4);
          panel2.add(Tabs.r5);
          panel2.add(t3);
          panel2.add(bedrock);
          panel2.add(stone);
          panel2.add(dirt);
          panel2.add(grass);
          panel2.add(log);
          panel2.add(planks);
          panel2.add(tnt);
          panel2.add(stonebrick);
          panel2.add(netherbrick);
          
          panel3.add(t2);
          
          panel4.add(bb1);
          panel4.add(bb2);
          panel4.add(bb3);
          panel4.add(b4);
          panel4.add(b5);
          panel4.add(b6);
          panel4.add(b7);
          panel4.add(b8);
          panel4.add(b9);
          
          panel4.add(bedrock);
          panel4.add(stone);
          panel4.add(dirt);
          panel4.add(grass);
          panel4.add(log);
          panel4.add(planks);
          panel4.add(tnt);
          panel4.add(chest);
          panel4.add(stonebrick);
          panel4.add(netherbrick);
          panel4.add(wool);
          panel4.add(gravel);
          JComponent cobble = addNewButton("test","test");
          panel4.add(cobble);
          
          panel5.add(gens);
          window.add(p1);
          window.add(l1);
          window.add(type);
          p1.add(next);
          p1.add(back);
          window.add(button);
          p1.add(menu);
          window.add(b1);
          window.add(b2);
          window.add(b3);
          p1.add(l2);
          window.add(Crafting.craft1);
          window.add(Crafting.generator);
          window.add(Crafting.methods);
          window.add(Item.items1);
          window.add(Block.blocks1);
          window.add(Crafting.tp1);
          window.add(load);
          window.add(Crafting.t1);
          bg.add(Tabs.r1);
          bg.add(Tabs.r2);
          bg2.add(Tabs.r3);
          bg2.add(Tabs.r4);
          bg2.add(Tabs.r5);
          addtowin();
          firstpage();
          t3.setVisible(false);

then i want to be able to set if the buttons should be visible or not without writing out every button because that is going to be a HUGE pain
Code:
ButtonTest.bedrock.setVisible(true);
        ButtonTest.stone.setVisible(true);
        ButtonTest.dirt.setVisible(true);
        ButtonTest.grass.setVisible(true);
        ButtonTest.log.setVisible(true);
        ButtonTest.planks.setVisible(true);
        ButtonTest.type.setVisible(false);
        ButtonTest.l2.setVisible(false);
        ButtonTest.bb1.setVisible(false);
        ButtonTest.bb2.setVisible(false);
        ButtonTest.bb3.setVisible(false);
        ButtonTest.b4.setVisible(false);
        ButtonTest.b5.setVisible(false);
        ButtonTest.b6.setVisible(false);
        ButtonTest.b7.setVisible(false);
        ButtonTest.b8.setVisible(false);
        ButtonTest.b9.setVisible(false);
        ButtonTest.tnt.setVisible(true);
        ButtonTest.chest.setVisible(true);
        ButtonTest.stonebrick.setVisible(true);
        ButtonTest.netherbrick.setVisible(true);
        ButtonTest.wool.setVisible(true);
        ButtonTest.gravel.setVisible(true);
        ButtonTest.b.setVisible(true);
        isTrue = true;

after that i want to be able to set the action without typing out this(i want it to be one line):
Code:
public void actionPerformed(ActionEvent p)
        {
            if("b1".equals(p.getActionCommand()))
            {
                List.True();
                b1 = true;
            }
            if("b2".equals(p.getActionCommand()))
            {
                List.True();
                b2 = true;
            }
            if("b3".equals(p.getActionCommand()))
            {
                List.True();
                b3 = true;
            }
            if("b4".equals(p.getActionCommand()))
            {
                List.True();
                b4 = true;
            }
            if("b5".equals(p.getActionCommand()))
            {
                List.True();
                b5 = true;
            }
            if("b6".equals(p.getActionCommand()))
            {
                List.True();
                b6 = true;
            }
            if("b7".equals(p.getActionCommand()))
            {
                List.True();
                b7 = true;
            }
            if("b8".equals(p.getActionCommand()))
            {
                List.True();
                b8 = true;
            }
            if("b9".equals(p.getActionCommand()))
            {
                List.True();
                b9 = true;
            }
            if("bedrock".equals(p.getActionCommand()))
            {
                new Recipe("Block.bedrock");
                done();
                List.backto();
            }
            if("stone".equals(p.getActionCommand()))
            {
                new Recipe("Block.stone");
                done();
                List.backto();
            }
            if("dirt".equals(p.getActionCommand()))
            {
                new Recipe("Block.dirt");
                done();
                List.backto();
            }
            if("grass".equals(p.getActionCommand()))
            {
                new Recipe("Block.grass");
                done();
                List.backto();
            }
            if("log".equals(p.getActionCommand()))
            {
                new Recipe("Block.log");
                done();
                List.backto();
            }
            if("planks".equals(p.getActionCommand()))
            {
                new Recipe("Block.planks");
                done();
                List.backto();
            }
            if("tnt".equals(p.getActionCommand()))
            {
                new Recipe("Block.TNT");
                done();
                List.backto();
            }
            if("stonebrick".equals(p.getActionCommand()))
            {
                new Recipe("Block.stoneBrick");
                done();
                List.backto();
            }
            if("netherbrick".equals(p.getActionCommand()))
            {
                new Recipe("Block.netherBrick");
                done();
                List.backto();
            }
            if("chest".equals(p.getActionCommand()))
            {
                new Recipe("Block.chest");
                done();
                List.backto();
            }
            if("brick".equals(p.getActionCommand()))
            {
                new Recipe("Block.brick");
                done();
                List.backto();
            }
            if("wool".equals(p.getActionCommand()))
            {
                new Recipe("Block.cloth");
                done();
                List.backto();
            }
            if("gravel".equals(p.getActionCommand()))
            {
                new Recipe("Block.gravel");
                done();
                List.backto();
            }

Reply With Quote
  #18  
Old December 20th, 2012, 05:10 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
One way to reduce the number of lines is to put the component references into an array:
stone, dirt, grass, etc And then use a loop as shown above to get the references and call the method.
Quote:
i want it to be one line
It will take several statements: loop and if test.
Put the Strings: "b1", "b2", etc in an array and use a loop through that array to compare its elements with the String returned by the getActionCommand() method.

Because there are different statements in the if, there will have to be different loops for each set of statements.
One for when setting true and one for setting false.

Last edited by NormR : December 20th, 2012 at 05:12 PM.

Reply With Quote
  #19  
Old December 20th, 2012, 08:03 PM
fox_news fox_news is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 fox_news User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
One way to reduce the number of lines is to put the component references into an array:
stone, dirt, grass, etc And then use a loop as shown above to get the references and call the method.
It will take several statements: loop and if test.
Put the Strings: "b1", "b2", etc in an array and use a loop through that array to compare its elements with the String returned by the getActionCommand() method.

Because there are different statements in the if, there will have to be different loops for each set of statements.
One for when setting true and one for setting false.

the b1 and b2 and stuff is not what i needed. all the buttons that i need return with the same lines

Reply With Quote
  #20  
Old December 20th, 2012, 08:28 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
If you want to call the same method on all the buttons,
Try putting references to the buttons into an array and write a loop to index through the elements in the array and call the method for each element in the array.

Reply With Quote
  #21  
Old December 21st, 2012, 02:15 AM
fox_news fox_news is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 11 fox_news User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
If you want to call the same method on all the buttons,
Try putting references to the buttons into an array and write a loop to index through the elements in the array and call the method for each element in the array.

i have no idea what you just said xD

Reply With Quote
  #22  
Old December 21st, 2012, 06:33 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
I suggested doing 3 things. Which one are you having problems with?
1) define an array
2) write a for loop
3) use array's elements to call method


I gave sample code in post #10

Reply With Quote
  #23  
Old December 22nd, 2012, 02:21 AM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,534 tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 1620
Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye Send a message via Google Talk to tvc3mye Send a message via Skype to tvc3mye
Facebook
Just declare a static array of JButton type, then add all button instances you created into this array right after the initialisation block (the block which instantiates all the JButton instances in your program with the keyword new ), and loop through these JButton instances one-by-one using a loop, in which you invoke the method(s) you want (them all) to be invoked whenever it is necessary to invoke the same method(s) on all JButton instances anywhere in the codes. It is just that simple.
__________________
When the programming world turns decent, the real world will turn upside down.

Last edited by tvc3mye : December 22nd, 2012 at 02:26 AM. Reason: Changed JComponent to JButton

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Swing - Need help with JButton

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap