2017-10-27 15 views
0

私が書いたプログラムのGUIを更新しようとしています。新しいGUIには、JTabbedPaneと上記の2つのボタンを持つJPanelを含むJFrameがあります。Java JTabbedPane getコンポーネント

JTabbedPaneの内部には、コンポーネントが異なる3つのJPanelがあります。 (ボタン、テキストフィールドなどのように)今は、タイプに基づいて同じアクションを実行するためにすべてのコンポーネントを取得する必要があります。

Ex。テキストフィールドがある場合は何かをする必要がありますが、ボタンがある場合は別の操作を行う必要があります。

Container focus = general_panel.getFocusCycleRootAncestor(); 
    FocusTraversalPolicy ftp = focus.getFocusTraversalPolicy(); 
    Component comp = ftp.getFirstComponent(general_panel); 
    Component first = comp; 
    while(comp != null){ 
     if(comp instanceof JComboBox){ 
      ((JComboBox) comp).setSelectedIndex(0); 
     } 
     .... 

     comp = ftp.getComponentAfter(focus, comp); 

     if(comp.equals(first)){ 
      break; 
     } 
    } 

と前のGUIでのJPanelと

が正常に動作します:

だから、以前私はこのような何かをしました。 しかし、tabbedpaneを使った同じメソッドは、最初のコンポーネントと他のコンポーネントの代わりに多くの "null"だけを受け取ります。

これは、タブ区画の内側に

javax.swing.JComboBox[,26,24,78x25,layout=javax.swing.plaf.basic.BasicComboBoxUI$Handler,alignmentX=0.0,alignmentY=0.0,[email protected],flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=Bianco] 
null 
null 
null 
null 
null 
null 
null 
null 
null 
null 
null 
null 
null 
null 

「古い」GUIと新しいものの両方がに埋め込まれたGUIの作成者で行われているのJPanelでSystem.out.pritnln(COMP)の結果でありますNetBeansではおそらくすべてのコンポーネントの設定は同じです。

しかし、TabbedPane内のPanelは、JFrame上でJPanelから別の方法で処理されますか?

+0

誰かが同じ状況で積み重なった場合、私はこれで解決しました: – ySlag

答えて

0

ただこれまでと同じような状況に積み重ねられ得る場合の誰かに、私はこれで解決:GUIクラス内

は、パネルでコンテナを作成しました:

Container focus = pnl_generali.getFocusCycleRootAncestor(); 

その後、以下の方法の残りの部分はありません:

public static List<Component> getAllComponents(final Container c) { 
     Component[] comps = c.getComponents(); 
     List<Component> compList = new ArrayList<Component>(); 
     for (Component comp : comps) { 
      compList.add(comp); 
      if (comp instanceof Container) { 
      compList.addAll(getAllComponents((Container) comp)); 
      } 


      if(comp instanceof JTextField){ 
       System.out.println(comp); 
      } 

     } 
     return compList; 
    } 
関連する問題