Swing: Link Toggle Buttons Together With a Button Group, Along With Corresponding Menu Items

Swing: link toggle buttons together with a button group, along with corresponding menu items

The Action interface is an effective approach "if you have two or more components that perform the same function," as discussed in How to Use Actions. In particular, an Action would allow your buttons and menu items to use the same code.

Addendum: The example below shows how a JMenu and a JToolBar can share the same Action for each of several files.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

/** @see http://stackoverflow.com/questions/4038605 */
public class FileMenu {

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {
new FileMenu().create();
}
});
}

void create() {
File userDir = new File(System.getProperty("user.dir"));
File[] files = userDir.listFiles();

JMenu menu = new JMenu("Recent Files");
JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
JLabel label = new JLabel(" ", JLabel.CENTER);
for (File f : files) {
if (f.isFile() && !f.isHidden()) {
RecentFile rf = new RecentFile(f, label);
menu.add(new JMenuItem(rf.getAction()));
toolBar.add(rf.getAction());
}
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);

JFrame f = new JFrame("FileMenu");
f.setJMenuBar(menuBar);
f.add(toolBar, BorderLayout.CENTER);
f.add(label, BorderLayout.SOUTH);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

class RecentFile extends AbstractAction {

private final File file;
private final JLabel label;

public RecentFile(final File file, final JLabel label) {
this.file = file;
this.label = label;
this.putValue(Action.NAME, file.getName());
this.putValue(Action.SHORT_DESCRIPTION, file.getAbsolutePath());
}

public void actionPerformed(ActionEvent e) {
label.setText(file.getName());

}

public Action getAction() {
return this;
}
}

Menu buttons to be present in all the frames

JToolBar is ideal for this, as it can float above the frame. Action lets you encapsulate the code conveniently. FileMenu is a basic example that combines the two.

Can I add multiple frames in CardLayout in Java?

No, a JFrame is a top-level container. Presumably, each of your existing frames sets or adds a container to the content pane, so you can add those containers to the CardLayout. Several examples are shown here. Also consider a JToolBar, seen here, for your navigation panel.

How to use Java Swing Actions to create menu items and toolbar items in Netbeans

Both JMenuItem and JButton have a setAction() method that you can use for setting the same instance of your Action.

Java swing 1.6 Textinput like firefox bar

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

Sample Image

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

Sample Image

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


Related Topics



Leave a reply



Submit