Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 487
Grammin Avatar asked Nov 14 '11 19:11

Grammin


People also ask

How do I instantiate a JTextField?

Use new JTextField("Some text") to initialize the text field with some text. Use new JTextField(10) to set the default columns of the text field. Use new JTextField("some text", 3) to specify the above properties at once.

Which method is used to add menubar on frame window?

As the code shows, to set the menu bar for a JFrame , you use the setJMenuBar method. To add a JMenu to a JMenuBar , you use the add(JMenu) method.

Can the program put text in JTextField?

The class JTextField is a component that allows editing of a single line of text.

What is JTextField?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt. TextField where it is reasonable to do so.


1 Answers

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();
            }
        });
    }
}
like image 54
mKorbel Avatar answered Oct 21 '22 05:10

mKorbel