Link to home
Start Free TrialLog in
Avatar of Omer-Pitou
Omer-Pitou

asked on

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

Avatar of mccarl
mccarl
Flag of Australia image

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.
Avatar of Omer-Pitou
Omer-Pitou

ASKER

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
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?
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

User generated image
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.
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.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
Thanks for help/
You're welcome!!