Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default 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'.

    Java 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:

    Java 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; 07-31-2007 at 11:56 PM. Reason: Codes are placed inside [code] tag. uncopywritable, please do this yourself next time.

  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    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.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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:
    Java Code:
    // Change this
    private void launchDialog() {
    // to this:
    public void launchDialog() {
    Java 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);
        }
    }

  4. #4
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    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.

  5. #5
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default 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 :)

Similar Threads

  1. help needed!!! :S
    By mark-mlt in forum Networking
    Replies: 1
    Last Post: 04-14-2008, 10:27 AM
  2. Help Needed
    By pks in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 01:07 AM
  3. Can I use annotations in superclass, and .cfg.xml files in subclasses in EJB3
    By narayanrec in forum Enterprise JavaBeans (EJB)
    Replies: 4
    Last Post: 01-06-2008, 03:40 AM
  4. Replies: 0
    Last Post: 01-02-2008, 05:24 AM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •