I am writing a small Swing program which involves embedding a video player in the interface. In order to achieve this, I am using vlcj.
While the GUI itself has some more components, here is an analog code structure:
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import javax.swing.*;
import java.awt.*;
public class MyTestClass extends JFrame {
public MyTestClass(){
EmbeddedMediaPlayerComponent playerCmpt =
new EmbeddedMediaPlayerComponent();
playerCmpt.setPreferredSize(new Dimension(200, 100));
JPanel leftPane = new JPanel();
leftPane.setPreferredSize(new Dimension(100, 100));
JSplitPane mainSplit = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
leftPane, playerCmpt);
this.add(mainSplit);
this.pack();
this.setVisible(true);
}
public static void main(String[] args){
new MyTestClass();
}
}
(I tried this code separately, and I'm facing the same problem as in my GUI)
Basically, the window has two parts: a left panel in which I display some data, and a right panel in which the video is to be embedded. The panels are put together in a JSplitPane
is order to allow the user to allocate the amount of room he desires for the player (and therefore, for the video itself). At first, the components get 100 and 200 pixels in width, respectively.
The problem is: EmbeddedMediaPlayerComponent
is getting a little too confortable. While I have set it to a preferred size of 200x100, it refuses to shrink down once the vertical split has been moved. That is, I can't bring the player below 200 pixels in width, and once I've enlarge it, I can't bring it back to 200 pixels... Setting a maximum size doesn't change anything. This little problem is annoying because it forces my left panel to shrink again and again, until it becomes practically invisible.
Is there any way I could have the media player follow the constraints set by JSplitPane
as the user tries to resize the components? If it's any use, the left pane contains a JTree
in my application, which also gets crushed by the player.
This one works for me. Just improve the code to fit for your purpose.
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.*;
import javax.swing.*;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
public class MyTestClass extends JFrame {
public MyTestClass() {
String vlcPath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
EmbeddedMediaPlayerComponent playerCmpt = new EmbeddedMediaPlayerComponent();
playerCmpt.setPreferredSize(new Dimension(200, 100));
JPanel leftPane = new JPanel();
leftPane.setPreferredSize(new Dimension(100, 100));
JPanel playerPanel = new JPanel(new BorderLayout());
playerPanel.add(playerCmpt);
JSplitPane mainSplit = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
leftPane, playerPanel);
playerPanel.setMinimumSize(new Dimension(10, 10));
this.add(mainSplit);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyTestClass();
}
}