Results 1 to 8 of 8
  1. #1
    theen3my is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Question How to update JTextArea with JSlider Values

    I am trying to write a program that displays a circle in a of a given size in a JPanel and displays the circle's area, radius, diameter, and circumference in a JTextArea.

    The user will be able to alter the radius with a JSlider. As the radius changes, the diameter, area, and circumference should be updated and displayed. Initial radius is set at 150.

    I am able to draw the initial circle and able to manipulate the size in real time as I move the JSlider.

    However, I am having difficulty getting the text in the JTextArea to update after the JSlider alters the dimensions of the circle.

    Any clues to what I am missing? Go easy, I'm a n00b.

    Here is the main class code


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

    public class HW2 extends JFrame
    {
    private JSlider radiusSlider; // Slider to select a Radius.
    private Homework2 work; // Panel to draw Circle.
    private JTextArea textArea;
    private int radiusX;
    private double diameter;
    private double area;
    private double circum;

    public HW2()
    {
    super( "HW2 Circle - INSY 4305 " );
    work = new Homework2(); // Creates Panel to draw circle.
    setSize( 500, 600 );
    setVisible( true );


    //Setting up JSlider to control radius value.

    radiusSlider = new JSlider( SwingConstants.HORIZONTAL, 100, 200, 150 );
    radiusSlider.setMajorTickSpacing( 10 );
    radiusSlider.setPaintTicks( true );
    radiusSlider.setPaintLabels( true );

    radiusX= work.radius;
    diameter = ( 2 * radiusX );
    area = ( Math.PI )*( radiusX ) * ( radiusX );
    circum = 2 * ( Math.PI ) * ( radiusX );




    //Registers Jslider Event Listener.

    radiusSlider.addChangeListener(
    new ChangeListener()
    {


    public void stateChanged( ChangeEvent e )
    {

    work.setRadius ( radiusSlider.getValue() );




    }
    }
    ); // End of Change Listener.

    // JText Area to display the Radius,Area,Diameter and Circumference.

    JTextArea textArea = new JTextArea( "Radius: "+ radiusSlider.getValue() + "\nDiameter: " + diameter + "\nArea: " + area + "\nCircumference: " + circum );
    textArea.setEditable( false );

    Container container= getContentPane();
    container.add( work, BorderLayout.NORTH );
    container.add( textArea, BorderLayout.CENTER );
    container.add( radiusSlider, BorderLayout.SOUTH );
    setSize( 500, 600 );
    setVisible( true );
    }

    public static void main(String args[])
    {
    HW2 application = new HW2();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );



    }

    }





    JPanel subclass code:

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

    public class Homework2 extends JPanel
    {
    int radius =150;

    public void paintComponent( Graphics g )
    {
    super.paintComponent( g );
    g.drawOval( 150, 150, radius, radius );
    } // paint Method Ends.

    public void setRadius( int newRadius )
    {
    radius=( newRadius>=0? newRadius : 150 );
    repaint();
    }

    public Dimension getPreferredSize()
    {
    return new Dimension( 400, 400 );
    }

    public Dimension getMinimumSize()
    {
    return getPreferredSize();
    }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Why is it that newbies to forums can always figure out how to use the "Bold" or "Color" tags when making a posting, but can never figure out how to use the "Code" tags to that the code retains its original formatting and is therefore easier to read?

    I don't see where you use textArea.setText() in the code where the slider event is handled.

  3. #3
    theen3my is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default Bleh

    Quote Originally Posted by camickr View Post
    Why is it that newbies to forums can always figure out how to use the "Bold" or "Color" tags when making a posting, but can never figure out how to use the "Code" tags to that the code retains its original formatting and is therefore easier to read?

    I don't see where you use textArea.setText() in the code where the slider event is handled.
    Why is it that the people who take joy in flaming those seeking help are always the ones asking stupid questions?

    Perhaps the reason I didn't notice the "code" tags is because I this may be my first venture into a PROGRAMMING forum. However, this isn't my first forum I am familiar with the standard "bold" and "color" tags.

    Since I was in a hurry, I only read through the Rules FAQ before posting. Thank you for the tip on the "code" tags, as I will use it in the future. Again, this is my first programming class and the first time I have used this type of forum.

    Thank you for the legitimate answer, but please save the "call out" for someone who actually deserves it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by theen3my View Post
    Thank you for legitimate answer, but please save the "call out" for someone who actually deserves it.
    His "call out" likely was posted in order to help you. There may be a reason why his was the only answer you've received despite having posted your question 12 hours ago. I'm betting that if you had used code tags, you'd have gotten more answers and quicker too.

    But this is a minor issue. I do wish you much luck in solving your problem. If his answer has helped you solve it, please mark this thread "Solved". If not, please post your most recent code revision with tags. Again, much luck.

    /Pete
    Last edited by Fubarable; 10-03-2009 at 01:02 AM.

  5. #5
    theen3my is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    Yes. His snide question that started his response was purely for my benefit.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    However, this isn't my first forum I am familiar with the standard "bold" and "color" tags.
    So, you are not familiar with "Preview" buttons that are available on most forums either? People use the preview to make sure the question is presented properly after all the markup is done the way it was expected. I know I would have questioned why the code looked so ugly.

    Since I was in a hurry
    Maybe you don't understand forums. The point is to make it easy for us to answer questions, after all you are the one asking for help. The fact that you where is a hurry, is not relevant to us. Take the time to post a proper question and you will get a proper answser.


    His "call out" likely was posted in order to help you.
    Always, otherwise the behaviour won't change.

    I would just like to note, as you mentioned, that the OP has not marked the question as answered (or provided a follow up question). Part of my rant was in the hope that the OP would be proactive and learn how to use the forum the way it was intended. Apparently not as he needed to be prompted again.

    Sigh...

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by theen3my View Post
    ... Again, this is my first programming class and the first time I have used this type of forum...
    What a blunder! New to swing and you disrespect camickr?
    P.S ignoreList++;

  8. #8
    theen3my is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Jslider changeListner
    By Basit56 in forum Java Applets
    Replies: 1
    Last Post: 09-14-2009, 05:04 PM
  2. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 02:48 PM
  3. Got Trouble with JSlider
    By hungleon88 in forum Advanced Java
    Replies: 6
    Last Post: 08-30-2008, 06:02 PM
  4. jmf & JSlider
    By nathanr_kamal in forum AWT / Swing
    Replies: 6
    Last Post: 06-28-2008, 08:56 AM
  5. unable to update MYSQL with values from jsp page
    By koushika in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2008, 02:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •