Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 12:04 AM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
help needed with methods in subclasses
I have a bunch of methods that set up a JDialog, that I got from Hardwired (thanks Hardwired ), which does everything I want and now I reckon I can set up one Dialog box to do pretty much all the stuff I want.

The problem is that I want to subclass DialogExample and call its main method from the superclass. So I thought that I should rename the main method of DialogExample 'getNewTitle' and call it from my super class, 'Menu'.

Code:
public class Menu{ public static void main(String[] args) { } getNewTitle(); } class DialogExample{ public void getNewTitle(String[] args) { DialogExample example = new DialogExample(); example.launchDialog(); } }
This doesn't seem to work.

Here is the complete code for DialogExample as by Hardwired:

Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DialogExample implements ActionListener { JDialog dialog; JTextField textField; public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("CANCEL")) System.exit(0); if(ac.equals("CLEAR")) textField.setText(""); if(ac.equals("OK")) { String text = textField.getText(); if(!text.equals("")) { dialog.dispose(); JOptionPane.showMessageDialog(null, "start your main app"); // For now we need this line. System.exit(0); } } } private JPanel getContent() { textField = new JTextField(16); String[] ids = { "Cancel", "Clear", "OK" }; JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = 3; panel.add(textField, gbc); gbc.gridy = 1; gbc.gridwidth = 1; for(int j = 0; j < ids.length; j++) { JButton button = new JButton(ids[j]); button.setActionCommand(ids[j].toUpperCase()); button.addActionListener(this); panel.add(button, gbc); } return panel; } private void launchDialog() { dialog = new JDialog(new Frame(), "dialog", false); dialog.addWindowListener(closer); dialog.getContentPane().add(getContent()); dialog.setSize(300,200); dialog.setLocation(200,200); dialog.setVisible(true); } public static void main(String[] args) { DialogExample example = new DialogExample(); example.launchDialog(); } private WindowListener closer = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; }

Last edited by levent : 08-01-2007 at 12:56 AM. Reason: Codes are placed inside [code] tag. uncopywritable, please do this yourself next time.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 02:10 AM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
First your use of subclass and superclass is incorrect. Subclasses and superclass refer to inheritance when one inherits from the other. What you have done is created two separate classes where the main of one calls the other.

What you are looking for I think is this:
DialogExample.getNewTitle();
or you can leave it called main and do this:
DialogExample.main();

When you call a static method on a class you use the actual class name and then the method.

Another option is to take the code that is inside the DialogExample main and put it in the Menu classes main method. You can just cut and paste it directly over and it will work.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-01-2007, 02:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
We don't call main methods in other classes. The main method is where the jvm (java virtual machine) starts an application.
You can instantiate another class and use it whether it has a main method or not.
Here's an example of how you can do this.
Note: for this class to compile I had to change the access modifier of the DialogExmple.launchDialog method from private to public (protected and package–private would also work).
In java:
Code:
// Change this private void launchDialog() { // to this: public void launchDialog() {
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnotherClass implements ActionListener { DialogExample dialogExample; public AnotherClass() { dialogExample = new DialogExample(); } public void actionPerformed(ActionEvent e) { if(dialogExample.dialog == null) dialogExample.launchDialog(); else dialogExample.dialog.toFront(); } private JPanel getContent() { JButton button = new JButton("show dialog"); button.addActionListener(this); JPanel panel = new JPanel(); panel.add(button); return panel; } public static void main(String[] args) { AnotherClass test = new AnotherClass(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getContent()); f.setSize(200,100); f.setLocationRelativeTo(null); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-01-2007, 02:51 AM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
Sure "we" call main methods in other classes. Especially when that is the only way you have to get at them. Classic example is running the javac compiler from within a Java program.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-01-2007, 03:47 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
much appreciated
I tried brianhks idea first because it seemed easier, but I just kept getting requests to create the 'main' method in the main class and alerts about static. It did seem the most intuitive way though. Hardwired's works fine and I think I understand a bit better now what's going on. Thanks again
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed!!! :S mark-mlt Networking 1 04-14-2008 11:27 AM
Help Needed pks New To Java 1 01-09-2008 02:07 AM
Can I use annotations in superclass, and .cfg.xml files in subclasses in EJB3 narayanrec Enterprise JavaBeans 4 01-06-2008 04:40 AM
Can I use annotations in superclass, and .cfg.xml files in subclasses narayanrec Database 0 01-02-2008 06:24 AM
Hibernate subclasses Ed Database 2 07-02-2007 06:42 PM


All times are GMT +3. The time now is 10:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org