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!
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!
Related
I want to be able to call the Introduction.Intro() method into my main file code, but it tells me I am unable to call a non-static method intro from a static context. Since I am still fairly new to coding I'm not entirely sure what the problem is. I've added my codes down below. I've tried countless online methods but sadly none have seemed to work.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Start extends JFrame implements ActionListener
{
private JFrame Main;
private JPanel PanelA, PanelB, PanelC;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public Start ()
{
//Button
Button = new JButton("Start");
Button.addActionListener(new ButtonListener());
//Text
Text = new JLabel("Welcome To The Game"); //ADD NAME OF THE GAME
//Image
Image = new ImageIcon(getClass().getResource("download.jfif")); //ADD THE IMAGE FOR WELCOME
ImageL = new JLabel(Image);
//Top Panel (PanelA) - Image
PanelA = new JPanel();
PanelA.setBorder(BorderFactory.createEmptyBorder(0,200,150,200));
PanelA.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelA.add(ImageL);
//Middle Panel (PanelB) - Text
PanelB = new JPanel();
PanelB.setBorder(BorderFactory.createEmptyBorder(50,200,10,200));
PanelB.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelB.add(Text);
//Bottom Panel (PanelC) - Buttons
PanelC = new JPanel();
PanelC.setBorder(BorderFactory.createEmptyBorder(0,200,20,200));
PanelC.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelC.add(Button);
//Main Frame
Main = new JFrame ();
Main.add(PanelA, BorderLayout.NORTH);
Main.add(PanelB, BorderLayout.CENTER);
Main.add(PanelC, BorderLayout.SOUTH);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main.setTitle("GAME TITLE"); //ADD THIS LATER
Main.pack();
Main.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae)
{
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button)
{
Introduction.Intro1(); //THESE LINE RIGHT HERE
return null; //THESE LINE RIGHT HERE
}
}
}
public static void main(String[] args)
{
new Start();
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Introduction
{
private JFrame Main;
private JPanel PanelD;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public void Intro()
{
Image = new ImageIcon(getClass().getResource("guy.jfif"));
ImageL = new JLabel(Image);
PanelD = new JPanel();
PanelD.setBorder(BorderFactory.createEmptyBorder(0,100,10,100));
PanelD.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelD.add(ImageL);
PanelD.setVisible(true);
Main.add(PanelD, BorderLayout.NORTH);
}
}
EDIT: So I made another method in the Introduction class where I added this line of code, it managed to fix the error, however, the panel isn't being saved and my JFrame is outputting blank.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
If you are looking to initialize the Introduction class in main method of Start class, You can add belo code in main method after Start()
Introduction M = new Introduction();
You main method becomes :
public static void main(String[] args)
{
new Start();
Introduction M = new Introduction();
m.Intro
}
Looking at this set of code, It looks like there is incompatible issue, as you have declare JFrame as return type, while you are returning instance of Introduction.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
Hey guys I really appreciate any help but I was recently coding this easy software and ran into a weird issue. I was trying to use an ActionListener but I some how messed up. Its been awhile since I have done this. Sorry if this is just a careless mistake.
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class startScreen implements ActionListener {
JFrame mainFrame;
JPanel mainPanel;
JButton gotIt;
private final String gotItText = "Got It!";
public static void main(String[] args) {
startScreen a = new startScreen();
a.screenSetup();
}
private void screenSetup() {
mainFrame = new JFrame();
mainPanel = new JPanel();
gotIt = new JButton();
mainFrame.add(mainPanel);
mainPanel.add(gotIt);
gotIt.setText(gotItText);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
mainFrame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
JButton buttonPressed = (JButton) arg0.getSource();
if (buttonPressed==gotIt) {
System.out.println("gotIt has been pressed!");
}
}
}
You should add your actionListener to the component that you want. Here the problem is you defined a function but you did not add it to your button.
I think this link gives you a very good understanding about how it works.
http://alvinalexander.com/java/jbutton-listener-pressed-actionlistener
This question already has answers here:
Elements not showing in GridBagLayout
(2 answers)
Closed 7 years ago.
So I'm trying to create my first GUI in Java but it's coming up blank. As far as I can see I've done everything correct, but of course since it's just blank I'm obviously doing something wrong.
I've got a mainclass and a class for the JFrame and all its contents.
I'm not getting any errors except for the public class MainFrame giving me "The serializable class MainFrame does not declare a static final serialVersionUID field of type long".
package main;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import gui.MainFrame;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run()
MainFrame frame = new MainFrame();
frame.setTitle("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400);
frame.setVisible(true);
}
});
}
}
.
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private JButton button;
private Container cont;
public MainFrame(){
Container cont = new Container();
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton button = new JButton("Change Color");
panel.add(button, BorderLayout.CENTER);
cont.add(panel, BorderLayout.CENTER);
cont.setBackground(Color.GREEN);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cont.setBackground(Color.CYAN);
}
});
}
}
You didn't add Container cont to the frame.
Forgot about adding component to frame:
panel.add(cont);
I have a desktop application using vclj and swing. I have a frame (JFrame component) that contains a video created using vclj (instance of VideoPanel). The problem is that I need to update the video to show in this jframe in runtime. When I try to do it I don't get that it works. Instead, the component of video vclj appears in black color without showing any video.
Code of video component:
public class VideoPanel extends JPanel {
private EmbeddedMediaPlayerComponent mymediaPlayer;
private EmbeddedMediaPlayer mediaPlayer;
private Canvas canvas;
public VideoPanel() {
setLayout(new BorderLayout(10, 10));
Canvas canvas_1 = new Canvas();
add(canvas_1, BorderLayout.CENTER);
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas_1);
mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(videoSurface);
}
public void startPlayer() {
mediaPlayer.playMedia(mediaPath);
}
}
Code called each time that I try to update the video shown in the jframe:
// Remove last video shown
getContentPane().removeAll();
// Create new video vclj
video = new VideoPanel();
// add video to jframe
add(video, BorderLayout.CENTER);
setVisible(true);
// Update hierarchy of components and repaint
revalidate();
repaint();
// Start the player
video.startPlayer();
// Update hierarchy of components and repaint
revalidate();
repaint();
setVisible(true);
pack();
As you can see, I call to revalidate and repaint methods before and after starting player for content is updated in the jframe.
BTW, When I pressed the close button of the window that has the video, it shows a frame of the video.
Thanks in advance!!
i made a simple small example working example with hard coded MediaPaths, replace this through your mediaPath Variable:
VideoFrame.java
package de.professional_webworkx.vlcj;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class VideoFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private VideoPanel videoPanel;
public VideoFrame() {
initializeGUI();
}
private void initializeGUI() {
JPanel buttonPanel = createButtonPanel();
this.setTitle("MyVideoApp");
this.setSize(1024, 768);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
videoPanel = new VideoPanel("/home/ottp/Videos/Test.ogv");
this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
this.getContentPane().add(videoPanel, BorderLayout.CENTER);
this.setVisible(true);
videoPanel.startPlayer();
}
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
JButton nextVideo = new JButton("Next Video");
nextVideo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateVideoPanel();
}
});
buttonPanel.add(nextVideo);
JButton prevVideo = new JButton("Prev Video");
buttonPanel.add(prevVideo);
return buttonPanel;
}
private void updateVideoPanel() {
this.remove(videoPanel);
videoPanel = new VideoPanel("/home/ottp/Videos/Example.ogv");
this.getContentPane().add(videoPanel, BorderLayout.CENTER);
videoPanel.startPlayer();
revalidate();
}
}
VideoPanel.java
package de.professional_webworkx.vlcj;
import java.awt.BorderLayout;
import java.awt.Canvas;
import javax.swing.JPanel;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
public class VideoPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private EmbeddedMediaPlayer mediaPlayer;
private Canvas _canvas;
private String mediaPath;
public VideoPanel(final String mediaPath) {
this.mediaPath = mediaPath;
setLayout(new BorderLayout(10, 10));
_canvas = new Canvas();
add(_canvas, BorderLayout.CENTER);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(_canvas);
mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(videoSurface);
}
public void startPlayer() {
mediaPlayer.playMedia(mediaPath);
}
}
App.java
package de.professional_webworkx.vlcj;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaListPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
/**
* Hello world!
*
*/
public class App
{
private static EmbeddedMediaListPlayerComponent component;
public static void main( String[] args )
{
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App();
}
});
}
private App() {
new VideoFrame();
}
}
It is a maven project, i added vlcj as a maven dependency to my pom.xml, so i had not to add the searchpath. And also the click on the Next Video Button is handled, so update your Videocontent within the updateVideoPanel() Method, there you can have a List or an Array with all your Videos you would like to show.
Hope this helps..
I am trying to create a piano program where you click the key and using an actionlistener is plays the note from the jfugue library. For some reason after about 18 clicks without changing anything the buttons stop working. I cut down the code to analyze why this might happen, thus only two notes.
Thanks in advance for any advice!
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.Color;
import javax.swing.JLayeredPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.*;
import org.jfugue.*;
public class ChordPlayer2 extends JComponent{
public ChordPlayer2(){
final Player player = new Player();
JFrame frame = new JFrame();
JButton cButton, csharpButton;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(0, 0);
buttonPanel.setSize(1700, 1000);
csharpButton = new JButton("");
csharpButton.setLocation(100, 150);
csharpButton.setSize(100,520);
buttonPanel.add(csharpButton);
cButton = new JButton("");
cButton.setLocation(0, 150);
cButton.setSize(160, 800);
buttonPanel.add(cButton);
class cClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C");
}
}
class csClicker implements ActionListener {
public void actionPerformed(ActionEvent event) {
player.play("C#");
}
}
ActionListener c = new cClicker();
cButton.addActionListener(c);
ActionListener cs = new csClicker();
csharpButton.addActionListener(cs);
buttonPanel.setOpaque(true);
//return buttonPanel;
frame.add(buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1700, 1000);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
//JFrame.setDefaultLookAndFeelDecorated(true);
ChordPlayer2 demo = new ChordPlayer2();
}
}
This is a known bug in JFugue:
https://code.google.com/p/jfugue/issues/detail?id=49
The most recent version is claimed to fix this:
https://code.google.com/p/jfugue/downloads/detail?name=jfugue-4.1.0-20120125.jar&can=2&q=