4

I would like to create a textwidget/component wich looks like the firefox address bar. I mean a Textfield wich allows me to place little Buttons inside the field (e.g. cancel/reload/...)

I tried customizing a JLayeredPane, by creating a custom layout manager which maximizes the Textfield, and places the remainder from right to left. My problem is that this gave painting issues, I would not always see the items I added over the textfield. This might be Jython related, I try suppling java.lang.Integer(1) to the JLayeredPane.add. However the Layers are ordered exactly reverse to what the documentation says.

TO cricumvent this I derived my own JLayeredPane class and redefined paint to call paintComponents which in turn iterates over all components and calls their paint method, starting with the textbox, the rest thereafter.

However I don't always get the updates right away, meaning the buttons are hidden/only partly displayed and I can't Interact with the button.

  1. What do I have to acutally see the update on screen (is it hidden in a buffer?))
  2. How can I make it so that I can interact with the buttons?
  3. How can I shorten the Texxtfield, so that the text starts scrolling to the front before I reach the end of the Textfield so that the text does not get hidden by the buttons? I still want the Textfields area to extend under the buttons

edit: the button is only displayed in the right place after i make the window smaller, after that it is also clickable

edit2: I took the freedom to boil the answer down to this, which hides away a lot of that button code/unneccessary stuff

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class playground {

    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");

    public playground() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.add(makeButton(), BorderLayout.WEST);

        JTextField text = new JTextField(20);
        text.setBorder(null);
        panel.add(text, BorderLayout.CENTER);
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setOpaque(false);
        buttonsPanel.setLayout(new GridLayout(1, 2, 2, 2));

        buttonsPanel.add(makeButton());
        buttonsPanel.add(makeButton());

        panel.add(buttonsPanel, BorderLayout.EAST);
        panel.setBackground(text.getBackground());

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(panel);
        menuBar.add(Box.createHorizontalGlue());
        JFrame frame = new JFrame("MenuGlueDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(menuBar);
        frame.pack();
        frame.setVisible(true);
    }

    public JToggleButton makeButton() {
        final JToggleButton button = new JToggleButton();
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setBorder(null);

        button.setIcon((errorIcon));
        button.setRolloverIcon((infoIcon));
        button.setSelectedIcon(warnIcon);
        button.setPressedIcon(warnIcon);

        button.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (button.isSelected()) {
                } else {
                }
            }
        });

        return button;
    }

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

            @Override
            public void run() {
                playground menuGlueDemo = new playground();
            }
        });
    }
}
ted
  • 4,791
  • 5
  • 38
  • 84
  • while writing this I realized it might be easier to make a Panel to represent the textfield and make the textfields background and border invisible, buit I am still interested in a solution, for educational purposes – ted May 31 '12 at 20:05
  • It looks like you'd better use buttons, rather than textfields. see mKorbel's answer – Asaf May 31 '12 at 20:14
  • @Asaf I think the point of the question is that he wants to achieve the specific **visual** effect that the FF address bar has. Placing buttons to the right of the textfield isn't it. – millimoose May 31 '12 at 21:02

2 Answers2

5

may be could it be simple by using JMenuBar, with Auto complete ComboBox / JFextField for example

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();
            }
        });
    }
}

EDIT

I can simply but a text input and some buttons in a container with a proper layout and achieve [Textfield...] [B1] [B2] but I want [Textfield [B1] [B2]]

enter image description here

