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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:51:52+00:00 2026-06-07T01:51:52+00:00

I am very new to Java AWT. My question header must seem ridiculous to

  • 0

I am very new to Java AWT. My question header must seem ridiculous to you, sorry about that. In my application I have three buttons which display different threads when clicked on. Now I want to add maybe a button or checkboxes or choicelist, etc when clicked on a particular button. For eg, if I click on yes button, it should display a choice list, something like that. How do I achieve something like that? Here is my code so far:

import java.awt.Button;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class AppWindow extends Frame implements ActionListener{
    String keymsg = "Test message";
    String mousemsg = "Nothing";
    int mouseX=30, mouseY=30;
    String msg;
    public AppWindow(){
        //addKeyListener(new MyKeyAdapter(this));
        //addMouseListener(new MyMouseAdapter(this));
        addWindowListener(new MyWindowAdapter());
    }

    public void paint(Graphics g){
        g.drawString(msg, 150, 100);
    }

    //Here the window is created:

    public static void main(String args[]){
        AppWindow appwin = new AppWindow();

        appwin.setSize(new Dimension(300,200));
        appwin.setTitle("My first AWT Application");
        appwin.setLayout(new FlowLayout(FlowLayout.LEFT));
        appwin.setVisible(true);

        Button yes,no,maybe;
        yes = new Button("yes");
        no = new Button("no");
        maybe = new Button("maybe");

        appwin.add(yes);
        appwin.add(no);
        appwin.add(maybe);

        yes.addActionListener(appwin);
        no.addActionListener(appwin);
        maybe.addActionListener(appwin);


    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        // TODO Auto-generated method stub
        String str = ae.getActionCommand();
        if(str.equals("yes")){
            msg = "You pressed Yes";
        }
        if(str.equals("no")){
            msg = "You pressed No";
        }
        if(str.equals("maybe")){
            msg = "You pressed Maybe";
        }

        repaint();
    }


}


class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent we){
        System.exit(0);
    }
}
  • 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-07T01:51:53+00:00Added an answer on June 7, 2026 at 1:51 am

    Points describing what you should be doing :

    • As already mentioned by others, better to use Swing over AWT, since Swing is more advanced.
    • As much as possible, always try to Paint on top of a JPanel or a
      JComponent, instead of Painting right on top of your JFrame, by
      overriding the paintComponent(Graphics g) method of the said
      JComponent/JPanel
    • Never call setVisible(true) on the JFrame until and unless it’s
      size has been established. So in general terms, this has to be the
      last call, once you are done adding components to the JFrame and
      the size of the JFrame has been realized by the LayoutManager.
    • Inside your actionPerformed(...), instead of writing all if
      statement blocks
      , you should adhere to the if-else if statement
      blocks
      . The benefit of this, over the former is that, at any given
      time, only one event will be fired, hence once the said condition is
      satisfied, you don’t want your code to keep checking other
      conditions, which in general is really not a good programming
      practice, IMHO.
    • MOST IMPORTANT THING : Never make calls like pack()/setVisible(…) from within the main method, such calls belong
      to the Event Dispatch Thread, and must be done on the same. Please
      read Concurrency in Swing for more detail.

    Have a look at the example program, for better understanding.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ComponentExample
    {
        private CustomPanel drawingBoard;
        private JPanel contentPane;
        private JButton yesButton;
        private JButton noButton;
        private JButton maybeButton;
        private JComboBox cbox;
        private ActionListener buttonAction = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                JButton button = (JButton) ae.getSource();
    
                if (cbox.isShowing())
                        contentPane.remove(cbox);
    
                if (button == yesButton)
                {
                    drawingBoard.setText("You Pressed YES.");           
                    contentPane.add(cbox, BorderLayout.PAGE_END);               
                }
                else if (button == noButton)
                    drawingBoard.setText("You Pressed NO.");
                else if (button == maybeButton)
                    drawingBoard.setText("You Pressed MAYBE.");             
    
                /*
                 * revalidate()/repaint() is needed
                 * when the JComponent is added or
                 * removed from the already 
                 * visible Container.
                 */
                contentPane.revalidate();
                contentPane.repaint();
            }
        };
    
        public ComponentExample()
        {
            cbox = new JComboBox(
                        new String[]{"I GOT IT"
                                   , "I STILL HAD DOUBT"});
        }
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Component Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            contentPane = new JPanel();
            contentPane.setOpaque(true);
            contentPane.setBackground(Color.DARK_GRAY);
            contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(5, 5));
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.setOpaque(true);
            buttonPanel.setBackground(Color.WHITE);
    
            yesButton = new JButton("YES");
            yesButton.addActionListener(buttonAction);
            noButton = new JButton("NO");
            noButton.addActionListener(buttonAction);
            maybeButton = new JButton("MAY BE");
            maybeButton.addActionListener(buttonAction);
            buttonPanel.add(yesButton);
            buttonPanel.add(noButton);
            buttonPanel.add(maybeButton);
    
            contentPane.add(buttonPanel, BorderLayout.PAGE_START);
    
            drawingBoard = new CustomPanel();
            contentPane.add(drawingBoard, BorderLayout.CENTER);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new ComponentExample().displayGUI();
                }
            });
        }
    }
    
    class CustomPanel extends JPanel
    {
        private String msg;
    
        public CustomPanel()
        {
            msg = "";
            setOpaque(true);
            setBackground(Color.WHITE);
        }
    
        public void setText(String msg)
        {
            this.msg = msg;
            repaint();
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(300, 300));
        }
    
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawString(msg, getWidth() / 3, getHeight() / 3);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to Android development and Java. Have read around but I'm
I have a java application (that I'm wanting to bring over to Android) that
I am very new to Java, I want to add a PNG image to
I'm very new to Java but I've been developing a habit to use final
Hi i am very new to java and this is my first piece of
I am very new to Java and I was wondering if it is possible
I am very new to Java. I am writing a program to read a
Let me start by saying I'm very new to Java and to this site.
Okay, I'm very new to Java so please bear with me. I am using
I am very new to processing.org and Java. I am trying to store objects

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.