Swingpropertychangesupport to Dynamically Update Jtextarea

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 having lblAssignmentNa
    property (use PropertyChangeListener)
  • Observable in your case is the class having textfield
    property (Use PropertyChangeSupport)

So you have to do something like this.

  1. Register the observer class into the observable class
  2. 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.

Sample Image

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 operation HIDE_ON_CLOSE, only one JDialog as local variable, reuse this Object by setVisible(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 your Display in a Canvas, 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



Leave a reply



Submit