Please have a look at the attached images. The vlcj media component hides the control buttons when the window is resized to original dimensions after maximizing.
public class PlayerVLCPanel extends JPanel {
private EmbeddedMediaPlayerComponent playerComponent;
public PlayerVLCPanel() {
this.playerComponent = ComponentStore.getCameraStreamer();
setLayout(new BorderLayout());
add(this.playerComponent.getVideoSurface(), BorderLayout.CENTER);
}
}
public class CameraJobControlPanel extends JPanel {
public CameraJobControlPanel() {
DesignGridLayout layout = new DesignGridLayout(this);
layout.row().grid(expertDnLabel).add(expertDnField);
layout.row().right().add(startCamButton, stopCamButton, startLazerButton, stopLazerButton).fill();
}
}
public class MainPlayerContainer extends JPanel {
public MainPlayerContainer() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(new EmptyBorder(3, 3, 3, 3));
add(topRegion()); // contains PlayerVLCPanel
add(bottomRegion()); // contains the Control buttons
}
}
Finally,
JFrame frame = new JFrame("vlcj Testing");
frame.add(new MainPlayerContainer());
frame.setLocation(100, 100);
frame.setSize(1050, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
How can I prevent this behaviour ? Thanks in advance.
Related
I'm dealing with a very specific issue. When I remove all the components of a transparent JPanel (background color with 0 alpha) and add new components, the removed elements are still displayed in the JPanel. Here is an example of the behavior.
And here is the code to generate it:
class MyPanel extends JPanel {
public MyPanel() {
this.setLayout(new FlowLayout());
this.setBackground(new Color(0, 0, 0, 0));
}
public void updateComponents() {
this.removeAll();
this.revalidate();
int n = new Random().nextInt(10);
for (int i = 0; i < n; i++) {
this.add(new JButton("Dummy Button"));
}
}
}
public class Runner {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 720);
MyPanel panel = new MyPanel();
JButton btUpdate = new JButton("Update");
btUpdate.addActionListener(actionEvent -> {
panel.updateComponents();
});
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(btUpdate, BorderLayout.NORTH);
frame.getContentPane().add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
Can somebody help me?
this.revalidate();
The point of invoking revalidate() is to invoke the layout manager.
You need to invoke that method AFTER you add all the components to the panel.
You also need to make sure the components are painted so the code should be:
this.revalidate();
this.repaint();
I have even tried using the AlphaContainer
How? Post your code showing what you attempted.
The usage should be:
//frame.getContentPane().add(panel, BorderLayout.SOUTH);
frame.getContentPane().add(new AlphaContainer(panel), BorderLayout.SOUTH);
I'm using a JPanel and tring to create a two new panels programatically inside this JPanel
public class MainWindow extends javax.swing.JFrame {
/**
* Creates new form MainWindow
*/
private javax.swing.JPanel jviewer;
public MainWindow() {
initComponents();
jviewer = new ImageRender(123);
}
}
For that reason, I have the next extension:
public class ImageRender extends JPanel {
JPanel mainViewer = new JPanel();
JPanel galleryViewer = new JPanel();
public ImageRender(Integer itemnum) {
setLayout(null);
mainViewer = new JPanel();
mainViewer.setBackground(Color.red);
mainViewer.setBounds(0, 0, 200, 200);
galleryViewer = new JPanel();
galleryViewer.setBackground(Color.green);
galleryViewer.setBounds(210, 0, 50, 200);
this.add(mainViewer);
add(galleryViewer);
mainViewer.setVisible(true);
setVisible(true);
System.out.println("Se ha finalizado esta tarea");
}
}
But, at this point, is not displaying any of the JPanel created in the ImageRender.java also any errors.
Someone has an idea about how to fix my implementation?
Reason of that is that you create an ImageRender instance, but it is never being added to the JFrame. Use add method in order to achieve that. Also do not use setLayout(null) and setBounds. Use a layout manager instead. Components will be validated automatically, plus you have a resizable window.
The fact ImageRender extends JPanel means that an ImageRender object is also a JPanel, hence it can be added to the frame. (Already mentioned in comments)
Take a look at an example (assume ImageRenderer class is in another file):
public class Test extends JFrame {
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new ImageRenderer()); //Create and add a new ImageRendere panel
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
private static class ImageRenderer extends JPanel {
public ImageRenderer() {
super(new GridLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBackground(Color.green);
add(leftPanel);
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setBackground(Color.blue);
add(rightPanel);
}
}
}
Selected code from SCMain.java:
public JPanel createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
return contentPane;
}
public JPanel populateContentPane() {
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new AddAccountForm(this, this.getInputSet());;
JPanel panel2 = new JPanel();
container.add(Box.createRigidArea(new Dimension(0, 5)));
container.add(panel1);
container.add(Box.createRigidArea(new Dimension(0, 5)));
container.add(panel2);
container.add(Box.createGlue());
return container;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Sole Commando v1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
frame.setJMenuBar(this.createMenuBar());
frame.setContentPane(this.createContentPane());
// Add split panels
frame.add(populateContentPane(), BorderLayout.CENTER);
frame.pack();
//Display the window.
frame.setSize(1080, 1080);
frame.setVisible(true);
}
Selected code from AddAccountForm.java:
public class AddAccountForm extends JPanel implements ActionListener{
AddAccountForm(SCMain main, Set<String> InputSet) {
//Combobox setup
setSize(300, 300);
System.out.println(Arrays.toString(storeNameList.toArray()));
submitButton.addActionListener(this);
}
public JPanel getAddAccountRoot() {
return addAccountRoot;
}
private void createUIComponents() {
// TODO: place custom component creation code here
storeNames = new JComboBox();
}
}
I tested using the AddAccountForm.java as a JFrame (extend JFrame instead of extend JPanel, and adding pack(), setContentPane(addAccountRoot) to AddAccountForm.java) and it brought up the correct AddAccountForm GUI if I just did:
SCMain new1 = new SCMain();
AddAccountForm new2 = new AddAccountForm(new1, new1.getInputSet());
However, when using it as a JPanel (panel1 in the above SCMain.java code) and running SCMain, AddAccountForm GUI does not show up at all.
Note: The JPanel AddAccountForm was created in IntelliJ GUI Builder, but as I said previously it works as a JFrame so the code must be somewhat correct.
I have two classes - one representing a JFrame and the other representing a JPanel. On the JPanel I have JComponents that I would want to use in capturing user input. On the JFrame I have a JButton that displays the JPanel on JDialog when a user hit it. Below is the code to achieve this:
A class that is extending a JFrame.
public class ControlPanel extends JFrame {
public JButton display;
ControlPanel() {
this.createUI();
// button action
this.display.addActionListener((ActionEvent) -> {
AddUser ad = new AddUser();
JDialog dialog = new JDialog(this,
Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
dialog.setSize(300, 300);
dialog.add(ad);
});
}
public void createUI() {
this.setTitle("Dispalying a JPanel on JDialog");
this.setSize(new Dimension(300, 300));
this.setVisible(true);
this.setLayout(new BorderLayout(5, 5));
this.display = new JButton("Display");
this.add(new JLabel("North", JLabel.CENTER), BorderLayout.NORTH);
this.add(this.display, BorderLayout.CENTER);
this.add(new JLabel("South", JLabel.CENTER), BorderLayout.SOUTH);
this.add(new JLabel("East"), BorderLayout.EAST);
this.add(new JLabel("West"), BorderLayout.WEST);
}
public static void main(String[] args) {
ControlPanel cp = new ControlPanel();
}
}
A class that is extending a JPanel.
This panel is the one that I would want to be added on to the JDialog.
public class AddUser extends JPanel {
public AddUser() {
this.prepareUI();
}
public void prepareUI() {
this.setPreferredSize(new Dimension(200, 200));
this.setLayout(new GridLayout(1, 4, 5, 5));
this.add(new JLabel("Label1"));
this.add(new JLabel("Label2"));
this.add(new JLabel("Label2"));
this.add(new JLabel("Label2"));
/* Code to test if the panel is displaying fine.
* The panel is displaying fine.
* JFrame fr = new JFrame();
fr.setVisible(true);
fr.setSize(300, 300);
fr.setLayout(new BorderLayout(5, 5));
fr.add(this, BorderLayout.CENTER);
fr.pack();*/
}
public static void main(String[] args) {
AddUser ad = new AddUser();
}
}
NOTE : I am doing this because I want the panel to be modal - the user must not interact with components on the JFrame before he is finished with the JPanel.
Problem : The Jpanel [this one:AddUser ad = new AddUser();] is not showing when the JDialog is shown.
Question : What went wrong with my code and how do I solve it?
Below is the out put when the display button is clicked.
I am working on the editor. I am using Java swing for it. I have embedded a JTextArea with JScrollPane. i want to position the jtextarea of particular size at the middle of JScrollPane. To do this I used setLocation function. But this is not working?
public class ScrollPaneTest extends JFrame {
private Container myCP;
private JTextArea resultsTA;
private JScrollPane scrollPane;
private JPanel jpanel;
public ScrollPaneTest() {
resultsTA = new JTextArea(50,50);
resultsTA.setLocation(100,100);
jpanel=new JPanel(new BorderLayout());
jpanel.add(resultsTA,BorderLayout.CENTER);
scrollPane = new JScrollPane(jpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(800, 800));
scrollPane.setBounds(0, 0, 800, 800);
setSize(800, 800);
setLocation(0, 0);
myCP = this.getContentPane();
myCP.setLayout(new BorderLayout());
myCP.add(scrollPane);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new ScrollPaneTest();
}
}
You simply have to add the JTextArea to the JScrollPane, and add it to the CENTER of the JPanel having BorderLayout.
Don't use AbsolutePositioning. Add a proper LayoutManager, and let LayoutManager do the rest for positioning and sizing your components on the screen.
In order to use the setBounds(...) method you have to use a null Layout for your component, which is not worth using, provided the perspective, as mentioned in the first paragraph of the AbsolutePositioning. Though in the code example provided by you, you are doing both the thingies together i.e. using Layout and using AbsolutePositioning, which is wrong in every way. My advice STOP DOING IT :-)
In the example provided the ROWS and COLUMNS provided by you are sufficient to size the JTextArea by the Layout concern.
Code Example :
import java.awt.*;
import javax.swing.*;
public class Example
{
private JTextArea tarea;
private void displayGUI()
{
JFrame frame = new JFrame("JScrollPane Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JScrollPane textScroller = new JScrollPane();
tarea = new JTextArea(30, 30);
textScroller.setViewportView(tarea);
contentPane.add(textScroller);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new Example().displayGUI();
}
});
}
}