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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:35:21+00:00 2026-06-09T09:35:21+00:00

I have a program selection tool that i made. it opens a JFrame with

  • 0

I have a program selection tool that i made. it opens a JFrame with 17 buttons, 15 of which are customizable, and they get their text from a .txt document located in the C: drive. when i click the assign button, it opens a JFileChooser to select a file to open when the button is clicked. You then select a button to change, and then type the text you want displayed by the button. After that the program rewrites the .txt file and updates the buttons. here is the code for updating:

public static void restart() {
    start.assignButtonActions();
    start.assignButtonText();
    start.paint(graphics);
}
public void assignButtonActions() {
    /**
     * assign button actions
     */
    for (int i = 0; i < buttonAction.length; i++) {
        buttonAction[i] = io.readSpecificFromHD("C:\\ButtonActions.txt", i
                + 1 + actionButton.length);
    }
}

public void assignButtonText() {
    for (int i = 0; i < actionButton.length; i++) {
        /**
         * set button text
         */
        actionButton[i].setText(io.readSpecificFromHD(
                "C:\\ButtonActions.txt", i + 1));
    }

}
public void paint(Graphics g) {
    g.drawImage(getImage("files/background.png"), 0, 0, FRAMEWIDTH,
            FRAMEHEIGHT, null);
    refresh();
}

public void refresh() {
    graphics.drawImage(getImage("files/background.png"), 0, 0, FRAMEWIDTH,
            FRAMEHEIGHT, null);
    for (int i = 0; i < actionButton.length; i++) {
        actionButton[i].repaint();
    }
    assignButton.repaint();
    helpButton.repaint();
}

Thats all the code that is required for this question i believe. The problem is, after the method restart() is called, the background is there, with a white square around the buttons, with it being white inside the square. not really a major problem, but really incredibly annoying and pretty unprofessional. At first i thought it was that the buttons were resizing after the background is painted, so i made it so that the refresh runs twice each time its called. didnt help one bit.

EDIT:
I fixed the problem. I took hovercraft’s answer and modified what i learned a little bit. all i had to do was modify the restart() method to:

public static void restart() {
    start.assignButtonActions();
    start.assignButtonText();
    start.repaint();
}

because the repaint(); repaint the whole component which was what hovercraft said. Thank you a ton everyone! hope this helps future questions.

  • 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-09T09:35:23+00:00Added an answer on June 9, 2026 at 9:35 am

    You appear to be handling your Swing graphics incorrectly by calling paint(...) directly and trying to use a Graphics object outside of a JComponent’s paintComponent(...) method. Don’t do this, as all the Swing graphics tutorials will tell you (if you’ve not gone through some of them yet, you will want to do this soon). Instead do all graphics within a JComponent’s (such as a JPanel’s) paintComponent(...), call the super’s method first, and use the Graphics object provided by the JVM in the paintComponent’s method parameter.

    Edit
    Tutorial links:

    • The introductory tutorial is here: Lesson: Performing Custom Painting.
    • The advanced tutorial is here: Painting in AWT and Swing.

    I’m thinking that you’ll have to re-write most of your graphics code. Changes you should make:

    • Draw only in a JPanel or other JComponent-derived class, not in a JFrame or other top-level window.
    • Draw in your class’s paintComponent(...) method.
    • Place an @Override annotation just above your paintComponent(...) method to be sure that you are in fact overriding the super method.
    • Call the super’s paintComponent(...) as the first line (usually) of your paintComponent(...) override method.
    • Use the Graphics object passed into this method by the JVM.
    • Do not use a Graphics object obtained by calling getGraphics() on a component (with rare exceptions).
    • Do not give your class a Graphics field and try to store the Graphics object in it. The Graphics objects given by the JVM do not persist and will quickly become null or non-usable.
    • Do not call paint(...) or paintComponent(...) directly yourself (with rare exceptions — and your current code does not qualify as one of the exceptions, trust me).
    • You will likely not need to call repaint() on your JButtons
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Visual Basic program in Excel which opens Word documents and copies
I have a program that displays information(simple text, but not in a txt box,
I am working on a program that includes a selection of code which is
I have a program in which the user must make a selection by entering
I have a program written in Delphi-7 which opens a new Word document which
I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it
I have program, that must interact with a console program before my program can
I have program that runs fast enough. I want to see the number of
I have a program that gets a JSON from the server using getJSON and

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.