help writeing to a jtextarea from a diffrent class
so i have a frame with a jtextarea and i want to write to it from a separate class i (for example)
try to write to it from the eventlistener here they are:
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class PFrame extends JFrame
{
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
JPanel pane3 = new JPanel();
JButton browse;
JButton artist;
JButton name;
JTextArea textBox;
JScrollPane scroll;
JLabel numLabel;
int count=0;
public PFrame()
{
super("mikey's program");
setSize(350, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
browse = new JButton("browse");
browse.setActionCommand("browse button");
browse.addActionListener(new ActListener());
textBox = new JTextArea();
textBox.setEditable(false);
scroll = new JScrollPane(textBox);
numLabel = new JLabel("number of songs:" +count);
artist = new JButton("artist");
name = new JButton("name");
BoxLayout box = new BoxLayout(pane1, BoxLayout.Y_AXIS);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
pane1.setLayout(box);
pane1.add(browse);
pane1.add(Box.createRigidArea(new Dimension(0, 10)));
pane1.add(scroll);
pane1.add(numLabel);
pane2.setLayout(new FlowLayout(FlowLayout.LEFT));
pane2.add(artist);
pane2.add(name);
pane3.setLayout(new BoxLayout(pane3, BoxLayout.X_AXIS));
pane3.add(pane1);
pane3.add(pane2);
add(pane3);
//try{
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// SwingUtilities.updateComponentTreeUI(this);
// } catch (Exception e){
// e.getMessage();
// }
setVisible(true);
}
[B] public void setTextBox(String str)
{
this.textBox.append(str);
System.out.println(str);
//maybe you repaint or something.......
}[/B]
[B] public static void main(String[] args)[/B]
{
[B] PFrame mainFrame = new PFrame();[/B]
}
}
and the listener
Code :
class ActListener implements ActionListener
{
JFileChooser myChooser = new JFileChooser();
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("browse button"))
{
Container a = new Container();
JButton b = (JButton)event.getSource(); ;
a = b.getParent();
myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int retval = myChooser.showDialog(a, "OK");
[B]PFrame corrant =(PFrame)a;
corrant.setTextBox("check if update works");[/B]
if (retval == JFileChooser.APPROVE_OPTION)
{
File myDirectory = myChooser.getSelectedFile();
}
}
}
}
a am not sure but i think the problem is that i cant get to the right instance of my PFrame
if some one could please help me i well be so happy thanks....
Re:here is the console error in the program
i forgot to say that it does compile but when in the program when it gets to
the lines in the listener that supposed to write to the jtexterea this is what
the console throws .......
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing
.JPanel cannot be cast to PFrame
at ActListener.actionPerformed(ActListener.java:25)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
ad.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
Re: help writeing to a jtextarea from a diffrent class
Currently you have the button on a JPanel, so the parent of the button is the panel. Your trying to access the PFrame object, which is the parent of the JPanel:
Code :
class ActListener implements ActionListener {
JFileChooser myChooser = new JFileChooser();
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("browse button")) {
Container a = new Container();
JButton b = (JButton) event.getSource();
[b]a = b.getParent().getParent();[/b]
myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int retval = myChooser.showDialog(a, "OK");
PFrame corrant = (PFrame) a;
corrant.setTextBox("check if update works");
if (retval == JFileChooser.APPROVE_OPTION) {
File myDirectory = myChooser.getSelectedFile();
}
}
}
}
Re: help writeing to a jtextarea from a diffrent class
You should only need to send the textBox handle of the original PFrame. Creating a new PFrame won't help you change the original one. Instead, have your PFrame class implement ActionListener, then inside of actionPerformed have this code:
Code :
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("browse button"))
{
Container a = new Container();
JFileChooser myChooser = new JFileChooser();
myChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int retval = myChooser.showDialog(this, "OK");
this.textBox.setText("check if update works");
if (retval == JFileChooser.APPROVE_OPTION)
{
File myDirectory = myChooser.getSelectedFile();
}
}
}
You're also going to want to add your current PFrame to the actionListener of the browse button, not a new ActListener
Re: help writeing to a jtextarea from a diffrent class
thanks very much for the advice that was actually my first idea but i read some where
that you should keep your listeners on a different class so you can develop them separately
well i tell you if it works thank you very much...
Re: help writeing to a jtextarea from a diffrent class
If you want your listener to be in a separate class, you need to make sure that the listener object has access to your other class's stuff. I'm not very much into GUI programming, so I'm not sure what the standard is here.
I think something like this should work (may run into a few bugs because I didn't want to come up with a whole GUI to test this on):
Code :
public class MainClass extends JFrame
{
JTextArea text;
JButton button;
public MainClass()
{
super("");
text = new JTextArea();
button = new JButton("test");
this.addComponent(text);
this.addComponent(button);
this.setVisible(true);
}
public void addActionListener(ActionListener listen)
{
button.addActionListener(listen);
}
public static void main(String[] args)
{
MainClass GUI = new MainClass();
MyListener listen = new MyListener(GUI);
GUI.addActionListener(listen);
}
}
Code :
public class MyListener implements ActionListener
{
MainClass objectHandle;
public MyListener(MainClass objectHandle)
{
this.objectHandle = objectHandle;
}
public void actionPerformed(ActionEvent event)
{
// do something
this.objectHandle.changeText("Hello!");
}
}