Re: Communicating from event handler to container



On Feb 26, 1:32 pm, "eastside" <easts...@xxxxxxxxxxxxxx> wrote:
I have a class, let's call it AC, which extends JPanel. Each instance of AC
also contains a JTextField. The actionlistener for the text field is also in
the class definition of AC. If a certain condition is detected by the
actionlistener, the object needs to tell the parent object that contains AC
to inform all other instances of AC that they need to be updated. How is
this communication back to the parent object usually accomplished in Java?
Thanks!


maybe(??) this might do what you want

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
public void buildGUI()
{
JPanel p = new JPanel(new GridLayout(5,1));
for(int x = 0; x < 5; x++) p.add(new AC());
JFrame f = new JFrame();
f.getContentPane().add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
class AC extends JPanel
{
static java.util.List acPanels = new java.util.ArrayList();
JTextField tf = new JTextField(5);
public AC()
{
acPanels.add(this);
add(tf);
tf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
for(int x = 0, y = acPanels.size(); x < y; x++)
{
AC acPanel = (AC)acPanels.get(x);
acPanel.tf.setText(acPanel.tf.getText()+x);
}
}
});
}
}

.



Relevant Pages

  • revise my code
    ... public static void main (String[] args) ... public void actionPerformed ... public class Form1 extends JFrame implements ActionListener ... JTextField text1; ...
    (comp.lang.java.programmer)
  • revise this code
    ... public static void main (String[] args) ... public void actionPerformed ... public class Form1 extends JFrame implements ActionListener ... JTextField text1; ...
    (comp.lang.java.programmer)
  • Communicating from event handler to container
    ... I have a class, let's call it AC, which extends JPanel. ... also contains a JTextField. ... actionlistener, the object needs to tell the parent object that contains AC ...
    (comp.lang.java.help)
  • Re: ActionListeners
    ... declare t final: ... final JTextField t; ... public void ActionPerformed{ ... > t.addActionListener(new ActionListener() { ...
    (comp.lang.java.gui)
  • ActionListeners
    ... JTextField t; ... t.addActionListener(new ActionListener() { ... public void ActionPerformed{ ... How do I get a reference to t inside the ActionPerformed method? ...
    (comp.lang.java.gui)