Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: help writeing to a jtextarea from a diffrent class

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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:
    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
    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....
    Last edited by mdstrauss; October 5th, 2009 at 03:45 PM. Reason: spelling


  2. #2
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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)

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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:

    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();
                }
     
            }
        }
    }

  4. The Following User Says Thank You to Trey For This Useful Post:

    mdstrauss (October 6th, 2009)

  5. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: does not work

    still same thing but great advice thanks anyway...
    other advice???

    i think i well send the frame from PFrame class or put the listener under the class so i can yous <.this>
    any other ideas well be great..

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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
    Last edited by helloworld922; October 6th, 2009 at 12:04 AM.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    mdstrauss (October 6th, 2009)

  8. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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...

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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):

    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);
         }
    }

    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!");
         }
    }

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM
  3. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 1st, 2009, 05:13 AM
  4. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2009, 12:48 PM
  5. Replies: 1
    Last Post: May 21st, 2009, 03:41 AM