with proper LayoutManager

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class MenuGlueDemo {

    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");

    public MenuGlueDemo() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JButton button = new JButton();
        button.setFocusable(false);
        //button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon((errorIcon));
        button.setPressedIcon(warnIcon);
        panel.add(button, BorderLayout.WEST);
        JTextField text = new JTextField(20);
        text.setBorder(null);
        panel.add(text, BorderLayout.CENTER);
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setOpaque(false);
        buttonsPanel.setLayout(new GridLayout(1, 2, 2, 2));
        final JToggleButton toggleButton = new JToggleButton();
        toggleButton.setFocusable(false);
        toggleButton.setMargin(new Insets(0, 0, 0, 0));
        toggleButton.setContentAreaFilled(false);
        toggleButton.setIcon((errorIcon));
        toggleButton.setRolloverIcon((infoIcon));
        toggleButton.setSelectedIcon(warnIcon);
        toggleButton.setPressedIcon(warnIcon);
        toggleButton.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (toggleButton.isSelected()) {
                } else {
                }
            }
        });
        buttonsPanel.add(toggleButton);
        final JToggleButton toggleButton1 = new JToggleButton();
        toggleButton1.setFocusable(false);
        toggleButton1.setMargin(new Insets(0, 0, 0, 0));
        toggleButton1.setContentAreaFilled(false);
        toggleButton1.setIcon((errorIcon));
        toggleButton1.setRolloverIcon((infoIcon));
        toggleButton1.setSelectedIcon(warnIcon);
        toggleButton1.setPressedIcon(warnIcon);
        toggleButton1.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if (toggleButton1.isSelected()) {
                } else {
                }
            }
        });
        buttonsPanel.add(toggleButton1);
        panel.add(buttonsPanel, BorderLayout.EAST);
        panel.setBackground(text.getBackground());
        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(panel);
        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);
    }

    private 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();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I did not give you the downvote, however I wonder how does this help me? I need a textinput under the Buttons and I dont want dropdowns over the text input. Nice fast answer but I am afraid you missed the point. I can simply but a text input and some buttons in a container with a proper layout and achieve `[Textfield...] [B1] [B2]` but I want `[Textfield [B1] [B2]]` – ted May 31 '12 at 20:13
  • Nice update, maybe I should clarify the combobox/dropdown/autocomplete part is secondary/not needed – ted May 31 '12 at 20:17
  • this is example how to do it, for placing & possitioning JButtons and Icon you can use J(X)Layer (JLayer in Java7 is based on JXLayer) or another way is to use [GlassPane](http://stackoverflow.com/a/9734016/714968), don't use JLayeredPane ever, – mKorbel May 31 '12 at 20:19
  • basically this could be possible by using JEditorPane, could be used as container too, but I'd suggest to search for full runnable examples (based on JPanel or JMenuBar), because Document (Model for JTextComponents) isn't easy job – mKorbel May 31 '12 at 20:27
  • please see my edit, sure you can to put any JComponent to the another, but you ahve to set proper LayoutManager, nothing special you can to try change the `JPanel panel = new JPanel();` with `JTextField panel = new JTextField(30);`, you can put each of JCompoents to the another(JComboBox, JSpinner ...), notice only JFrame and JPanel [accepting the PreferredSize came from its child(s)](http://stackoverflow.com/questions/8575641/how-returns-xxxsize-from-jcomponents-added-to-the-jlabel) – mKorbel May 31 '12 at 21:06
  • I am aware that there are layers in java 7 and i was impressed by the example page. Unfortunately I have to write code for java 6 (thats why I put 1.6 in the title), it is a bit complicated, but the software used here only guarantees java 1.6 not 1.7. Unfortunately [Java 1.6][http://docs.oracle.com/javase/6/docs/api/javax/swing/package-summary.html] does not contain layer... Could you please elaborate why I should not use `JLayeredPane`? I will ceck your code out in detail tomorrow in the morning, thank you for such a complete answer effort – ted May 31 '12 at 23:34
5

You may be able to adapt the approach shown in Component Border, which allows "a JTextField and a JButton to work together." The related article Text Prompt may also prove useful. Finally, consider JToolBar, illustrated here, as a flexible way to tie components together.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thank you this looks pretty promising and slightly easier. I will check it out tomorrow – ted May 31 '12 at 23:38
  • well the +1 is for sure, i will go with mKorbels answer so since I think it to be more flexible at a first glance, while yours seems to offer a very good (kiss) answer – ted Jun 01 '12 at 06:14