如何从Java中的其他类调用Action

时间:2015-07-31 01:49:34

标签: java swing action jtabbedpane actionevent

如何从Java中的另一个类调用Action?我在线获得了一个CloseTabButton类,允许每个JTabbedPane上有一个简单的关闭标签按钮,但是当关闭标签时,我想根据信息弹出一个对话框(如果文件没有保存,请保存,等等) 。这是文件:

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

class CloseTabButton extends JPanel implements ActionListener {
    private JTabbedPane pane;
    public CloseTabButton(JTabbedPane pane, int index) {
        this.pane = pane;
        setOpaque(false);

        // CloseIcon class just had a button with an x painted on it
        Icon closeIcon = new CloseIcon();
        JButton close = new JButton(closeIcon);

        close.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));
        close.addActionListener(this);

        add(new JLabel(pane.getTitleAt(index), pane.getIconAt(index), JLabel.LEFT));
        add(close);

        pane.setTabComponentAt(index, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int i = pane.indexOfTabComponent(this);

        String fileName = pane.getToolTipTextAt(i);

        // Where I want to ask if user wants to save, etc.
        if (fileName == "Untitled") {
            // Do stuff
        }

        pane.remove(i); // Removes the tab

        // If tab count < 1, then disable the save and save as buttons on menu
        if (pane.getTabCount() < 1) {
            JFrame frame = (JFrame) pane.getParent().getParent().getParent().getParent().getParent(); // Yes, there is that many in my code to get the parent JFrame

            int menuCount = frame.getJMenuBar().getMenuCount();

            for (int a = 0; a < menuCount; a++) {
                int itemCount = frame.getJMenuBar().getMenu(a).getItemCount();

                for (int b = 0; b < itemCount; b++) {
                    Component component = frame.getJMenuBar().getMenu(a).getMenuComponent(b);

                    if (!(component instanceof JSeparator)) {
                        // Not a seperator
                        String itemName = frame.getJMenuBar().getMenu(a).getItem(b).getAccessibleContext().getAccessibleName();
                        if (itemName == "Save As..") {
                            frame.getJMenuBar().getMenu(a).getItem(b).setEnabled(false);
                        }
                    }
                }
            }
        }
    }
}

在我的主要课程中,我有如下列出的行动:

static Action Close = new AbstractAction("Close") {
    public void actionPerformed(ActionEvent e) {
        closeCurrentWindow(); // function that will close tab
    }
}

其他菜单项也是Actions,正如您所看到的,我目前在CloseTabButton类中所做的事情非常令人沮丧,而且很可能是错误的编码方式。有没有更简单的方法来做我正在做的事情?

1 个答案:

答案 0 :(得分:0)

我可能做的第一件事是向ActionListener提供CloseTabButton支持,例如......

public class CloseTabButton extends JPanel {

    private JTabbedPane pane;

    public CloseTabButton(JTabbedPane pane, int index) {
        this.pane = pane;
        setOpaque(false);

        // CloseIcon class just had a button with an x painted on it
        Icon closeIcon = new CloseIcon();
        JButton close = new JButton(closeIcon);

        close.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));
        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                fireActionPerformed();
            }

        });

        add(new JLabel(pane.getTitleAt(index), pane.getIconAt(index), JLabel.LEFT));
        add(close);

        pane.setTabComponentAt(index, this);
    }

    public void addActionListener(ActionListener listener) {
        listenerList.add(ActionListener.class, listener);
    }

    public void removeActionListener(ActionListener listener) {
        listenerList.remove(ActionListener.class, listener);
    }

    protected void fireActionPerformed() {
        ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
        ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "You could provide you own action command for each tab here");
        for (ActionListener listener : listeners) {
            listener.actionPerformed(evt);
        }
    }

}

基本上,现在允许您将自己的ActionListener注册到CloseTabButton

接下来,这个fileName == "Untitled"不是你在Java中比较String的方式,你应该使用更像"Untitled".equals(fileName)

的东西

如果您的菜单基于实际Action,那么您只需自行停用Action即可。这需要一点点工作,但更少的猜测&#34;工作,然后你现在就做。

基本上,您会监控JTabbedPane本身,监控所选标签的更改并更新各个Action自身的状态

您可以通过多种方式执行此操作,例如将JTabbedPane的引用传递给Action,以便他们可以执行自己的监控(但我会使用某种方式)管理界面,可以更轻松地向Action提供信息,并直接解除代码和对JTabbedPane的依赖,然后您可以自由地使用JInternalFrame

你可以有一个&#34;菜单管理器&#34;做了类似的工作,监视文档容器的更改并根据当前状态更改菜单Action的状态,作为示例

<强>更新

如果您正在使用Action API(我建议使用),那么您可以简单地执行类似...

public class CloseTabButton extends JPanel {

    private JTabbedPane pane;

    public CloseTabButton(JTabbedPane pane, Action action, int index) {
        this.pane = pane;
        setOpaque(false);

        // CloseIcon class just had a button with an x painted on it
        Icon closeIcon = new CloseIcon();
        JButton close = new JButton(action);
        close.setIcon(closeIcon);
        close.setPreferredSize(new Dimension(closeIcon.getIconWidth(), closeIcon.getIconHeight()));

        add(new JLabel(pane.getTitleAt(index), pane.getIconAt(index), JLabel.LEFT));
        add(close);

        pane.setTabComponentAt(index, this);
    }

}

传递Action进行关闭操作,然后对JMenuItemJTabbedPane使用相同的操作。

&#34;核心&#34;问题是如何识别&#34;当前&#34;选项卡和文档以统一的方式