some problems when My Base Class is Extended with JDialog.

hello everybody,

I have a problem here. similar problem has been faced by different developers, but mine is little different form them.

pls have a look at the code..

package thepack;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

class MyWindowextends JFrameimplements ActionListener{

JButton btnok, btncancel ;

Container con;

JPanel pansouth;

JMenuBar menubar;

JMenu menu;

JMenuItem menuitem;

MyWindow(){

// TODO Auto-generated constructor stub

con = this.getContentPane();

con.setLayout(new BorderLayout());

menubar =new JMenuBar();

//Build the first menu.

menu =new JMenu("A Menu");

menu.setMnemonic(KeyEvent.VK_A);

menubar.add(menu);

menuitem =new JMenuItem("Add New Employee ",

KeyEvent.VK_E);

menu.add(menuitem);

this.setJMenuBar(menubar);

btnok =new JButton("OK");

btncancel =new JButton("cancel");

pansouth =new JPanel(new FlowLayout());

con.add(pansouth, BorderLayout.SOUTH);

pansouth.add(btnok);

pansouth.add(btncancel);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

btncancel.addActionListener(this);

menuitem.addActionListener(this);

}

publicvoid actionPerformed(ActionEvent arg0){

// TODO Auto-generated method stub

if(arg0.getSource()== btncancel)

{

System.exit(0);

}

elseif (arg0.getSource()== menuitem)

{

NewEmpWin emp =new NewEmpWin(this);

}

}

}

package thepack;

publicclass SampleWinextends JDialog{

Container con;

JButton btnok, btncancel;

JPanel pansouth;

public SampleWin(MyWindow window){

// TODO Auto-generated constructor stub

super(window,true);

System.out.println("we are in main of samp win");

con = this.getContentPane();

con.setLayout(new BorderLayout());

btnok =new JButton("OK");

btncancel =new JButton("cancel");

pansouth =new JPanel(new FlowLayout());

con.add(pansouth, BorderLayout.SOUTH);

pansouth.add(btnok);

pansouth.add(btncancel);

System.out.println("end of we are in main of samp win befor ini");

this.setSize(400,500);

this.setLocation(300, 300);

this.setResizable(false);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setVisible(true);

System.out.println("end of we are in main of samp win");

}

}

package thepack;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JLabel;

publicclass NewEmpWinextends SampleWinimplements ActionListener{

JLabel lfname ;

public NewEmpWin(MyWindow window){

// TODO Auto-generated constructor stub

super(window);

System.out.println("we are in main of new emp win");

lfname =new JLabel("first name");

con.add(lfname, BorderLayout.CENTER);

btncancel.addActionListener(this);

btnok.addActionListener(this);

System.out.println("end of we are in main of new emp win");

}

publicvoid actionPerformed(ActionEvent e){

// TODO Auto-generated method stub

if(e.getSource()== btnok)

{

lfname.setText("yes");

// some code

}

elseif (e.getSource()== btncancel)

{

lfname.setText("cancel");

this.dispose();

}

}

}

here i have created a sample modal dialog in sampleWin. and it is extended by NewEmpWin.

in NewEmpwin i have added some components.

but i fear that constructor of NewEmpWin is not getting called. so i cannot see the components added in it , but i can see only ok and cancel.

can some one help me.

[7596 byte] By [Avinash.Ka] at [2007-11-27 6:48:34]
# 1
hi!how you have written the main? how your entry point looks like?regards Aniruddha
Aniruddha-Herea at 2007-7-12 18:21:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

hey my entry point is

package thepack;

public class TheMain {

public TheMain()

{

System.out.println("we are in const of the main.");

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

TheMain m = new TheMain();

m.Createwindow();

}

void callme()

{

System.out.println("we are in call me of main");

}

void Createwindow()

{

MyWindow win = new MyWindow();

win.setSize(650,550);

win.setVisible(true);

}

}

Avinash.Ka at 2007-7-12 18:21:59 > top of Java-index,Desktop,Core GUI APIs...
# 3

hi!

your main problem is in SampleWin class constructor. there you are calling setVisible true of a modal dialog, so the current thread stops there and rest of the part is awaited. i have done a small change, check that out...

package com.ani;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.util.Date;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

public class TheMain

{

public TheMain()

{

System.out.println("we are in const of the main.");

}

/**

* @param args

*/

public static void main(String[] args)

{

TheMain m = new TheMain();

m.Createwindow();

}

void callme()

{

System.out.println("we are in call me of main");

}

void Createwindow()

{

MyWindow win = new MyWindow();

win.setSize(650, 550);

win.setVisible(true);

}

}

class MyWindow extends JFrame implements ActionListener

{

JButtonbtnok, btncancel;

Containercon;

JPanelpansouth;

JMenuBarmenubar;

JMenumenu;

JMenuItemmenuitem;

MyWindow()

{

con = this.getContentPane();

con.setLayout(new BorderLayout());

menubar = new JMenuBar();

//Build the first menu.

menu = new JMenu("A Menu");

menu.setMnemonic(KeyEvent.VK_A);

menubar.add(menu);

menuitem = new JMenuItem("Add New Employee ", KeyEvent.VK_E);

menu.add(menuitem);

this.setJMenuBar(menubar);

btnok = new JButton("OK");

btncancel = new JButton("cancel");

pansouth = new JPanel(new FlowLayout());

con.add(pansouth, BorderLayout.SOUTH);

pansouth.add(btnok);

pansouth.add(btncancel);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

btncancel.addActionListener(this);

menuitem.addActionListener(this);

}

public void actionPerformed(ActionEvent arg0)

{

if (arg0.getSource() == btncancel)

{

System.exit(0);

}

else if (arg0.getSource() == menuitem)

{

new NewEmpWin(this);

}

}

}

class SampleWin extends JDialog

{

Containercon;

JButtonbtnok, btncancel;

JPanelpansouth;

public SampleWin(MyWindow window)

{

super(window, true);

System.out.println("we are in main of samp win");

con = this.getContentPane();

con.setLayout(new BorderLayout());

btnok = new JButton("OK");

btncancel = new JButton("cancel");

pansouth = new JPanel(new FlowLayout());

con.add(pansouth, BorderLayout.SOUTH);

pansouth.add(btnok);

pansouth.add(btncancel);

System.out.println("end of we are in main of samp win befor ini");

this.setSize(400, 500);

this.setLocation(300, 300);

this.setResizable(false);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

//this.setVisible(true);

System.out.println("end of we are in main of samp win");

}

}

class NewEmpWin extends SampleWin implements ActionListener

{

JLabellfname;

public NewEmpWin(MyWindow window)

{

super(window);

System.out.println("we are in main of new emp win");

lfname = new JLabel("first name");

con.add(lfname, BorderLayout.CENTER);

btncancel.addActionListener(this);

btnok.addActionListener(this);

this.setVisible(true);

System.out.println("end of we are in main of new emp win");

}

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == btnok)

{

lfname.setText("yes : " + new Date(System.currentTimeMillis()));

}

else if (e.getSource() == btncancel)

{

lfname.setText("cancel");

this.dispose();

}

}

}

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 18:21:59 > top of Java-index,Desktop,Core GUI APIs...
# 4
hey... thanks , I got it.
Avinash.Ka at 2007-7-12 18:21:59 > top of Java-index,Desktop,Core GUI APIs...