Java swing 1.6 Textinput like firefox bar

I would like to create a textwidget / component that looks like a firefox address bar. I mean a text field that allows me to put small buttons inside the field (e.g. cancel / reload / ...)

I tried setting up JLayeredPane by creating my own layout manager that maximizes the text box and places the remainder from right to left. My problem is that it gave problems with the picture, I would not always see the elements that I added in the text box. This may be due to Jython, I'm trying to send java.lang.Integer(1) to JLayeredPane.add . However, the layers are ordered exactly according to the documentation.

To do this, I derived my own JLayeredPane class and redefined paint to call paintComponents , which, in turn, iterates over all components and calls their drawing method, starting with a text field and then the rest.

However, I do not always receive updates immediately, that is, the buttons are hidden / only partially displayed, and I can not interact with the button.

  • What do I need to see the update on the screen (is it hidden in the buffer?))
  • How can I make it so that I can interact with buttons?
  • How can I shorten Texxtfield so that the text starts to scroll forward before I get to the end of the text field so that the text is not hidden by buttons? I still want the Textfields to expand under the buttons

edit : the button only appears in the right place after I make the window smaller, after which it can also be clicked

edit2 : I took the liberty of boiling the answer to this question, which hides a lot of this button code / unnecessary 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(); } }); } } 
+4
source share
2 answers

can be simple using JMenuBar , Auto complete ComboBox / JFextField , e.g.

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 just use text input and some buttons in the container with the correct layout and achieve [Textfield ...] [B1] [B2], but I want [Textfield [B1] [B2]]

enter image description here

with the correct 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(); } }); } } 
+5
source

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

+5
source

Source: https://habr.com/ru/post/1415454/


All Articles