The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Page 2 -
Swing - Need help with JButton
Page 2 - Discuss Need help with JButton in the Java Help forum on Dev Shed. Need help with JButton Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 20th, 2012, 06:43 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Can you explain what you are trying to do and post some code to help us understand what your problem is?
|

December 20th, 2012, 04:37 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
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();
}
|

December 20th, 2012, 05:10 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
Last edited by NormR : December 20th, 2012 at 05:12 PM.
|

December 20th, 2012, 08:03 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
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
|

December 20th, 2012, 08:28 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

December 21st, 2012, 02:15 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
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 
|

December 21st, 2012, 06:33 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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
|

December 22nd, 2012, 02:21 AM
|
 |
Daniel Schildsky
|
|
Join Date: Mar 2004
Location: KL, Malaysia.
|
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|