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 8841061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:37:04+00:00 2026-06-14T10:37:04+00:00

I am currently doing the CS106A Stanford Java course for beginners. I am stuck

  • 0

I am currently doing the CS106A Stanford Java course for beginners. I am stuck at the handout number 7, requiring me to create a simple program that draws GRects with a GLabel on them on the canvas and then allows me to drag them around, remove them again or clear the whole canvas. In order to add such a box, I have added a JTextField in the SOUTH to enter the name of the box, and ADD / REMOVE / CLEAR buttons.

Entering a name into the textfield to add a box works. My problem is that the text I type into the JTextField, though recorded (since it shows up on the new box) is not displayed in the JTextField itself, so I do not see what I typed until I hit “ADD” and I read it on the box itself.

Here’s my code:

    package handout07Interactors;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.*;

import acm.graphics.*;
import acm.program.*;

@SuppressWarnings("serial")
public class Box_Diagram extends GraphicsProgram{

    public void init() {
        displayButtons();
        addActionListeners();

        //TODO make draggable

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equals("ADD")) {

            //create box
            GCompound canvasBox = createBox(tf.getText());

            //add box to HashMap
            map.put(tf.getText(), canvasBox);

            //add box to canvas
            int x = (int) (getWidth() - canvasBox.getWidth()) / 2;
            int y = (int) (getHeight() - canvasBox.getHeight()) / 2;
            add(canvasBox, x, y);

        } else if (e.getActionCommand().equals("REMOVE")) {

            //remove box with name
            if (map.get(tf.getText()) != null) {remove(map.get(tf.getText()));}; //if box exists, remove it 

        } else {

            for( String name: map.keySet() )
            {
                remove(map.get(name));
            }



        }

    }

    private GCompound createBox(String text) {

        // GCompound
        GCompound box = new GCompound();

        // create GRect
        GRect rect = new GRect(BOX_WIDTH, BOX_HEIGHT);
        box.add(rect);

        // add GLabel
        GLabel label = new GLabel(text);
        int x = (int) (rect.getWidth() - label.getWidth()) / 2;
        int y = 30; //manual entry, somehow calculation didn't work as it does for width
        box.add(label, x, y);

        map.put(text, box);

        return box;

    }

    private void displayButtons() {

        //label
        add(new JLabel("Name:"), SOUTH);

        //textfield
        tf = new JTextField(30);
        tf.addActionListener(this);
        add(tf, SOUTH);

        //ADD REMOVE CLEAR
        add(new JButton("ADD"), SOUTH);
        add(new JButton("REMOVE"), SOUTH);
        add(new JButton("CLEAR"), SOUTH);

    }

    //IVARS
    private JTextField tf;
    public static final int BOX_WIDTH = 100;
    public static final int BOX_HEIGHT = 50;
    public HashMap<String, GCompound> map = new HashMap<String, GCompound>();



}
  • 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-14T10:37:05+00:00Added an answer on June 14, 2026 at 10:37 am

    You’re adding 5 different components (the label, text field, and the 3 buttons) to the same place in the layout (the SOUTH section of your border layout).

    You should add these 5 components to another JPanel, using a FlowLayout for example, and then add this panel to the SOUTH of your main panel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently doing a console app that has to send scheduled emails. In one
I am currently doing a C# WPF application that generates a table that does
I'm currently doing the calculator app that has the graph functionality. So then, I
I am currently doing an android application that contains customize alert dialog. It contains
I'm currently doing an experimental page for college and I came upon a simple
I'm currently doing some oracle sql analysis and often encounter valid sql statement that
Currently doing a group project for college in Java. The assignment is to produce
I am currently doing this as follows: // _Container is the panel that the
I am currently doing some cleanup of Java projects which use Maven, and use
I am currently doing a git svn clone from an svn repository that has

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.