Hello,

I have a simple GUI but when i put another Jframe in an action listener it squishes the main page in with the new Jframe, i know this is probably basic but my text book yeilds no result, here is my code. Maybe i am going about the process in the wrong way. But i want my programm to be able to access one frame from a button push in another frame, if you run the code you will hopefully see what i mean

Is there something in particular i should read up on?

Any help would be sweet, thanks (class is Interface)

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

public class Interface extends JFrame
{
public static final int WIDTH = 600;
public static final int HEIGHT = 500;
int i = 0;
private JButton newSongB, exitB, deleteSongB, playlistB, searchB;
private NewSongButtonHandler nsHandler;
private DeleteSongButtonHandler dsHandler;
private PlaylistButtonHandler plHandler;
private SearchButtonHandler sbHandler;
private ExitButtonHandler ebHandler;

public JLabel nameL, artistL, fileSizeL, durationL;
public JTextField nameTF, artistTF, fileSizeTF, durationTF;
public JButton menuB,enterSongB;
public MainMenuHandler mmHandler;
public EnterSongDataHandler sdHandler;


//private void run()
//{
//This method should control the flow of the program
//and have all code for accessing the playlists
//and song database


public Interface ()
{
setTitle("Mp3 database: Please select Function");

newSongB = new JButton("Enter New Song");
nsHandler = new NewSongButtonHandler();
newSongB.addActionListener(nsHandler);

deleteSongB = new JButton("Delete Song");
dsHandler = new DeleteSongButtonHandler();
deleteSongB.addActionListener(dsHandler);

playlistB = new JButton("Playlist");
plHandler = new PlaylistButtonHandler();
playlistB.addActionListener(plHandler);

searchB = new JButton("Search Database");
sbHandler = new SearchButtonHandler();
searchB.addActionListener(sbHandler);

exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);

Container pane = getContentPane();
pane.setLayout(new GridLayout(5,0));

pane.add(newSongB);
pane.add(deleteSongB);
pane.add(playlistB);
pane.add(searchB);
pane.add(exitB);

setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

private class NewSongButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//JFrame newFrame = new JFrame("This frame popped up");
//newFrame.setSize(100,100);
//newFrame.setVisible(true);

setTitle("Enter Song Info");

nameTF = new JTextField(10);
artistTF = new JTextField(10);
fileSizeTF = new JTextField(10);
durationTF = new JTextField(10);

nameL = new JLabel("Enter Song Name: ",SwingConstants.CENTER);
artistL = new JLabel("Enter Artist Name: ",SwingConstants.CENTER);
fileSizeL = new JLabel("Enter File Size (in Kbytes)",SwingConstants.CENTER);
durationL = new JLabel("Enter song Duration (in Seconds)",SwingConstants.CENTER);

enterSongB = new JButton("Add Song");
sdHandler = new EnterSongDataHandler();
enterSongB.addActionListener(sdHandler);

menuB = new JButton("Main Menu");
mmHandler = new MainMenuHandler();
menuB.addActionListener(mmHandler);

Container pane = getContentPane();
pane.setLayout(new GridLayout(5,2));

pane.add(nameL);
pane.add(nameTF);
pane.add(artistL);
pane.add(artistTF);
pane.add(fileSizeL);
pane.add(fileSizeTF);
pane.add(durationL);
pane.add(durationTF);
pane.add(enterSongB);
pane.add(menuB);


setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}

private class DeleteSongButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}

}

private class PlaylistButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}

}

private class SearchButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}

}

private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}

}

private class EnterSongDataHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}
}

private class MainMenuHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

}
}


//}
public static void main(String[] args){
Interface intFace = new Interface();
//intFace.run();
}
}

That is beacuse you adding new content to existing content of your JFrame

public Interface ()
{
      //code befor this point
	Container pane = getContentPane();
	pane.setLayout(new GridLayout(5,0));
}

and then on button press

private class NewSongButtonHandler implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
                //code befor this point       
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(5,0));
          }
}

which will basicaly take already existing JFrame and add new content on the end of what ever is already in

So either clear the JFrame before you add new content or call new separate JFrame to display the content over there

Hey Peter,

Thanks, i modified the action listener code to

FROM
Container pane = getContentPane();
pane.setLayout(new GridLayout(5,2));

TO
setContentPane(new Container());
Container pane = getContentPane();
pane.setLayout(new GridLayout(5,2));

This removed the old menu and was replaced my new window.

Just another quick question

If i make a class out of

public void displayMenu()
    {
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(5,0));
        
        pane.add(newSongB);
        pane.add(deleteSongB);
        pane.add(playlistB);
        pane.add(searchB);
        pane.add(exitB);
        
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }

Which was in my main method will this work, i tried to implement it into a action button so i can access my main menu from other pages but i can not get it to operate

Some modifications done, see red sections. I think they are self-explanatory

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

