Allowing a window to be seen in JAVA

LMDC
LMDC used Ask the Experts™
on
I have a program that displays a window once 'add part' is selected but the window won't show up.  I tried using setVisible(true) but I get several errors.

Here's part of my code:


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuLayoutDemo extends JFrame implements ActionListener{

AddWin addwindow=new AddWin();

..

public void actionPerformed(ActionEvent e){              
          if(e.getActionCommand().equals("Add Part")){
               addwindow.init();
          }
     }


/* Here's the window that should pop up */

class AddWin extends JFrame{
               TextField componentName;
               

public void init(){
               setSize(400,200);                    setTitle("Add Part");
}
               
               
               setVisible(true);
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
Try:

class AddWin extends JFrame
{
   TextField componentName;
             
    public void init()
    {
        setSize(400,200);
        setTitle("Add Part");
        setVisible(true);
    }
}

Author

Commented:
That compiles fine but again when I run the program it won't display the AddWin window when I click on add part.
Mick BarryJava Developer
Top Expert 2010

Commented:
What type of componen t is "add Part", and do you add your action listener to it?
Maybe post the code that sets up your "Add Part" component.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
public class MenuLayoutDemo extends JFrame implements ActionListener{

...

JPanel jPanel2 = new JPanel();

...
 
Button addButton=new Button("Add Part");


  public void init()
  {
    addButton.addActionListener(this);

  }

  public MenuLayoutDemo()
  {
        ...    
            jPanel2.add(addButton);

  }

}
Mick BarryJava Developer
Top Expert 2010

Commented:
1 thing, you should avoid mixing AWT and Swing components.
Use JButton instead of Button.
Mick BarryJava Developer
Top Expert 2010

Commented:
Looks ok, try using show() instead of setVisible(true) though they should both do the same thing.

Author

Commented:
Okay -- I changed Button to JButton and substituted show(); for setVisible(true); but the window still doesn't show.

Since I'm using a jButton does anything need to be changed with the ActionPerformed method? It currently reads like so

if(e.getActionCommand().equals("Add Part")){
              addwindow.init();




Here's the updated code:


import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuLayoutDemo extends JFrame implements ActionListener{

  AddWin addwindow=new AddWin();

  ...

  public class MenuLayoutDemo extends JFrame implements ActionListener{

  ...

  JPanel jPanel1 = new JPanel();

  ...

  JButton addButton=new JButton("Add Part");


 public void init()
 {
   addButton.addActionListener(this);

 }

 public MenuLayoutDemo()
 {
       ...    
   addButton.setText("Add Part");
   addButton.setBounds(new Rectangle(132, 225, 89, 28));
   jPanel1.add(addButton, null);
 }

}

  public void actionPerformed(ActionEvent e)
  {              
      if(e.getActionCommand().equals("Add Part")){
      addwindow.init();
     }
   }




   class AddWin extends JFrame
   {
          TextField componentName;
             
          public void init()
          {
             setSize(400,200);
             setTitle("Add Part");
             show();
           }
    }
}
Hi,

   I have modified your code, it works fine, you can try that :

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class MenuLayoutDemo extends JFrame implements ActionListener
{


public void init()
{}

public MenuLayoutDemo()
{

  JPanel jPanel1 = new JPanel();
  JButton addButton=new JButton("Add Part");
  addButton.setText("Add Part");
  addButton.setBounds(new Rectangle(132, 225, 89, 28));
  addButton.addActionListener(this);

  jPanel1.add(addButton, null);

  getContentPane().setLayout ( new BorderLayout() );
  getContentPane().add(jPanel1, BorderLayout.CENTER);
  setBounds(10,10,100,100);
  show();

}

 public void actionPerformed(ActionEvent e)
 {
     if(e.getActionCommand().equals("Add Part"))
     {
      AddWin addwindow=new AddWin();
     addwindow.init();
    }
  }


  public static void main ( String[] argvs )
  {
       MenuLayoutDemo mld = new MenuLayoutDemo();
  }

}


//AddWin.java

import javax.swing.*;

public class AddWin extends JFrame
{
         JTextField componentName;

         public void init()
         {
            setSize(400,200);
            setTitle("Add Part");
            show();
          }
}


hth,

Mick BarryJava Developer
Top Expert 2010

Commented:
you'll need to post your complete code to help find the problem.

Author

Commented:
kokchoon78 -- I tried the modified code you posted and it compiled fine but when I ran it nothing happened.

objects -- here's my complete code.  The display isn't very 'aesthetic' but I wanted to solve this problem first.  :-)




import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class MenuLayoutDemo extends JFrame implements ActionListener{

AddWin addwindow=new AddWin();
 

   
BorderLayout borderLayout1 = new BorderLayout();
      JPanel jPanel1 = new JPanel();
      JPanel jPanel2 = new JPanel();
      JLabel jLabel1 = new JLabel();
      JLabel jLabel2 = new JLabel();
      JLabel jLabel3 = new JLabel();
      JTextField jTextField1 = new JTextField();
      JTextField jTextField2 = new JTextField();
      JTextField jTextField3 = new JTextField();
      JButton jButton1 = new JButton();
      JButton addButton = new JButton("Add Part");

protected JMenuBar menuBar;

public void init()
{
addButton.addActionListener(this);
setSize(400,200);
}


    public MenuLayoutDemo() {
        super("MenuLayoutDemo");
        menuBar = new JMenuBar();
        menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
        addNewMenu1("New Case");
        addNewMenu2("Add Component");
        addNewMenu3("Delete");
        getContentPane().add(menuBar, BorderLayout.WEST);

   

   jPanel2.setBounds(new Rectangle(191, 5, 10, 10));
   jLabel1.setText("jLabel1");
   jLabel1.setBounds(new Rectangle(82, 40, 85, 22));
   jLabel2.setText("jLabel2");
   jLabel2.setBounds(new Rectangle(82, 76, 85, 22));
   jLabel3.setText("jLabel3");
   jLabel3.setBounds(new Rectangle(82, 112, 85, 22));
   jTextField1.setText("jTextField1");
   jTextField1.setBounds(new Rectangle(132, 40, 85, 22));
   jTextField2.setText("jTextField2");
   jTextField2.setBounds(new Rectangle(132, 76, 85, 22));
   jTextField3.setText("jTextField3");
   jTextField3.setBounds(new Rectangle(132, 112, 85, 22));
   jButton1.setText("jButton1");
   jButton1.setBounds(new Rectangle(132, 175, 89, 28));
   jPanel1.setLayout(null);
   
   addButton.setText("Add Part");
   addButton.setBounds(new Rectangle(132, 225, 89, 28));
   jPanel1.add(addButton, null);


   this.setTitle("A Sample");
   this.getContentPane().add(jPanel1, BorderLayout.CENTER);
   jPanel1.add(jPanel2, null);
   jPanel1.add(jLabel1, null);
   jPanel1.add(jLabel2, null);
   jPanel1.add(jLabel3, null);
   jPanel1.add(jTextField1, null);
   jPanel1.add(jTextField2, null);
      jPanel1.add(jTextField3, null);
   jPanel1.add(jButton1, null);        

addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

       public void addNewMenu1(String title) {
        JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       
    }

       public void addNewMenu2(String title) {
        JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
        m.add("Power Supply");
        m.add("CPU");
      m.add("Hard Drive");
      m.add("Floppy Drive");
        JMenu m1 = (JMenu)m.add(new HorizontalMenu("Motherboard"));
        m1.add("Memory");
        m1.add("CPU Fan");
    }

       public void addNewMenu3(String title) {
        JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       
    }

    public static void main(String args[]) {
        MenuLayoutDemo f = new MenuLayoutDemo();
        f.pack();
        f.setVisible(true);
    }



public void actionPerformed(ActionEvent e){      //which button is pressed
            
            if(e.getActionCommand().equals("Add Part")){
                  addwindow.init();
            }
      }




class AddWin extends JFrame
{
  TextField componentName;
             
   public void init()
   {
       setSize(400,200);
       setTitle("Add Part");
       show();
   }
}



    class HorizontalMenu extends JMenu {
        HorizontalMenu(String label) {
            super(label);
            JPopupMenu pm = getPopupMenu();
            pm.setLayout(new BoxLayout(pm, BoxLayout.X_AXIS));
            setMinimumSize(getPreferredSize());
        }




       
       public void setPopupMenuVisible(boolean b) {
            boolean isVisible = isPopupMenuVisible();
            if (b != isVisible) {
                if ((b==true) && isShowing()) {
                    // Set location of popupMenu (pulldown or pullright)
                    // Perhaps this should be dictated by L&F
                    int x = 0;
                    int y = 0;
                    Container parent = getParent();
                    if (parent instanceof JPopupMenu) {
                        x = 0;
                        y = getHeight();
                    } else {
                        x = getWidth();
                        y = 0;
                    }
                    getPopupMenu().show(this, x, y);
                } else {
                    getPopupMenu().setVisible(false);
                }
            }
        }
    }
}
this is the modified version of your code
i only added one line
hehee

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class test extends JFrame implements ActionListener{

AddWin addwindow=new AddWin();
 

   
BorderLayout borderLayout1 = new BorderLayout();
     JPanel jPanel1 = new JPanel();
     JPanel jPanel2 = new JPanel();
     JLabel jLabel1 = new JLabel();
     JLabel jLabel2 = new JLabel();
     JLabel jLabel3 = new JLabel();
     JTextField jTextField1 = new JTextField();
     JTextField jTextField2 = new JTextField();
     JTextField jTextField3 = new JTextField();
     JButton jButton1 = new JButton();
     JButton addButton = new JButton("Add Part");

protected JMenuBar menuBar;

public void init()
{
addButton.addActionListener(this);
setSize(400,200);
}


   public test() {
       super("MenuLayoutDemo");
       menuBar = new JMenuBar();
       menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
       addNewMenu1("New Case");
       addNewMenu2("Add Component");
       addNewMenu3("Delete");
       getContentPane().add(menuBar, BorderLayout.WEST);

 

  jPanel2.setBounds(new Rectangle(191, 5, 10, 10));
  jLabel1.setText("jLabel1");
  jLabel1.setBounds(new Rectangle(82, 40, 85, 22));
  jLabel2.setText("jLabel2");
  jLabel2.setBounds(new Rectangle(82, 76, 85, 22));
  jLabel3.setText("jLabel3");
  jLabel3.setBounds(new Rectangle(82, 112, 85, 22));
  jTextField1.setText("jTextField1");
  jTextField1.setBounds(new Rectangle(132, 40, 85, 22));
  jTextField2.setText("jTextField2");
  jTextField2.setBounds(new Rectangle(132, 76, 85, 22));
  jTextField3.setText("jTextField3");
  jTextField3.setBounds(new Rectangle(132, 112, 85, 22));
  jButton1.setText("jButton1");
  jButton1.setBounds(new Rectangle(132, 175, 89, 28));
  jPanel1.setLayout(null);
 
  addButton.setText("Add Part");
  addButton.setBounds(new Rectangle(132, 225, 89, 28));
  addButton.addActionListener(this);
  jPanel1.add(addButton, null);


  this.setTitle("A Sample");
  this.getContentPane().add(jPanel1, BorderLayout.CENTER);
  jPanel1.add(jPanel2, null);
  jPanel1.add(jLabel1, null);
  jPanel1.add(jLabel2, null);
  jPanel1.add(jLabel3, null);
  jPanel1.add(jTextField1, null);
  jPanel1.add(jTextField2, null);
    jPanel1.add(jTextField3, null);
  jPanel1.add(jButton1, null);        

addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });
   }

      public void addNewMenu1(String title) {
       JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       
   }

      public void addNewMenu2(String title) {
       JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       m.add("Power Supply");
       m.add("CPU");
     m.add("Hard Drive");
     m.add("Floppy Drive");
       JMenu m1 = (JMenu)m.add(new HorizontalMenu("Motherboard"));
       m1.add("Memory");
       m1.add("CPU Fan");
   }

      public void addNewMenu3(String title) {
       JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       
   }

   public static void main(String args[]) {
       test f = new test();
       f.pack();
       f.setVisible(true);
   }



public void actionPerformed(ActionEvent e){     //which button is pressed
         
          if(e.getActionCommand().equals("Add Part")){
               addwindow.init();
          }
     }




class AddWin extends JFrame
{
 TextField componentName;
           
  public void init()
  {
      setSize(400,200);
      setTitle("Add Part");
      show();
  }
}



   class HorizontalMenu extends JMenu {
       HorizontalMenu(String label) {
           super(label);
           JPopupMenu pm = getPopupMenu();
           pm.setLayout(new BoxLayout(pm, BoxLayout.X_AXIS));
           setMinimumSize(getPreferredSize());
       }




       
      public void setPopupMenuVisible(boolean b) {
           boolean isVisible = isPopupMenuVisible();
           if (b != isVisible) {
               if ((b==true) && isShowing()) {
                   // Set location of popupMenu (pulldown or pullright)
                   // Perhaps this should be dictated by L&F
                   int x = 0;
                   int y = 0;
                   Container parent = getParent();
                   if (parent instanceof JPopupMenu) {
                       x = 0;
                       y = getHeight();
                   } else {
                       x = getWidth();
                       y = 0;
                   }
                   getPopupMenu().show(this, x, y);
               } else {
                   getPopupMenu().setVisible(false);
               }
           }
       }
   }
}
er .. change back the class name to MenuLayoutDemo
hehehee

Author

Commented:
AH!  Thanks, skyshadow!!  It worked.  But before I give you the points -- what did you change?
Hi,

   i see..... is it we must add the listener b4 we add the button to a container, skyshadow ?

regards,
Kok Choon.
Mick BarryJava Developer
Top Expert 2010

Commented:
You never add the listener, as you never call init.
Th following may be more what you are after as it calls init. But please DO NOT award me the points. skyshadow found the problem.

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class MenuLayoutDemo extends JFrame implements ActionListener{

AddWin addwindow=new AddWin();
 

   
BorderLayout borderLayout1 = new BorderLayout();
     JPanel jPanel1 = new JPanel();
     JPanel jPanel2 = new JPanel();
     JLabel jLabel1 = new JLabel();
     JLabel jLabel2 = new JLabel();
     JLabel jLabel3 = new JLabel();
     JTextField jTextField1 = new JTextField();
     JTextField jTextField2 = new JTextField();
     JTextField jTextField3 = new JTextField();
     JButton jButton1 = new JButton();
     JButton addButton = new JButton("Add Part");

protected JMenuBar menuBar;

public void init()
{
addButton.addActionListener(this);
setSize(400,200);
}


   public MenuLayoutDemo() {
       super("MenuLayoutDemo");
       menuBar = new JMenuBar();
       menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
       addNewMenu1("New Case");
       addNewMenu2("Add Component");
       addNewMenu3("Delete");
       getContentPane().add(menuBar, BorderLayout.WEST);

 

  jPanel2.setBounds(new Rectangle(191, 5, 10, 10));
  jLabel1.setText("jLabel1");
  jLabel1.setBounds(new Rectangle(82, 40, 85, 22));
  jLabel2.setText("jLabel2");
  jLabel2.setBounds(new Rectangle(82, 76, 85, 22));
  jLabel3.setText("jLabel3");
  jLabel3.setBounds(new Rectangle(82, 112, 85, 22));
  jTextField1.setText("jTextField1");
  jTextField1.setBounds(new Rectangle(132, 40, 85, 22));
  jTextField2.setText("jTextField2");
  jTextField2.setBounds(new Rectangle(132, 76, 85, 22));
  jTextField3.setText("jTextField3");
  jTextField3.setBounds(new Rectangle(132, 112, 85, 22));
  jButton1.setText("jButton1");
  jButton1.setBounds(new Rectangle(132, 175, 89, 28));
  jPanel1.setLayout(null);
 
  addButton.setText("Add Part");
  addButton.setBounds(new Rectangle(132, 225, 89, 28));
  jPanel1.add(addButton, null);


  this.setTitle("A Sample");
  this.getContentPane().add(jPanel1, BorderLayout.CENTER);
  jPanel1.add(jPanel2, null);
  jPanel1.add(jLabel1, null);
  jPanel1.add(jLabel2, null);
  jPanel1.add(jLabel3, null);
  jPanel1.add(jTextField1, null);
  jPanel1.add(jTextField2, null);
     jPanel1.add(jTextField3, null);
  jPanel1.add(jButton1, null);        

  init();

addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });
   }

      public void addNewMenu1(String title) {
       JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       
   }

      public void addNewMenu2(String title) {
       JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       m.add("Power Supply");
       m.add("CPU");
     m.add("Hard Drive");
     m.add("Floppy Drive");
       JMenu m1 = (JMenu)m.add(new HorizontalMenu("Motherboard"));
       m1.add("Memory");
       m1.add("CPU Fan");
   }

      public void addNewMenu3(String title) {
       JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
       
   }

   public static void main(String args[]) {
       MenuLayoutDemo f = new MenuLayoutDemo();
       f.pack();
       f.setVisible(true);
   }



