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 2 of 2

Thread: how to add a JFrame listener for a JPanel

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to add a JFrame listener for a JPanel

    I have a JFrame object in which a JPanel and a JTextArea exist. And in the JPanel, users can use mouse to draw some lines. If the user draw new things in the JPanel, the JTextArea in JFrame can update some information.

    How can make the textarea update information about the JPanel, in other words, how can JPanel build a listener to update JTextArea?



    Basic code is




    import java.awt.*;

    import javax.swing.*;
    import java.awt.event.*;

    class DrawPanel extends JPanel implements MouseListener, MouseMotionListener
    {


    DrawPanel()
    {
    setLayout(null);
    addMouseListener(this);
    addMouseMotionListener(this);
    }

    public void mousePressed(MouseEvent e)
    {
    p1 = p2 = e.getPoint();
    Graphics g = getGraphics();
    g.setColor(drawColor);
    g.setXORMode(getBackground());
    g.drawLine(p1.x, p1.y, p2.x, p2.y);

    }


    public void mouseDragged(MouseEvent e)
    {
    p3 = e.getPoint();
    paintMyComponent();
    p2 = p3;
    }

    void paintMyComponent()
    {
    Graphics g = getGraphics();
    g.setColor(drawColor);
    g.setXORMode(getBackground());
    g.drawLine(p1.x, p1.y, p2.x, p2.y);//erase
    g.drawLine(p1.x, p1.y, p3.x, p3.y); //draw
    }

    }



    class drawpic extends JFrame implements ActionListener
    {
    DrawPanel myDrawPanel;


    JTextArea tf = new JTextArea("",20,20);

    //********************************
    //When new lines are drawn in DrawPanel, the tf should should update some information about the lines accordingly.
    // QUESTIONS: HOW TO MAKE IT???? THANKS!!!!!!!!
    //********************************


    drawpic()
    {
    Container cP = getContentPane();
    cP.setLayout(new BorderLayout());

    myDrawPanel = new DrawPanel();
    myDrawPanel.setBounds(0, 50, 600, 550);
    cP.add(myDrawPanel);



    Container textbn = new Container();

    textbn.setLayout(new FlowLayout());
    tf.setEditable(false);

    cP.add(textbn, BorderLayout.SOUTH);

    textbn.add(tf);
    }


    public static void main(String [] args)
    {
    drawpic application = new drawpic();
    application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);


    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to add a JFrame listener for a JPanel

    When new lines are drawn in DrawPanel, the tf should should update
    If the DrawPanel has a reference to the textfield then its methods can use that reference to update the textfield. Pass a reference to the textfield in the DrawPanel class's constructor.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] JPanel not showing in JFrame
    By Tjstretch in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2011, 12:27 AM
  2. Dynamically add a Jpanel to a Jframe
    By Closet_Rambo in forum AWT / Java Swing
    Replies: 6
    Last Post: October 3rd, 2011, 03:51 AM
  3. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  4. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 04:12 AM