2013-05-15 58 views
0

我有JTabbedPane,每個選項卡都有一個JTextPane。與多個容器共享組件

每個JTextPane都有一個彈出菜單,但我希望他們都共享完全相同的彈出式菜單。爲什麼?因爲當我切換標籤頁時,我希望在每個彈出框上突出顯示相同的選項。

我該怎麼做?我嘗試添加一個靜態PopupMenu實例到每個窗格,但是當我將它添加到一個,它從其他消失..

編輯:在下面的SSCCE時,TabOne上選中該複選框,它不檢查TabTwo 。我希望它檢查兩個選項卡。除了那個之外,所有其他選項對於每個選項卡都是唯一的。

SSCCE:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.lang.reflect.InvocationTargetException; 

import javax.swing.*; 


public class Main { 

    public static void main(String[] Args) { 
     final Main M = new Main(); 

     try { 
      SwingUtilities.invokeAndWait(new Runnable() { 

       @Override 
       public void run() { 
        JFrame F = new JFrame("SSCCE"); 
        JTabbedPane Pane = new JTabbedPane(); 
        Pane.addTab("TabOne", M.new DebugBox(500, 500)); 
        Pane.addTab("TabTwo", M.new DebugBox(500, 500)); 

        F.setLayout(new BorderLayout()); 
        F.add(Pane, BorderLayout.NORTH); 
        F.pack(); 
        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        F.setVisible(true); 
       } 

      }); 
     } catch (InvocationTargetException | InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 


    public class DebugBox extends JTextPane { 

     private JScrollPane ScrollPane = null; 
     private final JPopupMenu Menu = new JPopupMenu(); 
     private static final long serialVersionUID = 7731036968185936516L; 

     public DebugBox(int Width, int Height) { 
      this.ScrollPane = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
      this.setPreferredSize(new Dimension(Width, Height)); 

      JCheckBoxMenuItem Debug = new JCheckBoxMenuItem(new AbstractAction() { 
       private static final long serialVersionUID = -336209978671944858L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
           //DO something here that affects ALL the DebugBoxes because they all share this Menu Option somehow :S 
           //Change name of this menu option, all instances have their names changed too. 
       } 
      }); 

      JMenuItem Copy = new JMenuItem(new AbstractAction() { 
       private static final long serialVersionUID = -6774461986513304498L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        DebugBox.this.copy(); 
       } 
      }); 

      JMenuItem Clear = new JMenuItem(new AbstractAction() { 
       private static final long serialVersionUID = -5567371173360543484L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        DebugBox.this.setText(null); 
       } 
      }); 

      JMenuItem SelectAll = new JMenuItem(new AbstractAction() { 
       private static final long serialVersionUID = -8792250195980016624L; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        DebugBox.this.selectAll(); 
       } 
      }); 

      this.Menu.add(Copy); 
      this.Menu.add(Clear); 
      this.Menu.add(SelectAll); 
      this.Menu.add(Debug); 

      Copy.setText("Copy"); 
      Clear.setText("Clear"); 
      SelectAll.setText("Select All"); 
      Debug.setText("Show Debug Box"); 
      this.setEditable(false); 
      this.add(this.Menu); 

      this.addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseReleased(MouseEvent e) { 
        if (e.isPopupTrigger()) { 
         DebugBox.this.Menu.show(DebugBox.this, e.getX(), e.getY()); 
        } 
       } 
      }); 
     } 
    } 
} 
+1

你可以提供一個[SSCCE(http://sscce.org/)如何您的工作流程... – MadProgrammer

+0

可能重複:http://stackoverflow.com /問題/ 4620601 /傾斜-A-擺動分量待添加到多容器。你可以根據@MadProgrammer顯示你是如何處理彈出式菜單的。 – Craig

+0

寫完一個快速而髒的SSCCE。我剛剛編輯了這篇文章。 – Brandon

回答

2

如果你想分享的對象,那麼你需要創建對象並通過對象作爲參數傳遞給其他類。例如:

JPopupMenu popup = createSharedPopupMenu(); 
Pane.addTab("TabOne", M.new DebugBox(500, 500, popup)); 
Pane.addTab("TabTwo", M.new DebugBox(500, 500, popup)); 

然後,您需要創建適用於任何文本組件的通用操作。該DefaultEditorKit提供了一些這些行動對您:

public JPopupMenu createSharedPopupMenu() 
{ 
    JPopupMenu popup = new JPopupMenu() 

    JMenuItem copy = new JMenuItem(new DefaultEditorKit.CopyAction()); 
    popup.add(copy); 
    ... 

    return popup. 
} 

如果你,那麼你需要創建自己的編輯器工具包不提供一個動作,你應該延長TextAction,而不是AbstractAction。 TextAction類有一個方法將返回具有焦點的文本組件。所以你可以用通用的方式實現Action。

此外,您不需要使用MouseListener來調用彈出窗口。你可以使用下面的方法的JComponent:

JComponent.setComponentPopupMenu(JPopupMenu popup) 
+0

我肯定會檢查出TextAction。我無法將Object作爲參數傳遞,因爲Object一次只能由一個容器包含。當添加到第二個容器時,它會從第一個容器中移除。 :( – Brandon

+1

這是正確的,但popupmenu一次只顯示在一個組件上,它不會永久添加到容器中,您傳遞的所有內容都是對組件的引用,因此彈出窗口可以顯示/隱藏的需要 – camickr

+0

最佳答案我沒有想到這個,起初我做了一個靜態的最終PopUpMenu,併爲每個JTextArea添加了一個監聽器,但是這個答案是最好的,我將重新編寫它來適應這個答案。 – Brandon