Results 1 to 4 of 4
  1. #1
    roguecowboy is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default getContentPane not displaying text

    Working through 'Learning Java' and this particular code doesn't work properly. Its meant to change the color of the text each time the button is clicked. Well, It creates the frame and the button, but no text to change colors. Any help?? Thanks.

    Java Code:
    //file: HelloJava3.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class HelloJava3
        extends JComponent
        implements MouseMotionListener, ActionListener {
        
      // Coordinates for the message
      int messageX = 125, messageY = 95;
      String theMessage;
      
      JButton theButton;
      
      // Current index into someColors
      int colorIndex;
      static Color[] someColors = {Color.black, Color.red,
          Color.green, Color.blue, Color.magenta};
          
      public HelloJava3(String message) {
        theMessage = message;
        theButton = new JButton("Change Color");
        setLayout(new FlowLayout());
        add(theButton);
        theButton.addActionListener(this);
        addMouseMotionListener(this);
      }
      
      public void paintCompoment(Graphics g) {
        g.drawString(theMessage, messageX, messageY);
      }
      
      public void mouseDragged(MouseEvent e) {
        // Save the mouse coordinates and paint the message.
        messageX = e.getX();
        messageY = e.getY();
        repaint();
      }
      
      public void mouseMoved(MouseEvent e) {}
      
      public void actionPerformed(ActionEvent e) {
        // Did somebody push our button?
        if (e.getSource() == theButton)
          changeColor();
      }
      
      synchronized private void changeColor() {
        // Change the indiex to the next color.
        if (++colorIndex == someColors.length)
          colorIndex = 0;
        setForeground(currentColor()); //Use the new color.
        repaint(); // Paint again so we can see the change.
      }
      
      synchronized private Color currentColor() {
        return someColors[colorIndex];
      }
      
      public static void main(String[] args) {
        JFrame f = new JFrame("HelloJava3");
        // Make the application exit when the window is closed.
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent we) { System.exit(0); }
        });
        f.setSize(300, 300);
        f.getContentPane().add(new HelloJava3("Hello, Java!"));
        f.setVisible(true);
      }
    }

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

    Default

    So have you narrowed down the problem?

    1) When you click the button does the ActionListener get invoked
    2) If the actionListener gets invoked does the color change
    3) does the paintComponent() method get invoked

    Add some debug statements to your code to narrow down the problem.

  3. #3
    roguecowboy is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    I'm sorry. I'm rather new to the swing utilities and whatnot. The frame is created along with the button, which is click-able. However, the text ("Hello Java!") passed as a parameter in main is not showing up in the frame. Therefore, there is no text view the color change. So essentially this is creating the frame, and the button, which does nothing.

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

    Default

    Is the painting method being executed?

    Did you add a System.out.println(...) to verify it is being called? This is basic debugging.

    As a tip, whenever you override method you should use @Override on the line above the method you are overriding to make sure you override the proper method.

Similar Threads

  1. Displaying text in a JPanel
    By DrKilljoy in forum New To Java
    Replies: 7
    Last Post: 04-15-2011, 07:28 PM
  2. Difference between add() and getContentPane().add()?
    By AcousticBruce in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 06:18 PM
  3. Replies: 1
    Last Post: 02-27-2010, 12:35 AM
  4. Displaying text in a tabel
    By pele in forum SWT / JFace
    Replies: 5
    Last Post: 05-28-2008, 07:05 AM
  5. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 10:30 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
  •