View Single Post
  #1 (permalink)  
Old 08-01-2007, 12:04 AM
uncopywritable uncopywritable is offline
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.
Reply With Quote
Sponsored Links