public class Interface extends JFrame
{
	public static final int WIDTH = 600;
	public static final int HEIGHT = 500;
	int i = 0;
	private JButton newSongB, exitB, deleteSongB, playlistB, searchB;
	private NewSongButtonHandler nsHandler;
	private DeleteSongButtonHandler dsHandler;
	private PlaylistButtonHandler plHandler;
	private SearchButtonHandler sbHandler;
	private ExitButtonHandler ebHandler;
	
	public JLabel nameL, artistL, fileSizeL, durationL;
	public JTextField nameTF, artistTF, fileSizeTF, durationTF;
	public JButton menuB,enterSongB;
	public MainMenuHandler mmHandler;
	public EnterSongDataHandler sdHandler;
	
	//private void run()
	//{
	//This method should control the flow of the program
	//and have all code for accessing the playlists
	//and song database
	
	
	public Interface ()
	{		
		newSongB = new JButton("Enter New Song");
		nsHandler = new NewSongButtonHandler();
		newSongB.addActionListener(nsHandler);
		
		deleteSongB = new JButton("Delete Song");
		dsHandler = new DeleteSongButtonHandler();
		deleteSongB.addActionListener(dsHandler);
		
		playlistB = new JButton("Playlist");
		plHandler = new PlaylistButtonHandler();
		playlistB.addActionListener(plHandler);
		
		searchB = new JButton("Search Database");
		sbHandler = new SearchButtonHandler();
		searchB.addActionListener(sbHandler);
		
		exitB = new JButton("Exit");
		ebHandler = new ExitButtonHandler();
		exitB.addActionListener(ebHandler);
		displayMenu();		
	}
	
	public void displayMenu()
    {
    	setTitle("Mp3 database: Please select Function");
    	setContentPane(new Container());
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(5,0));
        
        pane.add(newSongB);
        pane.add(deleteSongB);
        pane.add(playlistB);
        pane.add(searchB);
        pane.add(exitB);
        
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }
	
	private class NewSongButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			//JFrame newFrame = new JFrame("This frame popped up");
			//newFrame.setSize(100,100);
			//newFrame.setVisible(true);
			
			setTitle("Enter Song Info");
			
			nameTF = new JTextField(10);
			artistTF = new JTextField(10);
			fileSizeTF = new JTextField(10);
			durationTF = new JTextField(10);
			
			nameL = new JLabel("Enter Song Name: ",SwingConstants.CENTER);
			artistL = new JLabel("Enter Artist Name: ",SwingConstants.CENTER);
			fileSizeL = new JLabel("Enter File Size (in Kbytes)",SwingConstants.CENTER);
			durationL = new JLabel("Enter song Duration (in Seconds)",SwingConstants.CENTER);
			
			enterSongB = new JButton("Add Song");
			sdHandler = new EnterSongDataHandler();
			enterSongB.addActionListener(sdHandler);
			
			menuB = new JButton("Main Menu");
			mmHandler = new MainMenuHandler();
			menuB.addActionListener(mmHandler);			
			
			setContentPane(new Container());
			Container pane = getContentPane();
			pane.setLayout(new GridLayout(5,2));
			
			pane.add(nameL);
			pane.add(nameTF);
			pane.add(artistL);
			pane.add(artistTF);
			pane.add(fileSizeL);
			pane.add(fileSizeTF);
			pane.add(durationL);
			pane.add(durationTF);
			pane.add(enterSongB);
			pane.add(menuB);
			
			
			setSize(WIDTH,HEIGHT);
			setVisible(true);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
		}
	
	}
	
	private class DeleteSongButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		}
	
	}
	
	private class PlaylistButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		
		}
	
	}
	
	private class SearchButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		
		}
	
	}
	
	private class ExitButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		System.exit(0);
		}
	
	}
	
	private class EnterSongDataHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
		
		}
	}
	
	private class MainMenuHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			displayMenu();
		}
	}
	
	
	//}
	public static void main(String[] args)
	{
		Interface intFace = new Interface();
		//intFace.run();
	}
}

Thanks heaps,

i implemented that code and it works great, now i can use the class over and over from different menus

Just had a big mental block,

Thanks again

Bleh, got back to this thread just a little too late I guess, but perhaps you will still see this post.

If you wish to organize the UI sections in a more modular and manageable way, you might consider this arrangement

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

public class Interface extends JFrame {

    public Interface() {
        displayMenu();
        setSize(600, 500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void displayMenu() {
        setTitle("Mp3 database: Please select Function");
        Container pane = getContentPane();
        pane.removeAll();
        pane.add(new MenuPanel(this));

        pane.validate();
        pane.repaint();
    }

    public void addNewSong() {
        setTitle("Enter Song Info");
        Container pane = getContentPane();
        pane.removeAll();
        pane.add(new SongDataPanel(this));
        pane.validate();
        pane.repaint();
    }

    /** Main menu UI */
    private class MenuPanel extends JPanel {

        private Interface programFrame;
        private JButton newSongB, exitB, deleteSongB, playlistB, searchB;
        private NewSongButtonHandler nsHandler;
        private DeleteSongButtonHandler dsHandler;
        private PlaylistButtonHandler plHandler;
        private SearchButtonHandler sbHandler;
        private ExitButtonHandler ebHandler;

        public MenuPanel(Interface programFrame) {
            super();
            this.programFrame = programFrame;

            setLayout(new GridLayout(5, 0));

            newSongB = new JButton("Enter New Song");
            nsHandler = new NewSongButtonHandler();
            newSongB.addActionListener(nsHandler);
            add(newSongB);

            deleteSongB = new JButton("Delete Song");
            dsHandler = new DeleteSongButtonHandler();
            deleteSongB.addActionListener(dsHandler);
            add(deleteSongB);

            playlistB = new JButton("Playlist");
            plHandler = new PlaylistButtonHandler();
            playlistB.addActionListener(plHandler);
            add(playlistB);

            searchB = new JButton("Search Database");
            sbHandler = new SearchButtonHandler();
            searchB.addActionListener(sbHandler);
            add(searchB);

            exitB = new JButton("Exit");
            ebHandler = new ExitButtonHandler();
            exitB.addActionListener(ebHandler);
            add(exitB);

        }

        private class NewSongButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                programFrame.addNewSong();
            }
        }

        private class DeleteSongButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
            }
        }

        private class PlaylistButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
            }
        }

        private class SearchButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
            }
        }

        private class ExitButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
    }

    /** "Add new song" UI */
    private class SongDataPanel extends JPanel {

        private Interface programFrame;
        private JLabel nameL, artistL, fileSizeL, durationL;
        private JTextField nameTF, artistTF, fileSizeTF, durationTF;
        private JButton menuB, enterSongB;
        private MainMenuHandler mmHandler;
        private EnterSongDataHandler sdHandler;

        public SongDataPanel(Interface programFrame) {
            super();
            this.programFrame = programFrame;

            nameTF = new JTextField(10);
            artistTF = new JTextField(10);
            fileSizeTF = new JTextField(10);
            durationTF = new JTextField(10);

            nameL = new JLabel("Enter Song Name: ", SwingConstants.CENTER);
            artistL = new JLabel("Enter Artist Name: ", SwingConstants.CENTER);
            fileSizeL = new JLabel("Enter File Size (in Kbytes)", SwingConstants.CENTER);
            durationL = new JLabel("Enter song Duration (in Seconds)", SwingConstants.CENTER);

            enterSongB = new JButton("Add Song");
            sdHandler = new EnterSongDataHandler();
            enterSongB.addActionListener(sdHandler);

            menuB = new JButton("Main Menu");
            mmHandler = new MainMenuHandler();
            menuB.addActionListener(mmHandler);

            setLayout(new GridLayout(5, 2));

            add(nameL);
            add(nameTF);
            add(artistL);
            add(artistTF);
            add(fileSizeL);
            add(fileSizeTF);
            add(durationL);
            add(durationTF);
            add(enterSongB);
            add(menuB);

        }

        private class EnterSongDataHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
            }
        }

        private class MainMenuHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                programFrame.displayMenu();
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                Interface intFace = new Interface();
            }
        });
    }
}

For ease of display and copy-paste, I left each UI panel an inner class of Interface here, but really they could each be in a separate file. That would keep each subsection of your program more organized as you added more panels.

commented: Beautiful work as always +7

Hey Ezzaral

Thats a neat way to set it out, but what do these functions do

public MenuPanel(Interface programFrame) {           
super();            
this.programFrame = programFrame;

what is super();?

public static void main(String[] args) {        
EventQueue.invokeLater(new Runnable() {

What does EventQueue.invokeLater invoke?

Thanks for your time

Super() invokes the parent class constructor there. It's necessary when you override a constructor in a sub-class to ensure that the functions performed by that constructor in the parent class get executed. "Super" can also be invoked in methods that are overridden. It invokes that method with that same signature on the parent class and is often used when you do not want to alter the base class method's functionality, but just extend it a bit.

The programFrame part is just setting that instance's programFrame reference to the one that is passed via the constructor. That gives the panel a reference from which to call methods on the main frame, like displayMenu().

EventQueue.invokeLater() places a task on the AWT event dispatch queue, which is the thread that handles GUI-related events. Starting the frame class this way ensures that construction of the frame occurs on that "GUI event" thread. This is recommended because Swing is essentially designed to run single-threaded and many of its operations are not thread-safe. In many(or even most cases) you wouldn't even notice a difference starting the GUI directly from main(), but in some cases you can get undesirable behavior from concurrency issues. You can read more about this here: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html if you would like more explanation on it.

Cool,

The program i am working on is (probably obviously) a Mp3 song/playlist manager, and there are a couple of other classes (song, playlist,database, and interface) and it doesn't store the songs externally on close.

i was worried that during page changes i might loose some song data or something

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.