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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:42:31+00:00 2026-06-13T12:42:31+00:00

I have posted my code below, but essentially I’m having a little trouble. I

  • 0

I have posted my code below, but essentially I’m having a little trouble.
I am trying to create a program which will take 3 inputs of text, these being red, green and blue.
The idea is that the text starts as red, and when a change colour button is pressed. The RGB values entered will be taken and the colour will be changed based on the values.
However I am having trouble getting the values entered into the text field to be taken by the program and change the colour. Any help is appreciated.

I was also having problem with getting both the text and colour values to change together in the handler when they were edited manually in the code. It would either just change the colour, or just change the text.

Any help will be greatly appreciated. 😀

package RGBProgram;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RGB extends JApplet {
    Color col = new Color(255, 0, 0);
    String str = "Hello";
    JButton butReset, butChange;
    JTextField textR, textG, textB;

    public void init(){
        butReset = new JButton("Reset");
        butChange = new JButton("Change Colour");
        textR = new JTextField("Red", 10);
        textG = new JTextField("Green", 10);
        textB = new JTextField("Blue", 10);

        RGBPanel panel = new RGBPanel(this);
        JPanel butPanel = new JPanel();
        JPanel textPanel = new JPanel();
        butPanel.add(butReset);
        butPanel.add(butChange);
        textPanel.add(textR);
        textPanel.add(textG);
        textPanel.add(textB);
        add(panel, BorderLayout.CENTER);
        add(butReset, BorderLayout.NORTH);
        add(butChange, BorderLayout.SOUTH);
        add(textPanel, BorderLayout.WEST);

        Handler reset = new Handler(this);
        Handler change = new Handler(this);

        textR.addActionListener (new Handler(this));
        textG.addActionListener (new Handler(this));
        textB.addActionListener (new Handler(this));
        butReset.addActionListener(reset);
        butChange.addActionListener(change);

    }

    class RGBPanel extends JPanel{
        RGB theApplet;

            RGBPanel(RGB app){
                theApplet = app;
            }

        public void paintComponent(Graphics g)
        {super.paintComponent(g);
        Color cols = col;
        String str1 = str;
        g.setColor(cols);
        g.drawString(str1, 0, 150);
        }
    }
}

package RGBProgram;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Handler implements ActionListener {
     RGB theApplet;
    Handler(RGB app){
        theApplet = app;
    }

    public void actionPerformed(ActionEvent e){
        String red = theApplet.textR.getText();
        String green = theApplet.textG.getText();
        String blue = theApplet.textB.getText();
        theApplet.textR.setText("");
        theApplet.textG.setText("");
        theApplet.textB.setText("");

        try{
            int r = Integer.parseInt(red.trim());
            int g = Integer.parseInt(green.trim());
            int b = Integer.parseInt(blue.trim());

        }
        catch (NumberFormatException ex){

        }

        if (e.getSource() == theApplet.butChange)
            theApplet.str = "Goodbye";
            theApplet.col = new Color(r, g, b);
        if (e.getSource() == theApplet.butReset)
            theApplet.str = "Hello";
            theApplet.col = new Color(255, 0, 0);
    theApplet.repaint();
    }

}
  • 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-13T12:42:32+00:00Added an answer on June 13, 2026 at 12:42 pm

    I changed the actionPerformed method in the Handler class to be as below and the color is now applied correctly:

    public void actionPerformed(ActionEvent e) {
        String red = theApplet.textR.getText();
        String green = theApplet.textG.getText();
        String blue = theApplet.textB.getText();
        theApplet.textR.setText("");
        theApplet.textG.setText("");
        theApplet.textB.setText("");
    
        try {
            int r = Integer.parseInt(red.trim());
            int g = Integer.parseInt(green.trim());
            int b = Integer.parseInt(blue.trim());
            if (e.getSource() == theApplet.butChange)
                theApplet.str = "Goodbye";
            theApplet.col = new Color(r, g, b);
            if (e.getSource() == theApplet.butReset)
                theApplet.str = "Hello";
            theApplet.repaint();
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a plot (sample code pasted below) that I am trying to add
So I just have posted a question about this code (which was answered): $(document).ready(Main);
UPDATE: To help clarify what I'm asking I have posted a little java code
I'm fairly new to ggplot. I have made a bumpplot using code posted below.
I have a problem with my code posted below, set and get methods. I
Note there's a lot of code posted below but its mostly all the same,
Ok so the title may have been confusing so i have posted 2 code
I have posted an example below and the desired result . I saw a
I have posted in here before but all have led to dead ends. First
I am creating a dynamic array inside a function. The code (posted below) runs

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.