Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:57:27+00:00 2026-06-14T02:57:27+00:00

When I press the ‘draw’ button, the clock doesn’t show up. I know the

  • 0

When I press the ‘draw’ button, the clock doesn’t show up. I know the button works since calling the setText method on a textField object inside it comes through just fine. I tried various things and cannot find anything that works. I even approached my professor and he also had a tough time. But then I resized the frame and it suddenly showed up… I don’t understand why and I’m trying to figure out how to make the clock show up simply from pressing the ‘draw’ button. Any help is appreciated and thank you so much in advance!

Here are my classes in their entirety just in case you wish to run them yourself but for those preferring brevity, focus on inner class within ClockFrame class:

            import java.awt.Graphics;
            import java.awt.Graphics2D;
            import java.awt.geom.Ellipse2D;
            import java.awt.geom.Line2D;

            import javax.swing.JComponent;

            public class ClockComponent extends JComponent
            {
                final int HOURS_ON_CLOCK = 12;
                final double MINUTES_IN_HOUR = 60;
                final double HOUR_LENGTH = .4;
                final double MINUTE_LENGTH = .9;

                private double radius;
                private double posXY;
                private double hourHandLength;
                private double minuteHandLength;
                private double center;

                private Line2D.Double hourHand;
                private Line2D.Double minuteHand;

                public ClockComponent(double rad, double xy)
                {
                    radius = rad;
                    posXY = xy;

                    center = posXY + radius;

                    hourHandLength = radius * HOUR_LENGTH;
                    minuteHandLength = radius * MINUTE_LENGTH;

                    hourHand = new Line2D.Double(center, center, 
                            center, center - hourHandLength);
                    minuteHand = new Line2D.Double(center, center, 
                            center, center - minuteHandLength);
                }

                public void setClock(int hour, int minute)
                {
                    final double HOURS_TO_RADIANS = 2 * Math.PI / HOURS_ON_CLOCK;
                    final double MINUTES_TO_RADIANS = 2 * Math.PI / MINUTES_IN_HOUR;

                    hourHand.setLine(center, center, center + hourHandLength * 
                            Math.sin((hour + minute / MINUTES_IN_HOUR) * HOURS_TO_RADIANS), 
                            center - hourHandLength * 
                            Math.cos((hour + minute / MINUTES_IN_HOUR) * HOURS_TO_RADIANS));
                    minuteHand.setLine(center, center,
                            center + minuteHandLength * Math.sin(minute * MINUTES_TO_RADIANS),
                            center - minuteHandLength * Math.cos(minute * MINUTES_TO_RADIANS));
                }

                public void paintComponent(Graphics g)
                {
                    Graphics2D g2 = (Graphics2D) g;

                    double diameter = 2 * radius;

                    Ellipse2D.Double clockFace = 
                            new Ellipse2D.Double(posXY, posXY, diameter, diameter);

                    g2.draw(clockFace);
                    g2.draw(hourHand);
                    g2.draw(minuteHand);
                }
            }

ClockFrame class with the ActionListener inner class. I feel that the ActionListener inner class is where I am missing something but I can’t figure out what exactly…

            import java.awt.BorderLayout;
            import java.awt.GridLayout;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;

            import javax.swing.JButton;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JPanel;
            import javax.swing.JTextField;
            import javax.swing.border.EtchedBorder;

            public class ClockFrame extends JFrame
            {
                final int FIELD_SIZE = 10;
                final int FRAME_WIDTH = 450;
                final int FRAME_HEIGHT = 450;
                final int FIELD_WIDTH = 5;

                private JPanel frameFace;
                private JPanel fieldPanel;
                private JPanel drawingPanel;

                private JLabel hourLabel;
                private JLabel minuteLabel;

                private JTextField hourField;
                private JTextField minuteField;

                private JButton drawButton;

                private ClockComponent clock;

                public ClockFrame(double rad, double xy)
                {
                    setTitle("Clock viewer");
                    setSize(FRAME_WIDTH, FRAME_HEIGHT);

                    frameFace = new JPanel(new BorderLayout());
                    fieldPanel = new JPanel();
                    drawingPanel = new JPanel();
                    drawingPanel.setBorder(new EtchedBorder());

                    hourLabel = new JLabel();
                    hourLabel.setText("Hour ");

                    minuteLabel = new JLabel();
                    minuteLabel.setText("Minute ");

                    hourField = new JTextField(FIELD_SIZE);
                    minuteField = new JTextField(FIELD_SIZE);

                    drawButton = new JButton("Draw");
                    drawingPanel.setLayout(new GridLayout(1, 1));

                    clock = new ClockComponent(rad, xy);

                    fieldPanel.add(hourLabel);
                    fieldPanel.add(hourField);
                    fieldPanel.add(minuteLabel);
                    fieldPanel.add(minuteField);
                    fieldPanel.add(drawButton);

                    ActionListener clockListener = new DrawListener();
                    drawButton.addActionListener(clockListener);

                    frameFace.add(fieldPanel, BorderLayout.NORTH);
                    frameFace.add(drawingPanel, BorderLayout.CENTER);

                    add(frameFace);
                }

                class DrawListener implements ActionListener
                {
                    public void actionPerformed(ActionEvent event)
                    {
                        int hour = Integer.parseInt(hourField.getText());
                        int minute = Integer.parseInt(minuteField.getText());

                        clock.setClock(hour, minute);
                        drawingPanel.add(clock);
                    }
                }
            }

And a main method

            import javax.swing.JFrame;

            public class ClockViewer
            {
                public static void main(String[] args)
                {
                    JFrame frame = new ClockFrame(75, 100);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:57:29+00:00Added an answer on June 14, 2026 at 2:57 am

    Whenever you add a component to a container or remove one from the container after the GUI has been realized, you need to tell that container to re-lay out its components and re-draw them. This is done by calling revalidate() and then repaint() on the container, here the drawingPanel:

    clock.setClock(hour, minute);
    drawingPanel.add(clock);
    drawingPanel.revalidate();
    drawingPanel.repaint();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I press CTRL + Space the auto completing not working. It doesn't show
I press the button to connect to the server (TCP), but i don't know
When I press the home button it doesn't go back like I think it
When i press keyboard down button the textfield is added to sprite but not
I press login button to login into my app and I'm using asyncTask for
When i press a button in my app, I need to return to the
What happens when I press the Run Garbage Collector button in Eclipse? Does it
I am trying to press a button that takes the text from textbox4 and
When I press the button onces, I want to start count, and when I
When I press the Save button I return from my Controller this: return Json(new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.