Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Wnt2bsleepin

Thread: Action Listener and repaint

  1. #1
    jazzgnat is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Action Listener and repaint

    I'm working on this program for weight conversions for school. I've employed the action listener but my repaint method isn't displaying the new information. Also I think it may not be working at all because I changed the color in repaint but it doesn't change. Any help finding the source of this problem would be helpful.

    here's the code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.geom.*;
    import java.awt.event.*;
    public class StringTest extends JFrame implements ActionListener
    {
    int x = 0;
    int y = 150;
    double lbs = 0;
    String[] weights = new String[4];
    String[] unit = {"pounds","ounces","kilograms","metric tons"};
    double[] weight = {lbs*1,lbs*16,lbs/2.204623,lbs/2204.623};
    Font comic = new Font("Comic Sans", Font.ITALIC, 20);
    JLabel label = new JLabel("Enter your weight in pounds");
    JTextField field = new JTextField(10);
    JButton button = new JButton("Submit");

    public StringTest()
    {
    super("Weight Conversion");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    add(label);
    add(field);
    add(button);
    field.addActionListener(this);
    button.addActionListener(this);
    for(x = 0; x<weights.length; ++x)
    {
    weights[x] = "your weight in "+unit[x]+" is: "+weight[x];
    }

    }


    public void actionPerformed(ActionEvent e)
    {
    String answer = field.getText();
    lbs = Double.valueOf(answer);
    repaint();
    }

    public void paint(Graphics g)
    {
    super.paint(g);
    Graphics2D g2D = (Graphics2D)g;
    g2D.setFont(comic);
    g2D.setColor(Color.RED);
    y = 150;
    for(x = 0; x<weights.length; ++x)
    {
    g2D.drawString(weights[x], 50, y);
    y += 30;
    }

    }

    public void repaint(Graphics gr)
    {
    Graphics2D gr2D = (Graphics2D)gr;
    gr2D.setFont(comic);
    gr2D.setColor(Color.BLUE);
    for(x=0;x<weights.length; ++x);
    {
    gr2D.drawString(weights[x], 50, y);
    }
    }

    public static void main(String[] args)
    {
    StringTest frame = new StringTest();
    frame.setSize(400,400);
    frame.setVisible(true);
    }
    }

    p.s. I imported the geom package because I intended to do borders around the conversions but getting the conversions working is more important right now.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Action Listener and repaint

    Ok, I don't think you're supposed to make a custom repaint(), although you can. I got rid of it and got the text to turn from blue to black when the button it pressed. Also, please use tags when posting, it makes it easier to read.
    Fubarable likes this.

  3. #3
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Action Listener and repaint

    Also, you are trying to update the contents of the weight array by dividing it by the value of lbs. This doesn't work because when the array is created, lbs is equal to zero. You need to find out how to remedy that

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Action Listener and repaint

    What are you trying to achieve by creating a repaint(Graphics) overload that's never called?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jazzgnat is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Action Listener and repaint

    Thanks for your help guys! I moved the assignment for the weight and weights[] into the action listener. Everything is working now.

Similar Threads

  1. Can anyone help me with an action listener?
    By mdCollins1 in forum New To Java
    Replies: 5
    Last Post: 03-21-2012, 04:07 AM
  2. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  3. Help with Combo box and action listener
    By sunilmenhdiratta in forum AWT / Swing
    Replies: 2
    Last Post: 06-25-2011, 08:32 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM

Tags for this Thread

Posting Permissions

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