|
-
May 28th, 2012, 06:14 PM
#1
window closed event not firing.
I have a class which extends JFrame. In the class there is a method buildGui(). At the bottom of that method I attempt to add a window listener and window closed event. In the event I do a System.out which does not execute, therefore I know the event has not fired.
Code:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.List;
//imports specific to VLCJ and Youtube Video
import uk.co.caprica.vlcj.binding.LibVlc;
//import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import uk.co.caprica.vlcj.binding.internal.libvlc_media_t;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
//import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
//imports for grabbing the NativeLibrary Locations for VLC DLL files
import com.sun.jna.NativeLibrary;
import com.sun.jna.Native;
/**
* This class displays the details of a particular game and
* also displays a youtube video.
* @author 471 Development Team
*/
@SuppressWarnings("serial")
public class GameDetailsDialog extends JFrame{
protected MusicRecording myRecording;
private EmbeddedMediaPlayer mediaPlayer;
private MediaPlayerFactory factory;
//Hold the parent frame
protected JFrame parentContainer;
boolean lineWrap;
JTextArea ta = new JTextArea(10,10);
//CONSTRUCTOR
public GameDetailsDialog(JFrame theParentContainer, MusicRecording theMusicRecording) {
this(theParentContainer, "Game Information For" + theMusicRecording.toString(), theMusicRecording);
}
//CONSTRUCTOR
public GameDetailsDialog(JFrame theParentContainer, String theTitle, MusicRecording theMusicRecording) {
super();
myRecording = theMusicRecording;
parentContainer = theParentContainer;
parentContainer.setSize(1000,400);
buildGui();
}
/**
* This method covers the details of creating and arranging the dialog components.
* @throws IOException
*/
private void buildGui(){
setLayout(new BorderLayout());
final JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
final JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel,BoxLayout.Y_AXIS));
//create and arrange the label, "Title: ... on the infoPanel"
JLabel title = new JLabel("<html><font face = 'Arial' size='5'>Title:</font>" + myRecording.getTitle() + "</html>");
title.setFont(new Font ("Script MT Bold",Font.PLAIN,14));
infoPanel.add(title);
//create and arrange the label, "Year: ... on the infoPanel"
JLabel yearLabel = new JLabel("<html><font face = 'Arial' size='8'>Year:</font>" + myRecording.getYear() + "</html>");
yearLabel.setFont(new Font("Book Antiqua",Font.PLAIN,10));
infoPanel.add(yearLabel);
//create and arrange the label, "Box: ... on the infoPanel"
JLabel boxLabel = new JLabel ("Box:");
infoPanel.add(boxLabel);
//create and arrange the label, "Manual: ... on the infoPanel"
JLabel manualLabel = new JLabel ("Manual:");
infoPanel.add(manualLabel);
//create and arrange the label, "Manual: ... on the infoPanel"
JLabel extrasLabel = new JLabel ("Extras:");
infoPanel.add(extrasLabel);
String imageName = myRecording.getImageName();
ImageIcon recordingIcon = null;
JLabel recordingLabel = null;
// attempt to load the image
try
{
if (imageName.trim().length() == 0)
{
recordingLabel = new JLabel(" Image not available ");
}
else
{
recordingIcon = new ImageIcon("C:\\users\\Matthew\\Desktop\\gamecovers\\" + imageName);
recordingLabel = new JLabel(recordingIcon);
}
}
catch (Exception exc)
{
// okay...couldn't load. Just give a text message.
recordingLabel = new JLabel(" Image not available ");
}
recordingLabel.setBorder(BorderFactory.createRaisedBevelBorder());
infoPanel.add(recordingLabel);
add(BorderLayout.WEST,infoPanel);
String gameDescr = myRecording.getDescr();
System.out.println(gameDescr);
//attempt to load the game description
try
{
String lines2;
FileInputStream in = new FileInputStream("C:\\Users\\Matthew\\Desktop\\gamecovers\\gamedescr\\" + gameDescr + ".txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((lines2=br.readLine())!=null)
{
ta.append(lines2 + "\n");
}
//set the text properties
ta.setLineWrap(true);
ta.setEditable(false);
ta.setCaretPosition(0);
//create textarea to hold the text being read in from file
JScrollPane tracksScrollPane = new JScrollPane(ta);
tracksScrollPane.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
tracksScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
tracksScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
TitledBorder listBorder = BorderFactory.createTitledBorder("Description");
tracksScrollPane.setBorder(listBorder);
tracksScrollPane.getViewport().setViewPosition(new Point(0,0));
//add(BorderLayout.CENTER, tracksScrollPane);
add(BorderLayout.SOUTH,tracksScrollPane);
}
catch(FileNotFoundException exp){
System.out.println("File Not Found");
}
catch(Exception e){
e.printStackTrace();
}
//Load the Native Libraries for the VLC Player
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "C:\\Users\\Matthew\\Downloads\\crs471 - Copy\\crs471 - Copy\\VideoLAN\\VLC\\"
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//create a super panel to hold panel which holds canvas
JPanel panel = new JPanel();
panel.setLocation(520,170);
//panel.setSize(350, 350);
//panel.setBounds(0, 0, 350, 350);
panel.setVisible(true);
//create a panel to hold the Canvas
JPanel cp = new JPanel();
cp.setBackground(Color.BLACK);
cp.setSize(350,350);
//create a Canvas to hold the video player
Canvas vs = new Canvas();
vs.setSize(500, 350);
// vs.setBackground(Color.BLACK);
vs.setVisible(true);
//add the canvas to the panel
cp.add(vs,BorderLayout.CENTER);
//add the panel to the app
panel.add(cp,BorderLayout.CENTER);
add(BorderLayout.CENTER,panel);
//create a Media Object that will play the actual video
factory = new MediaPlayerFactory();
mediaPlayer = factory.newEmbeddedMediaPlayer();
// mediaPlayer.toggleFullScreen();
mediaPlayer.setVideoSurface(factory.newVideoSurface(vs));
mediaPlayer.setFullScreen(true);
mediaPlayer.setPlaySubItems(true); // <--- This is very important for YouTube media
mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter(){
@Override
public void mediaSubItemAdded(MediaPlayer mediaPlayer, libvlc_media_t subItem){
List<String> items = mediaPlayer.subItems();
System.out.println(items);
}
});
String link = myRecording.getLink();
int time = myRecording.getTime();
String t1 = Integer.toString(time);
String t2 = ":start-time=" + t1;
System.out.println(time);
mediaPlayer.playMedia(link,t2);
}
});
// locate this window based off of the parent frame
Point parentLocation = parentContainer.getLocation();
this.setLocation(parentLocation.x + 50, parentLocation.y + 50);
this.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent ev) {
System.out.println("This event has fired");
mediaPlayer.stop();
mediaPlayer.release();
factory.release();
}
});
pack();
}
//Closes the dialog when ok button is pressed
class OkButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent event)
{
setVisible(false);
mediaPlayer.release();
factory.release();
}
}
}
-
May 29th, 2012, 03:13 PM
#2
Re: window closed event not firing.
Have you called the frame's setDefaultCloseOperation(..) method to close the window rather than the default action of hiding it.
-
May 29th, 2012, 04:03 PM
#3
Re: window closed event not firing.
No. I have not. Here let me break out the pieces that are important so it is easier to understand.
Code:
public class GameDetailsDialog extends JFrame{
...
protected JFrame parentContainer;
...
public GameDetailsDialog(JFrame theParentContainer, MusicRecording theMusicRecording) {
this(theParentContainer, "Game Information For" + theMusicRecording.toString(), theMusicRecording);
}
//CONSTRUCTOR
public GameDetailsDialog(JFrame theParentContainer, String theTitle, MusicRecording theMusicRecording) {
super();
myRecording = theMusicRecording;
parentContainer = theParentContainer;
parentContainer.setSize(1000,400);
buildGui();
}
...
Inside the buidGui() method
Code:
// locate this window based off of the parent frame
Point parentLocation = parentContainer.getLocation();
this.setLocation(parentLocation.x + 50, parentLocation.y + 50);
this.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent ev) {
System.out.println("This event has fired");
mediaPlayer.stop();
mediaPlayer.release();
factory.release();
}
});
pack();
-
May 30th, 2012, 03:37 AM
#4
Re: window closed event not firing.
You have not understood my last post.
The window closed event will only get fired if the window closes. The default action for the "window close" (ie 'X') button is to hide the window and not to close the window, so the event does not get fired. You need to call
setDefaultCloseOperation(..) with the either WindowConstants.DISPOSE_ON_CLOSE or JFrame.EXIT_ON_CLOSE. This changes the default behaviour of the window and causes it to close when 'X' is pressed and so the event is fired.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|