Refreshing existing windows in Java Swing
The best way would be for your Person class to implement ChangeListener
:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/ChangeListener.html
Then, every place that uses an information from person would call person.addChangeListener(this);
.
In every setter of the person class you should call stateChanged
to notify your listeners that Person
has changed.
Every time the windows/screens receive the event from changeListener, they should refresh the proper components (for ex, in a JLabel, you would call label.setText(person.getSurname());
.
How can i refresh window
I just show you the places I changed and added.
EDIT:
public class Edytor extends JFrame{
CObrazek obraz = new CObrazek();//here (new place)
public static boolean pom, pom1;
public static Image image;
public static String sciezka;
public void EdytorShow(String image_path) {//changed
setBounds(420,50,800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
//CObrazek obraz = new CObrazek();//disabled
przybornik przyb = new przybornik();//here (new place)
/**
* We do not want the window to appear without selecting an image.
* For this reason we will use "if"
*/
if(!image_path.equals("null")){
con.add(obraz);
// przybornik przyb = new przybornik();//Moved out of if ;)
pom = false;
image = new ImageIcon(image_path).getImage();
setVisible(true);
}//if
}
public static void main(String args[]) {
Edytor et = new Edytor();
et.EdytorShow("null");
}
}
And in the "przybornik.java" file:
private static boolean isWindowActive = false; //for window active.
public przybornik() {
initComponents();
if(isWindowActive == false){ //window active check here.
setVisible(true);
isWindowActive = true;
}
}
//-------- and:
Edytor edytor = new Edytor();//here (new place)
private void read_butActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new
File(System.getProperty("user.home")));
int path = fileChooser.showOpenDialog(this);
if (path == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " +
selectedFile.getAbsolutePath());
//Edytor.sciezka = selectedFile.getAbsolutePath();
//new Edytor().EdytorShow(selectedFile.getAbsolutePath());//disabled(Moved out of function ;)
edytor.EdytorShow(selectedFile.getAbsolutePath());//newly added
}
System.out.print(Edytor.sciezka);
}
How do I refresh a GUI in Java?
Swing components have a repaint(), revalidate(), and doLayout() method. One of those should probably be able to redraw whichever pieces you want. However, doLayout is not something that you should be taking responsibility for, that's the layout engines responsibility.
You may also want to check out this post, the first response has a pretty good explanation, and links to an article with more detail.
In terms of the second part of your question, I'm not sure, but we may need to see some code to get an idea. Is the 'replaced area' actually being removed from the view?
Refreshing jframe and disposing the older frame
I used
static JFrame frame = new JFrame("Frame");
and the problem is solved
How to refresh graphics of a Window class in Java
I played around a bit with your example and came up with something working, but I wouldn't call it a nice solution.
The main issue seems to be that there is no way to tell the overlay to refresh (or I just have not found it). Just repainting
the overlay does not update it on screen, so the workaround I used is to hide and show it again.
For the timeing of the update interval I used a javax.swing.Timer
.
(In a real version you probably want to start and stop the timer via the MediaPlayerEventListener
).
As a side effect the repaint
method is called and the x
coordinate is adjusted to move the image around the screen.
In the simplified example below (use your main
to run it), I moved a red rectangle with the x coordinate instead of some unknown image.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import com.sun.jna.platform.WindowUtils;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.OverlayApi;
public class AppFrame extends JFrame {
private static final long serialVersionUID = -1569823648323129877L;
public class Overlay extends Window {
private static final long serialVersionUID = 8337750467830040964L;
private int x, y;
private boolean ltr = true;
public Overlay(Window owner) throws HeadlessException {
super(owner, WindowUtils.getAlphaCompatibleGraphicsConfiguration());
setBackground(new Color(0,0,0,0));
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (ltr) {
if (x < 1180)
x += 1;
else
ltr = false;
} else {
if (x < 0)
ltr = true;
else
x -= 1;
}
g.setColor(Color.RED);
g.fillRect(x, y, 100, 100);
String s = Integer.toString(x);
g.setColor(Color.WHITE);
g.drawChars(s.toCharArray(), 0, s.length(), x+10, y+50);
}
}
private EmbeddedMediaPlayerComponent appPlayer;
public void createAndShowGUI() {
appPlayer = new EmbeddedMediaPlayerComponent();
appPlayer.setPreferredSize(new Dimension(1280, 800));
getContentPane().add(appPlayer);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("App");
setVisible(true);
pack();
Overlay overlay = new Overlay(this);
OverlayApi api = appPlayer.mediaPlayer().overlay();
api.set(overlay);
api.enable(true);
//appPlayer.mediaPlayer().media().play(" ... ");
Timer timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
api.enable(false);
api.enable(true);
}
});
timer.setRepeats(true);
timer.setDelay(200);
timer.start();
}
}
If that is an option for you, it might be far easier to use an animated gif
instead. At least that is working on its own (no need for the Timer).
Update:
As you figured out using a JPanel
seems to work better.
Just use setOpaque(false)
to make it transparent.
Here an adjusted example.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.OverlayApi;
public class AppFrame2 extends JFrame {
private static final long serialVersionUID = -1569823648323129877L;
public class OverlayPanel extends JPanel {
private static final long serialVersionUID = 8070414617530302145L;
private int x, y;
private boolean ltr = true;
public OverlayPanel() {
this.setOpaque(false);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (ltr) {
if (x < 1180)
x += 1;
else
ltr = false;
} else {
if (x < 0)
ltr = true;
else
x -= 1;
}
g.setColor(Color.RED);
g.fillRect(x, y, 100, 100);
String s = Integer.toString(x);
g.setColor(Color.WHITE);
g.drawChars(s.toCharArray(), 0, s.length(), x+10, y+50);
}
}
public class Overlay extends Window {
private static final long serialVersionUID = 8337750467830040964L;
OverlayPanel panel;
public Overlay(Window owner) throws HeadlessException {
super(owner);
setBackground(new Color(0,0,0,0));
panel = new OverlayPanel();
this.add(panel);
}
}
private EmbeddedMediaPlayerComponent appPlayer;
public void createAndShowGUI() {
appPlayer = new EmbeddedMediaPlayerComponent();
appPlayer.setPreferredSize(new Dimension(1280, 800));
getContentPane().add(appPlayer);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("App");
setVisible(true);
pack();
Overlay overlay = new Overlay(this);
OverlayApi api = appPlayer.mediaPlayer().overlay();
api.set(overlay);
api.enable(true);
//appPlayer.mediaPlayer().media().play(" ... ");
Timer timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
overlay.panel.repaint();
}
});
timer.setRepeats(true);
timer.setDelay(17);
timer.start();
}
}
Related Topics
In Java, How to Combine Two Json Arrays of Objects
How to Mock Resultset and Populate It Using Mockito in Java
How to Store Original Date/Time Without Timezone Calculation to Mongodb
How to Center Crop a Background Image of Linear Layout
I Want to Get Time Difference Between Two Time in Milisecond
Map Yaml to Object Hashmap in Springboot
Gradle Java9 Could Not Target Platform: 'Java Se 9' Using Tool Chain: 'Jdk 8 (1.8)'
Using Loop to Accept Additional User Input Until User Enters a Value That Ends the Input
Random Number Generation Without Repetition in Java
Android Tv Recyclerview Set Next Focus of Its Item
Create List of Object from Another Using Java 8 Streams
Passing Data into HTML from Java Via Spring
Where Does Gradle Save Dependencies' Jars
Ora-00942 Sqlexception With Hibernate (Unable to Find a Table)
How to Access Variable Outside a Try Catch Block
Spring Boot JPA - Onetomany Relationship Causes Infinite Loop
Setting a Log File Name to Include Current Date in Log4J
How to Query Dynamodb Based on Partition Key and Sort Key [Java]