Java - Custom Toolbar & Buttons Listeners Implementation

Hi,
First, thanks to the Java experts who assisted me so far.
Here is my problem (I might be over thinking).
I have a custom toolbar (holding number of buttons), and that custom toolbar will be identical on all the forms (that I call GridForms).
But individual listeners to implement need to be done in individual Grid Forms (Parent form).
When I place the custom toolbar on the Grid Forms, i am not seeing the individual buttons appearing in the navigator, and I can only see them in the toolbar. In the navigator, I only see a single line (CustomToolBar1) without children (buttons). This makes difficult for me to implement Listeners and related handlers.
Any idea, how I can fix this. Here is my CustomToolBar class
public class CustomToolBar extends JToolBar {
    public CustomToolBar() {
        setRollover(true);
        setFloatable(true);
        setBorderPainted(true);
        setOrientation(HORIZONTAL);
        
        JButton btnNew = new JButton(NEW_RECORD_C, new ImageIcon(getClass().getResource("/myImages/newRecord.png")));
        btnNew.setHorizontalTextPosition(CENTER);
        btnNew.setVerticalTextPosition(BOTTOM);
        btnNew.setFocusPainted(false);
        
        JButton btnEdit = new JButton(EDIT_RECORD_C, new ImageIcon(getClass().getResource("/myImages/editRecord.png")));
        btnEdit.setHorizontalTextPosition(CENTER);
        btnEdit.setVerticalTextPosition(BOTTOM);
        btnEdit.setFocusPainted(false);
        //btnEdit.setPreferredSize(null);
              
        JButton btnCopy = new JButton(COPY_RECORD_C, new ImageIcon(getClass().getResource("/myImages/copyRecord.png")));
        btnCopy.setHorizontalTextPosition(CENTER);
        btnCopy.setVerticalTextPosition(BOTTOM);
        btnCopy.setFocusPainted(false);
        
        JButton btnDelete = new JButton(DELETE_RECORD_C, new ImageIcon(getClass().getResource("/myImages/deleteRecord.png")));
        btnDelete.setHorizontalTextPosition(CENTER);
        btnDelete.setVerticalTextPosition(BOTTOM);
        btnDelete.setFocusPainted(false);
             
        JButton btnSearch = new JButton(SEARCH_RECORD_C, new ImageIcon(getClass().getResource("/myImages/findRecord.png")));
        btnSearch.setHorizontalTextPosition(CENTER);
        btnSearch.setVerticalTextPosition(BOTTOM);
        btnSearch.setFocusPainted(false);
        
        JButton btnImport = new JButton(IMPORT_RECORD_C, new ImageIcon(getClass().getResource("/myImages/importRecord.png")));
        btnImport.setHorizontalTextPosition(CENTER);
        btnImport.setVerticalTextPosition(BOTTOM);
        btnImport.setFocusPainted(false);
    
        add(btnNew);
        add(btnEdit);
        add(btnCopy);
        add(btnDelete);
        add(btnImport);
        addSeparator();
        add(btnSearch);
        //Filter
        //Refresh
        //Export
        //Print
    }
    
}

Open in new window

Omer-PitouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
I'm not sure what you mean by "navigator" and I can't deduce this from your code either. Maybe you could post a screenshot of what you have and more description about what it *should* show.
Omer-PitouAuthor Commented:
The navigator is the left pane that appears when you have a frame opened in design mode. It lists all the components on that frame. I am using netbeans. It is like an inspector of components.
In one word, this is my problem.
- The code I posted is a custom toolbar class holding number of buttons, and will be placed on multiple windows.
- How can I implement listener on those buttons encapsulated inside a toolbar class. The listener needs to be implemented on individual forms (windows),
To figure out my problem, place my custom toolbar class on a frame and try to implement a listener along with an handler.

Regards
krakatoaCommented:
I'm not quite sure either what you are getting at - but couple of things . . . what is stopping you implementing ActionListener? You can then implement the actionPerformed() method in the JFrame, and you can determine the source (the JButton each time), but interrogating the Event param

((JButton)act.getSource()).getLabel()

(where 'act' is the ActionEvent)

Is that anything like what you are aiming for?
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Omer-PitouAuthor Commented:
Hi,
I have attached a picture to illustrate that custom toolbar (with buttons embedded as you have seen in the previous code I posted).
From the navigator (Left pane), look at the line JInternalFrame1, you will see that I have only one item there which is the custom toolbar itself, but its children do not appear,while you can see them on the right pane.
Handling click events on individual becomes difficult.
The code you provided can be put in the custom tool bar class, but the problem is they need to be different for every form that will hold this custom tool bar.
An alternative to this, is to remove buttons inside the custom tool bar , and add them visually on the frame, once the  custom tool bar is place on it. From there I can define listeners for these buttons.
If the first option is doable, I will go for that.

Regards

Capture.PNG
krakatoaCommented:
I think my help is going to be quite limited on this, but for our general benefit here it might be useful to tell us what the class objects are that you are using for this GUI (the Nav etc), and whether you expect to use one set of buttons and listeners which change their behaviour according to the choices in the navigator, or whether you want several toolbars, each with its own listeners. Posting some key code from your codebase would also be helpful, especially for other experts here like mccarl, who I feel pretty sure will have insights from that code.
Omer-PitouAuthor Commented:
Let's sum it up.
- 1 custom toolbar holding New, Edit, Delete, Copy Record buttons (See the custom class I posted before)
- This custom toolbar will be placed on different individual frames. i.e. debtorsMaintenance Frame, articlesMaintenance Frame
- Each frame displays a table (Grid) with the custom tool bar on top
- For example on debtorsMaintenance Frame, I need to set up an Action listener whose handler code will be pointing to the debtors table, and on articlesMaintenance an Action Listener whose handler code will be pointing to the articles table. Two totally different handler codes.
- The only thing they have in common is the toolBar structure.
- The problem is how to set up those listeners on individual frames while related buttons are embedded into a custom tool bar.
- To simulate this, open up a JFrame, drag the custom tool bar on it (ignore the icon), and try to implement Action listeners from a frame you re working on.

Regards.
krakatoaCommented:
Do you implement any snap-to-grid actions for your gui? Otherwise if it's a matter of dragging the 'bar, you will be able to use getLocation() to discover its whereabouts, upon which you can make some conditional statements that will action the listeners I would have thought.
Omer-PitouAuthor Commented:
Thanks for your prompt reply. Can you provide a sample of code.
Yes I am dragging the custom bar. with getLocation(), I am not seeing how I am going to access individual buttons embedded inside.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I think this is just a Netbeans thing, and how to make it recognize that there are subcomponents contained in the parent custom component. If your current code is not giving you the option, it obviously ins't looking at the actual component hierarchy to determine this. Perhaps you need to implement your toolbar more like a JavaBean, ie. with getters, so that Netbeans knows about the buttons contained in the toolbar and can easily set the action listeners on them.

Try something like this and see if the Netbeans GUI Builder now lets you attache listeners to the buttons...

public class CustomToolBar extends JToolBar {

    private JButton btnNew;
    private JButton btnEdit;
    private JButton btnCopy;
    private JButton btnDelete;
    private JButton btnSearch;
    private JButton btnImport;

    public CustomToolBar() {

        setRollover(true);
        setFloatable(true);
        setBorderPainted(true);
        setOrientation(HORIZONTAL);
        
        btnNew = new JButton(NEW_RECORD_C, new ImageIcon(getClass().getResource("/myImages/newRecord.png")));
        btnNew.setHorizontalTextPosition(CENTER);
        btnNew.setVerticalTextPosition(BOTTOM);
        btnNew.setFocusPainted(false);
        
        btnEdit = new JButton(EDIT_RECORD_C, new ImageIcon(getClass().getResource("/myImages/editRecord.png")));
        btnEdit.setHorizontalTextPosition(CENTER);
        btnEdit.setVerticalTextPosition(BOTTOM);
        btnEdit.setFocusPainted(false);
        //btnEdit.setPreferredSize(null);
              
        btnCopy = new JButton(COPY_RECORD_C, new ImageIcon(getClass().getResource("/myImages/copyRecord.png")));
        btnCopy.setHorizontalTextPosition(CENTER);
        btnCopy.setVerticalTextPosition(BOTTOM);
        btnCopy.setFocusPainted(false);
        
        btnDelete = new JButton(DELETE_RECORD_C, new ImageIcon(getClass().getResource("/myImages/deleteRecord.png")));
        btnDelete.setHorizontalTextPosition(CENTER);
        btnDelete.setVerticalTextPosition(BOTTOM);
        btnDelete.setFocusPainted(false);
             
        btnSearch = new JButton(SEARCH_RECORD_C, new ImageIcon(getClass().getResource("/myImages/findRecord.png")));
        btnSearch.setHorizontalTextPosition(CENTER);
        btnSearch.setVerticalTextPosition(BOTTOM);
        btnSearch.setFocusPainted(false);
        
        btnImport = new JButton(IMPORT_RECORD_C, new ImageIcon(getClass().getResource("/myImages/importRecord.png")));
        btnImport.setHorizontalTextPosition(CENTER);
        btnImport.setVerticalTextPosition(BOTTOM);
        btnImport.setFocusPainted(false);
    
        add(btnNew);
        add(btnEdit);
        add(btnCopy);
        add(btnDelete);
        add(btnImport);
        addSeparator();
        add(btnSearch);
        //Filter
        //Refresh
        //Export
        //Print
    }
    
    public JButton getBtnNew() {
        return btnNew;
    }

    public JButton getBtnEdit() {
        return btnEdit;
    }

    public JButton getBtnCopy() {
        return btnCopy;
    }

    public JButton getBtnDelete() {
        return btnDelete;
    }

    public JButton getBtnSearch() {
        return btnSearch;
    }

    public JButton getBtnImport() {
        return btnImport;
    }

}

Open in new window


(NB. The above code was done by hand so it may have errors, hopefully it gives you the idea though)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Omer-PitouAuthor Commented:
Still no hierarchy from Netbeans.
This works.... Thank a lot. I can now programmatically set up the  Listener this way
customToolBar1.getBtnNew().addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                   JOptionPane.showMessageDialog(null,"NEW");
                }
            });


Do you think Eclipse will show the hierarchy.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Eclipse doesn't even have a GUI editor anyway (at least not standard, and I don't know of any plugins).

Unfortunately I can't really help further because I don't have Netbeans installed but at least you are able to do it programmatically
Omer-PitouAuthor Commented:
Thanks for help/
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.