The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Swing - Need help with JButton
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 18th, 2012, 10:04 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 52 m 47 sec
Reputation Power: 0
|
|
Swing - Need help with JButton
hi i am trying to make a method where it will make a new JButton(i want this method to be able to make multi JButtons because i need to make over 200 of them and they all need to do the same thing) and can set the symbol that it is called by, set an action command and set the action listener class but i can get it to work.
Code:
public addNewButton(String text, String name)
{
JButton j = new JButton(text);
j.setName(name);
j.addActionListener(new R4());
j.setActionCommand(text);
j.setVisible(false);
new Recipe(text);
}
the new Recipe(text) works fine so that doesn't need to be fixed.
then i want to add it to a list to be called to setVisible(true);
Code:
i don't know how to do this
here is an example of what the method looks like when i call it
Code:
new addNewButton("CobbleStone", "Block.cobblestone");
then i want to add it to a TabbedPane
Code:
panel4.add(CobbleStone);
|

December 18th, 2012, 12:54 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
A couple of things.
Your method doesn't list a return type.
Also you don't have a return statement.
|

December 18th, 2012, 02:01 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | what the method looks like when i call it |
That call looks like a constructor, not a method. new is used when you create a new instance of a class.
The new statement returns a reference to the newly created object. Your code should save that reference in a variable.
|

December 18th, 2012, 05:49 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 That call looks like a constructor, not a method. new is used when you create a new instance of a class.
The new statement returns a reference to the newly created object. Your code should save that reference in a variable. |
oh ok so like:
Code:
protected static JComponent addNewButton(String text, String name)
{
JButton j = new JButton("test");
j.setName(name);
j.addActionListener(new R4());
j.setActionCommand(text);
return new JButton(text);
}
also how can i add buttons to a group so i can just type one line to set them visible or not visible?
|

December 18th, 2012, 05:53 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 52 m 47 sec
Reputation Power: 0
|
|
I GOT THE METHOD TO WORK!
i got it to add a new button but now i still need help with putting them in a group. so i can add them all at one time and set them visible with one line like:
Code:
panel4.add(<buttongroup>);
or
Code:
<buttongroup>.setVisible(true);
Code:
<buttongruop>.setVisible(false);
|

December 18th, 2012, 06:25 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
I've never seen a method that does that. I always use a loop.
|

December 18th, 2012, 08:18 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 I always use a loop. |
umm explain please
|

December 18th, 2012, 08:45 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
define an array that holds the components
begin loop through the array
get next component from array
do whatever with the component
end loop
|

December 18th, 2012, 08:58 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 define an array that holds the components
begin loop through the array
get next component from array
do whatever with the component
end loop |
can i have an example code please?
|

December 19th, 2012, 06:29 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Code:
define an array that holds the components: Component[] compAry = {comp1, comp2};
begin loop through the array: for(int i=0; i<compAry.length; i++) {
get next component from array : Component aComp = compAry[i];
do whatever with the component: aComp.someCompMethod();
end loop: }
|

December 19th, 2012, 06:06 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
Code:
define an array that holds the components: Component[] compAry = {comp1, comp2};
begin loop through the array: for(int i=0; i<compAry.length; i++) {
get next component from array : Component aComp = compAry[i];
do whatever with the component: aComp.someCompMethod();
end loop: }
|
how can that add them to a TabbedPane?
|

December 19th, 2012, 06:26 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
What is the code to add a component to a TabbedPane?
Do you have a program that you are working on?
Last edited by NormR : December 19th, 2012 at 06:28 PM.
|

December 19th, 2012, 10:11 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 What is the code to add a component to a TabbedPane?
Do you have a program that you are working on? |
yes but if i posted my code i would feel bad because its a mess. if it is needed i will post it though.
|

December 19th, 2012, 10:48 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 12 m 41 sec
Reputation Power: 0
|
|
|
JButton
public JButton()
Creates a button with no set text or icon.
JButton
public JButton(Icon icon)
Creates a button with an icon.
Parameters:
icon - the Icon image to display on the button
|

December 19th, 2012, 11:53 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 52 m 47 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by jhon marvi JButton
public JButton()
Creates a button with no set text or icon.
JButton
public JButton(Icon icon)
Creates a button with an icon.
Parameters:
icon - the Icon image to display on the button |
i never said i didn't know how to make a JButton. please read more next time.
|
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
|
|
|
|
|