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:
  #1  
Old December 18th, 2012, 10:04 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
Unhappy 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);

Reply With Quote
  #2  
Old December 18th, 2012, 12:54 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,826 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 9 h 16 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
A couple of things.

Your method doesn't list a return type.

Also you don't have a return statement.

Reply With Quote
  #3  
Old December 18th, 2012, 02:01 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,956 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 56 m 11 sec
Reputation Power: 345
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.

Reply With Quote
  #4  
Old December 18th, 2012, 05:49 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
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?

Reply With Quote
  #5  
Old December 18th, 2012, 05:53 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
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);

Reply With Quote
  #6  
Old December 18th, 2012, 06:25 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,956 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 56 m 11 sec
Reputation Power: 345
Quote:
add them all at one time

I've never seen a method that does that. I always use a loop.

Reply With Quote
  #7  
Old December 18th, 2012, 08:18 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
I always use a loop.

umm explain please

Reply With Quote
  #8  
Old December 18th, 2012, 08:45 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,956 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 56 m 11 sec
Reputation Power: 345
define an array that holds the components
begin loop through the array
get next component from array
do whatever with the component
end loop

Reply With Quote
  #9  
Old December 18th, 2012, 08:58 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
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?

Reply With Quote
  #10  
Old December 19th, 2012, 06:29 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,956 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 56 m 11 sec
Reputation Power: 345
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:                        }

Reply With Quote
  #11  
Old December 19th, 2012, 06:06 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
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?

Reply With Quote
  #12  
Old December 19th, 2012, 06:26 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,956 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 56 m 11 sec
Reputation Power: 345
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.

Reply With Quote
  #13  
Old December 19th, 2012, 10:11 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
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.

Reply With Quote
  #14  
Old December 19th, 2012, 10:48 PM
jhon marvi jhon marvi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 jhon marvi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #15  
Old December 19th, 2012, 11:53 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 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.

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