2016-07-15 4 views
2

私はJava GUIを書いています。私はいくつかのプリセットJComboBoxesを持っていて、お互いに区別できるように、私はクラスを拡張し、enum変数を追加しておきます。JComboBoxクラスを拡張するには?

は、ここでは、2つの標準JComboBoxesのMCVEだ:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.DefaultComboBoxModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class XComboBoxMCVE extends JPanel { 

    private JComboBox tfComboBox; 
    private JComboBox ynComboBox; 
    private JComponent[] components; 

    public XComboBoxMCVE() { 
     setLayout(new BorderLayout()); 

     JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5)); 

     Boolean[] trueFalse = { true, false }; 
     DefaultComboBoxModel tfModel = new DefaultComboBoxModel(trueFalse); 
     tfComboBox = new JComboBox(tfModel); 

     String[] yesNo = { "Yes", "No" }; 
     DefaultComboBoxModel ynModel = new DefaultComboBoxModel(yesNo); 
     ynComboBox = new JComboBox(ynModel); 

     components = new JComponent[] { tfComboBox, ynComboBox }; 

     JButton printSelection = new JButton("Print Type"); 
     printSelection.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       for (JComponent component : components) { 
        // I also have other components in component in program, 
        // therefore this usage.. 
        if (component instanceof JComboBox) { 
         JComboBox temp = (JComboBox) component; 
         System.out.println("Printing selection: " + temp.getSelectedItem().toString()); 
         // if (temp.getBoxType() == BoxType.Company){ 
         // System.out.println("Companies say: " + 
         // temp.getSelectedItem().toString()); 
         // } else if(temp.getBoxType() == BoxType.Device){ 
         // System.out.println("Devices are: " + 
         // temp.getSelectedItem().toString()); 
         // } 
        } 
       } 
      } 
     }); 

     JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5)); 
     buttonPane.add(printSelection); 

     comboPanel.add(tfComboBox); 
     comboPanel.add(ynComboBox); 

     add(comboPanel, BorderLayout.CENTER); 
     add(buttonPane, BorderLayout.PAGE_END); 
    } 

    public static void createAndShowGUI(){ 
     JFrame frame = new JFrame("MCVE"); 
     frame.setLayout(new BorderLayout()); 

     XComboBoxMCVE pane = new XComboBoxMCVE(); 

     frame.add(pane, BorderLayout.CENTER); 
     frame.setResizable(false); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

class XComboBox extends JComboBox { 
    BoxType type; 

    public XComboBox(BoxType type) { 
     this.type = type; 
    } 

    public void setBoxType(BoxType type) { 
     this.type = type; 
    } 

    public BoxType getBoxType() { 
     return this.type; 
    } 

    public enum BoxType { 
     Model, Company, Device 
    } 
} 

例では、上記説明されたように、ユーザがボタンをクリックしたときに、互いから2 JComboBoxes別個に私のために方法はありません。 BoxTypeの使用例は、1つのタイプのメッセージを印刷する一方で、もう1つのタイプのメッセージは別のメッセージを印刷することです。例えば:私は私が私がXComboBoxクラスではまだ実装していないJComboBoxと入力DefaultComboBoxModel、で行ったものと同様の使用しようとするコンストラクタで問題につまずいてきたが

if(temp.getBoxType() == BoxType.Device){ 
    System.out.println("The devices are: " + temp.getSelectedItem().toString()); 
} else if(temp.getBoxType() == BoxType.Company){ 
    System.out.println("The companies say: " + temp.getSelectedItem().toString()); 
} 

質問

要素を構築するとき、私はそれをDefaultComboBoxModelを与えることができるようにどのように私はXComboBoxクラスを変更することができますか?

私はこれを行うことができるようにしたい:

ynComboBox = new XComboBox(tfModel); 
ynComboBox.setBoxType(BoxType.Device); 

は、任意のヘルプとや指導をありがとう!

+2

1.なぜあなたは最初の場所でするJComboBoxを拡張していますか? 2.あなたが実際に何を抱いているのか、BoxTypeが何を表しているのかは分かりません。 3.実際に実行できる実際の[mcve]コードを作成することを検討してください。 –

+0

うわー..ええ。申し訳ありません..すべてそれをできるだけ早く修正します。何らかの理由で古いタイトルを保存しました。 – Zeliax

+0

完了...何かが分かりにくいかどうかお気軽にお問い合わせください。 MCVEはXComboBoxを使用しませんが、質問の一部であるので、私はそこに保管しました。 – Zeliax

答えて

2

JComboBoxをBoxTypeに関連付ける必要があります.2つのオブジェクトを関連付けるには、マップ(ここではMap<BoxType, JComboBox>)を使用するのが最適な方法の1つです。これを済ませたら、コンボボックスで簡単に抽出することができます。再び、私はJComboの拡張を避けるだろう。例えば、私のMCVE:あなたはどんな生来の行動を変えることが表示されないよう

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.EnumMap; 
import java.util.List; 
import java.util.Map; 

import javax.swing.BorderFactory; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class XComboBoxMCVE extends JPanel { 
    private static final long serialVersionUID = 1L; 
    private Map<BoxType, JComboBox<String>> comboMap = new EnumMap<>(BoxType.class); 

    public XComboBoxMCVE() { 
     setLayout(new BorderLayout()); 
     JPanel comboPanel = new JPanel(new GridLayout(0, 1, 5, 5)); 

     // just for example 
     String[] modelItemsArray = {"Model Item A", "Model Item B", "Model Item C", "Model Item D"}; 
     BoxItems modelItems = new BoxItems(BoxType.MODEL, modelItemsArray); 
     String[] CompanyItemsArray = {"Company Item A", "Company Item B", "Company Item C", "Company Item D"}; 
     BoxItems companyItems = new BoxItems(BoxType.COMPANY, CompanyItemsArray); 
     String[] deviceItemsArray = {"Device Item A", "Device Item B", "Device Item C", "Device Item D"}; 
     BoxItems deviceItems = new BoxItems(BoxType.DEVICE, deviceItemsArray); 

     createAndPlaceComboBox(BoxType.MODEL, modelItems, comboPanel); 
     createAndPlaceComboBox(BoxType.COMPANY, companyItems, comboPanel); 
     createAndPlaceComboBox(BoxType.DEVICE, deviceItems, comboPanel); 

     JButton printSelection = new JButton("Print Type"); 
     printSelection.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       for (BoxType boxType : BoxType.values()) { 
        // use the Map to get the appropriate JComboBox 
        JComboBox<String> combo = comboMap.get(boxType); 
        String selection = (String) combo.getSelectedItem(); 
        if (selection == null) { 
         selection = "NONE"; 
        } 
        String text = String.format("Selection for box type %s: %s: ", 
          boxType.getText(), selection); 
        System.out.println(text); 
       } 
      } 
     }); 

     JPanel buttonPane = new JPanel(new GridLayout(0, 1, 5, 5)); 
     buttonPane.add(printSelection); 

     add(comboPanel, BorderLayout.CENTER); 
     add(buttonPane, BorderLayout.PAGE_END); 
    } 

    private void createAndPlaceComboBox(BoxType boxType, BoxItems boxItems, JPanel panel) { 
     String[] items = boxItems.getItems().toArray(new String[] {}); 
     DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(items); // create model 
     JComboBox<String> combo = new JComboBox<>(comboModel); // crteate combo 
     comboMap.put(boxType, combo); // put combo into Map 

     JPanel wrapPanel = new JPanel(new BorderLayout()); // wrapper panel that has a title border 
     wrapPanel.add(combo); 
     wrapPanel.setBorder(BorderFactory.createTitledBorder(boxType.getText() + " Items")); 
     panel.add(wrapPanel); 
    } 

    public static void createAndShowGUI() { 
     JFrame frame = new JFrame("MCVE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     XComboBoxMCVE pane = new XComboBoxMCVE(); 
     frame.add(pane, BorderLayout.CENTER); 
     frame.setResizable(false); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

// A better enum, one not inside of an unnecessary class 
enum BoxType { 
    MODEL("Model"), COMPANY("Company"), DEVICE("Device"); 
    private String text; 

    private BoxType(String text) { 
     this.text = text; 
    } 

    public String getText() { 
     return text; 
    } 
} 

// class to associate BoxType with possible items associated with it 
class BoxItems { 
    private BoxType boxType; 
    private List<String> items = new ArrayList<>(); 

    public BoxItems(BoxType boxType) { 
     this.boxType = boxType; 
    } 

    public BoxItems(BoxType boxType, String[] itemsArray) { 
     this(boxType); 
     for (String item : itemsArray) { 
      items.add(item); 
     } 
    } 

    public void addItem(String item) { 
     items.add(item); 
    } 

    public BoxType getBoxType() { 
     return boxType; 
    } 

    public List<String> getItems() { 
     // unmodifiable so that can't be changed outside of this class 
     return Collections.unmodifiableList(items); 
    } 
}