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



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 3rd, 2013, 07:17 PM
notelek notelek is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 notelek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 57 sec
Reputation Power: 0
Setting a button to run a constructor?

Hi!
I am new here, and have a simple question
say I had a simple jframe and button:

Code:
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
frame.setLocationRelativeTo(null);

JButton button = new JButton();
button.setSize(new Dimension(100 ,100));
button.setLocation(5,5);
button.setVisible(true);
frame.getContentPane().add(button);
}


How would I make button run a certain constructor?
also, can it run one that is in a different class?

Reply With Quote
  #2  
Old January 10th, 2013, 01:39 PM
jayhamm jayhamm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 jayhamm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 52 sec
Reputation Power: 0
Code:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
frame.setLocationRelativeTo(null);

JButton button = new JButton();
button.setSize(new Dimension(100 ,100));
button.setLocation(5,5);
button.setVisible(true);
        
//add an ActionListener to your button
button.addActionListener(new ActionListener() {

@Override //required to override abstract actionPerformed() method from ActionListener interface
public void actionPerformed(ActionEvent e) {
System.out.println("Button pressed!");
//Add your constructor here
//Constructor constructor = new Constructor();
}
});

frame.getContentPane().add(button);
}



This should work for the specific case you asked for. It adds a unique action listener to button and you can replicate this multiple times for multiple buttons. If you want to have a single actionPerformed() method for multiple buttons you need to move this out of the main method. I'm not a Java guru but it has something to do with the main method being static.

You also should make sure that your class implements the ActionListener and that you import the right packages:

Code:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test implements ActionListener

Last edited by jayhamm : January 10th, 2013 at 04:16 PM. Reason: add import/implement statements

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Setting a button to run a constructor?


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 - 2014, Jelsoft Enterprises Ltd.

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