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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:41:53+00:00 2026-05-17T20:41:53+00:00

For a school project, I need to make a simple paint application that can

  • 0

For a school project, I need to make a simple paint application that can draw lines, ovals, and rectangles.

The assignment specifies that I need toolbar buttons and menu items for each type of shape.

I would like to go a little above and beyond, by making the buttons JToggleButtons in the toolbar and the menu items JRadioButtonMenuItems. Furthermore, I want it so that when you select one of the toolbar buttons, it deselects the other ones, selects the appropriate menu item, and deselects the other menu items. Same for selecting one of the menu items.

I know I can group any AbstractButton with a ButtonGroup, but I am not sure if this is the right way to go, because though it handles one “group” of buttons just fine, I am not sure it can handle two parallel groups.

Doing it without ButtonGroup would mean in each of the 6 event listeners I would have to manually deselect the other buttons, and each pair would call the same code to set the shape type.

I could also make two ButtonGroups, one for the menu, one for the toolbar, but then I also have to duplicate shape type setting code.

In either situation, I also run the risk of the menu setting a button which sets a menu item which sets a button, ad infintum.

What is the best way to tackle this problem?

(Bonus points for being able to solve the problem with the Netbeans GUI designer; It’s just easier)

  • 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-05-17T20:41:53+00:00Added an answer on May 17, 2026 at 8:41 pm

    The Action interface is an effective approach “if you have two or more components that perform the same function,” as discussed in How to Use Actions. In particular, an Action would allow your buttons and menu items to use the same code.

    Addendum: The example below shows how a JMenu and a JToolBar can share the same Action for each of several files.

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.io.File;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JToolBar;
    
    /** @see http://stackoverflow.com/questions/4038605 */
    public class FileMenu {
    
        public static void main(String[] args) {
    
            EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new FileMenu().create();
                }
            });
        }
    
        void create() {
            File userDir = new File(System.getProperty("user.dir"));
            File[] files = userDir.listFiles();
    
            JMenu menu = new JMenu("Recent Files");
            JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
            JLabel label = new JLabel(" ", JLabel.CENTER);
            for (File f : files) {
                if (f.isFile() && !f.isHidden()) {
                    RecentFile rf = new RecentFile(f, label);
                    menu.add(new JMenuItem(rf.getAction()));
                    toolBar.add(rf.getAction());
                }
            }
            JMenuBar menuBar = new JMenuBar();
            menuBar.add(menu);
    
            JFrame f = new JFrame("FileMenu");
            f.setJMenuBar(menuBar);
            f.add(toolBar, BorderLayout.CENTER);
            f.add(label, BorderLayout.SOUTH);
            f.pack();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    class RecentFile extends AbstractAction {
    
        private final File file;
        private final JLabel label;
    
        public RecentFile(final File file, final JLabel label) {
            this.file = file;
            this.label = label;
            this.putValue(Action.NAME, file.getName());
            this.putValue(Action.SHORT_DESCRIPTION, file.getAbsolutePath());
        }
    
        public void actionPerformed(ActionEvent e) {
            label.setText(file.getName());
    
        }
    
        public Action getAction() {
            return this;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a school project, I need to create a way to create personnalized queries
I'm working on a project for school, and I'm implementing a tool which can
For a school assignment I have to write x86 assembly code, except I can't
For our school project, we are tasked to define a design document describing the
I'm working on a school project and I'm getting some weird errors from Xcode.
I am trying to use Qt for a project in school but am running
Up till now I have been developing my personal and school projects at home
At school we have been programming in MIPS assembly language for some time. I'm
For my school work, I do a lot of switching computers (from labs to
I went to school for programming years ago and when I got out I

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.