Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/Action.html"><code>Action</code></a> interface is an effective approach "if you have two or more components that perform the same function," as discussed in <a href="http://download.oracle.com/javase/tutorial/uiswing/misc/action.html"><em>How to Use Actions</em></a>. In particular, an <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/Action.html"><code>Action</code></a> would allow your buttons and menu items to use the same code.</p> <p>Addendum: The example below shows how a <code>JMenu</code> and a <code>JToolBar</code> can share the same <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/Action.html"><code>Action</code></a> for each of several files.</p> <pre><code>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() &amp;&amp; !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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload