0

I have created a simple VLCJ project that consists of a simple embedded player and a button to exit.

The code is as follows:

package test;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

public class Demo {


private final JFrame frame; 
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JPanel videoPane;
private JPanel buttonPane;
private Button exitButton;
private ActionListener a;

private static String vlc_location = "C:\\Program Files\\VideoLAN\\VLC";

public static void main(String[] args) {

    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlc_location);
    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Demo().run();
        }
    });
}

public Demo() { 

    mediaPlayerComponent = new EmbeddedMediaPlayerComponent();

    a = new MyActionListener();
    exitButton = new Button("Exit");
    exitButton.setActionCommand("Exit app");        
    exitButton.addActionListener(a);

    buttonPane = new JPanel();
    buttonPane.setLayout(new BorderLayout());
    buttonPane.setBackground(Color.black);
    buttonPane.add(exitButton, BorderLayout.CENTER);

    videoPane = new JPanel();
    videoPane.setLayout(new BorderLayout());
    videoPane.setBackground(Color.black);
    videoPane.add(mediaPlayerComponent, BorderLayout.CENTER);
    videoPane.add(buttonPane, BorderLayout.PAGE_END);


    frame = new JFrame("vlcj demo");        
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(100, 100);
    frame.setSize(1200, 800);       
    frame.setContentPane(videoPane);        
    frame.setVisible(true);
}

public void run() {         
    mediaPlayerComponent.getMediaPlayer().playMedia(video_file);
}

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        String s = arg0.getActionCommand();

        if (s.equals("Exit")) {
            System.exit(0);
        }

    }

}

}

The problem is that the button does show up but it cannot be clicked. When i removed the videoPane, it was back to clickable! Any ideas if I'm missing something?

I am using the version 2.1.0 for vlcj.

Thanks!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Eugene
  • 1,013
  • 1
  • 22
  • 43
  • 1
    Try using a JButton instead of a Button, mixing heavy and light weight components is never a good idea – MadProgrammer Feb 04 '13 at 10:08
  • I have tried using JButton but that causes the jpanel not to even show the button. I have tried a combination of panel and jpanel too. – Eugene Feb 04 '13 at 10:15
  • The problem MIGHT have something to do with playing the media within the EDT. Try starting the media in a separate thread (just set up and prepare the UI in the EDT first) – MadProgrammer Feb 04 '13 at 10:53

1 Answers1

1

Thanks MadProgrammer for your advise. I went on to think about it and tried commenting away the line of code in run(). The JButton came back!

However, when i un-commented the code in run(), the JButton disappeared. I was thinking maybe the Swing runnable was causing issue with the creation of the JButton.

Hence, what i did was to comment away the whole Swing runnable and just use:

final Demo demo = new Demo();
demo.run();

The demo can now play video and display the Exit button, thanks!

Eugene
  • 1,013
  • 1
  • 22
  • 43