SwingPropertyChangeSupport to dynamically update JTextArea
When I put displayOutput.setText(mList)
within the propertyChange
method, it worked:
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (pcEvt.getPropertyName().equals(
ArrayForUpdating.BOUND_PROPERTY)) {
mList = (pcEvt.getNewValue().toString());
displayOutput.setText(mList);
}
}
Thank you to everyone for your help and especially your patience.
SwingPropertyChangeSupport refresh GUI after reading from a file
This looks to be a problem of references.
You appear to have at least two ArrayForUpdate objects, one that is listened to by the GUI, and another completely distinct instance that is held inside of the S_Record class. The latter one is the one that is getting the information from the file while the former one, the one being listened to by the GUI is not. The solution: make sure that there is only one ArrayForUpdate object that is used in both places. Perhaps you can pass a reference of the GUI's instance into your S_Record object via its constructor?
Changing the Text of A JLabel in Another Class (Java)
First, try to not call Class names as its behaviour AddAssigment is a bad name for a class. Should be AddAssignmentPanel for example.
Second you should read this tutorial and then use it Property Change Listeners, (basically an implementation of Observer Pattern
).
Observer
in your case is the class havinglblAssignmentNa
property (usePropertyChangeListener
)Observable
in your case is the class havingtextfield
property (UsePropertyChangeSupport
)
So you have to do something like this.
- Register the observer class into the observable class
- When event happens, in your case,
btnCreateAssignment
pressed you should notify your observers.
So in ViewAssigments
should be defined like this.
public class ViewAssigments extends Something implements PropertyChangeListener
In AddAssigment
register ViewAssigment
as an observer
In btnCreateAssigment
event you have to call propertyChangeSupport.firePropertyChange(..)
and it's send to the observers to notify that state changes.
Additionally if your classes extends JComponent
you have a PropertyChangeSupport instance ready for register Listeners
and to firePropertyChange(..)
How to refresh JTable of a JFrame from a another JFrame without closing the first JFrame?
I have been reading all the material provided in the comments, thanks to that I was able to find a solution using JDialog.
So, in the class of the created JDialog, it is important to set the main JFrame instance as the "parent" of the JDialog:
public class UpdtDialog extends javax.swing.JDialog {
MainJFrame frame = new MainJFrame();
public UpdtDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setLocation(50, 300);
setSize(400, 300);
frame = (MainJFrame) parent;
}
//then all the code necessary, in my case it will be button and the
//update method
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//TODO
}
public void updtProdList(String name, Object[] info){
//TODO
}
}
On the main frame the button that shows the JDialog will have the instance of the JDialog class.
public class InterfazSwing extends javax.swing.JFrame {
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
UpdtDialog diag = new UpdtDialog(this, true);
diag.setVisible(true);
}
}
That way, I was able to solve the problem I was facing while updating the JTable.
How to wire one pane to another
Here's an example using the observer pattern, also seen here, here and here. Note that it would also be possible to listen to the combo's model.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
/**
* @see http://en.wikipedia.org/wiki/Observer_pattern
* @see https://stackoverflow.com/a/10523401/230513
*/
public class PropertyChangeDemo {
public PropertyChangeDemo() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new ObserverPanel());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
PropertyChangeDemo example = new PropertyChangeDemo();
}
});
}
}
class ObserverPanel extends JPanel {
private JLabel title = new JLabel("Value received: ");
private JLabel label = new JLabel("null", JLabel.CENTER);
public ObserverPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObserverPanel"));
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(title);
panel.add(label);
this.add(panel);
ObservedPanel observed = new ObservedPanel();
observed.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) {
String value = e.getNewValue().toString();
label.setText(value);
}
}
});
this.add(observed);
}
}
class ObservedPanel extends JPanel {
public static final String PHYSICIST = "Physicist";
private static final String[] items = new String[]{
"Alpher", "Bethe", "Gamow", "Dirac", "Einstein"
};
private JComboBox combo = new JComboBox(items);
private String oldValue;
public ObservedPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObservedPanel"));
combo.addActionListener(new ComboBoxListener());
this.add(combo);
}
private class ComboBoxListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
String newValue = (String) combo.getSelectedItem();
firePropertyChange(PHYSICIST, oldValue, newValue);
oldValue = newValue;
}
}
}
Update child (panel) of CardLayout
The observer pattern, discussed here, is the key to this. In particular, both panels could listen to a common model, which would fire events to update each listening panel. Examples using PropertyChangeListener
may be found here and here.
Timer or other idea required to allow code to continue execution after calling method and JOptionPane
But it hangs on the JOptionPane. I need a way to make it so that the
program either keeps going underneath the JOptionPane or to close the
pane after about 10 seconds. I am not sure how to work either into my
code, currently
there are two ways
(better easier, comfortable) create
JDialog(JFrame parent, boolean true)
, with default close operationHIDE_ON_CLOSE
, only oneJDialog
aslocal variabl
e, reuse thisObject
bysetVisible(false/true)
looping inside arrays of all JOptionPanes (all exists there untill current JVM is alive) by
@kleopatra
Rendering javax.swing.JButton in a lwjgl window
Adding a javax.swing.JButton
to a java.awt.Canvas
used for Display
may be subject to rendering artifacts. Instead, try one of the following alternatives:
Add the button to a
JPanel
, as shown here. Put yourDisplay
in aCanvas
, as shown here. Put both in a suitable layout, e.g.add(canvas, BorderLayout.CENTER;
add(panel, BorderLayout.SOUTH);Control your
Display
using components in a modeless dialog, illustrated here.
Related Topics
Http Basic Authentication in Java Using Httpclient
What's the Difference Between Softreference and Weakreference in Java
Keyword for the Outer Class from an Anonymous Inner Class
How to Get the Current Time in Yyyy-Mm-Dd Hh:Mi:Sec.Millisecond Format in Java
Differencebetween Method Overloading and Overriding
Abstracttablemodel Gui Display Issue
Importing Two Classes with Same Name. How to Handle
Classpath Resource Not Found When Running as Jar
How to Mock Private Method for Testing Using Powermock
Convert Iterable to Stream Using Java 8 Jdk
Comparing Java Enum Members: == or Equals()
Why Does Changing the Returned Variable in a Finally Block Not Change the Return Value
Differencebetween "Class.Forname()" and "Class.Forname().Newinstance()"
Controlling Maven Final Name of Jar Artifact
Seeking Useful Eclipse Java Code Templates
Why Java Classes Do Not Inherit Annotations from Implemented Interfaces