public void actionPerformed(ActionEvent e){     //which button is pressed
         
          if(e.getActionCommand().equals("Add Part")){
               addwindow.init();
          }
     }




class AddWin extends JFrame
{
 TextField componentName;
           
  public void init()
  {
      setSize(400,200);
      setTitle("Add Part");
      show();
  }
}



   class HorizontalMenu extends JMenu {
       HorizontalMenu(String label) {
           super(label);
           JPopupMenu pm = getPopupMenu();
           pm.setLayout(new BoxLayout(pm, BoxLayout.X_AXIS));
           setMinimumSize(getPreferredSize());
       }




       
      public void setPopupMenuVisible(boolean b) {
           boolean isVisible = isPopupMenuVisible();
           if (b != isVisible) {
               if ((b==true) && isShowing()) {
                   // Set location of popupMenu (pulldown or pullright)
                   // Perhaps this should be dictated by L&F
                   int x = 0;
                   int y = 0;
                   Container parent = getParent();
                   if (parent instanceof JPopupMenu) {
                       x = 0;
                       y = getHeight();
                   } else {
                       x = getWidth();
                       y = 0;
                   }
                   getPopupMenu().show(this, x, y);
               } else {
                   getPopupMenu().setVisible(false);
               }
           }
       }
   }
}
yup .. u forgot to add the listener ..
next time u gotta be careful not to make such a mistake .. such mistakes can be avoided
:)

-=[sKySh@DoW]=-
kokchoon78,

add the button first
then the listener
then add the button to the container
:)

-=[sKySh@DoW]=-

Author

Commented:
Thanks skyshadow!
you're welcome

:)
-=[sKySh@DoW]=-
and thanks for the points

:)
-=[sKySh@DoW]=-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial