How can I add a JTextField to a JFrame's MenuBar?
Asked Answered
F

1

10

I've been trying to overload JMenu and put in some custom code to support a JTextField but that isn't going well. My main purpose here is to add a search field to the right of my menu items. So I have something like File, Edit, help on the left and then on the right would be the search bar, almost like how there is a google search bar in some browsers. Does anyone have an idea how I could go about adding this functionality?

Famous answered 14/11, 2011 at 19:2 Comment(2)
Why isn't it going well? What specifically seems to be troubling you?Decembrist
@Decembrist I've run into a dead end where I'm trying to add the text box instead of a button. JMenuItem inherits from AbstractButton so I don't know how to add a text box instead of just changing the text on an AbstractButton. I also don't believe this is the best way to add the functionality I'm talking about because it seems I may have to change around a bunch of stuff deep within JMenu. I guess I'm looking for guidance on a better way to do this then what I'm currently trying to do.Famous
S
24

I never see that as JMenuItem, I think that alyways placed in JMenuBar

enter image description here

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

public class MenuGlueDemo {

    public MenuGlueDemo() {
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(createMenu("Menu 1"));
        menuBar.add(createMenu("Menu 2"));
        menuBar.add(createMenu("Menu 3"));
        menuBar.add(new JSeparator());
        menuBar.add(new JButton("   Seach ....  "));
        menuBar.add(new JTextField("   Seach ....  "));
        menuBar.add(new JComboBox(new Object[]{"height", "length", "volume"}));
        menuBar.add(Box.createHorizontalGlue());
        menuBar.add(createMenu("About"));
        JFrame frame = new JFrame("MenuGlueDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(menuBar);
        frame.pack();
        frame.setVisible(true);
    }

    public JMenu createMenu(String title) {
        JMenu m = new JMenu(title);
        m.add("Menu item #1 in " + title);
        m.add("Menu item #2 in " + title);
        m.add("Menu item #3 in " + title);
        if (title.equals("About")) {
            m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }
        return m;
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MenuGlueDemo menuGlueDemo = new MenuGlueDemo();
            }
        });
    }
}
Shied answered 14/11, 2011 at 20:13 Comment(4)
I wish I could upvote this more then once, thank you for another excellent answer!Famous
you are welcome, glad to help +1, and not easy job to put any JComponent to the JMenuItem, I used for that only JDialog/JWindow (GridLayout) with added JComponentsShied
Did you mean "Search" instead of "Seach"?Ranit
@Cool Guy probably inadequate intensity of light at end of my tunnel, too :-)Shied

© 2022 - 2024 — McMap. All rights reserved.