2016-07-05 8 views
0

私はJavaでGUIを書いています。私は現在静的なデータしか持っていないJXTableを持っていますが、私が作成したテンプレートパネルを使ってデータを入力できるようにしたいのです。複数のボタンにテンプレートダイアログパネルを使用する方法は?

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.Window; 
import java.awt.Dialog.ModalityType; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.border.TitledBorder; 
import javax.swing.table.DefaultTableModel; 

public class Template_StackOverflowExample extends JPanel{ 

    private JPanel diagPanel = new dialogTemplate(); 
    Object[] columnIdentifiers = { 
     "id", 
     "imei", 
    }; 

    Object[][] data = { 
     {"1", "123"}, 
     {"2", "123"}, 
     {"3", "123"} 
    }; 

    private JDialog dialog; 
    private static DefaultTableModel model; 

    public Template_StackOverflowExample(){ 
     setLayout(new BorderLayout()); 
     JPanel pane = new JPanel(new BorderLayout()); 

     JButton addRow = new JButton("Add Row"); 
     addRow.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       openRowPane("Add Row"); 
      } 
     }); 
     JButton editRow = new JButton("Edit Row"); 
     editRow.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       openRowPane("Edit Row"); 
      } 
     }); 

     JPanel buttonPane = new JPanel(new GridLayout(0, 1)); 
     TitledBorder buttonBorder = new TitledBorder("Buttons"); 
     buttonPane.setBorder(buttonBorder); 

     buttonPane.add(addRow); 
     buttonPane.add(editRow); 

     model = new DefaultTableModel(); 
     model.setColumnIdentifiers(columnIdentifiers); 
     JTable table = new JTable(model); 

     for(int i = 0; i < data.length; i++){ 
      model.insertRow(i, data[i]); 
     } 

     JScrollPane scrollPane = new JScrollPane(table); 

     pane.add(buttonPane, BorderLayout.LINE_END); 
     pane.add(scrollPane, BorderLayout.CENTER); 

     add(pane, BorderLayout.CENTER); 
    } 

    public void openRowPane(String name){ 
     if(dialog == null){ 
      Window win = SwingUtilities.getWindowAncestor(this); 
      if(win != null){ 
       dialog = new JDialog(win, name, ModalityType.APPLICATION_MODAL); 
       dialog.getContentPane().add(diagPanel); 
       dialog.pack(); 
       dialog.setLocationRelativeTo(null); 
      } 
     } 
     dialog.setVisible(true); 
    } 

    public static void createAndShowGUI(){ 
     JFrame frame = new JFrame("MCVE");  
     Template_StackOverflowExample mainPanel = new  Template_StackOverflowExample(); 
     frame.add(mainPanel); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args){ 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

class dialogTemplate extends JPanel{ 

    private JComponent[] content; 
    private String[] labelHeaders = { 
     "ID:", 
     "IMEI:", 
    }; 

    public dialogTemplate(){ 
     JPanel diagTemplate = new JPanel(); 
     diagTemplate.setLayout(new BorderLayout()); 
     JPanel rowContent = new JPanel(new GridLayout(0, 2)); 

     JLabel idLabel = null; 
     JLabel imeiLabel = null; 

     JLabel[] labels = { 
      idLabel, 
      imeiLabel, 
     }; 

     JTextField idTextField = new JTextField(20); 
     JTextField imeiTextField = new JTextField(20); 

     content = new JComponent[] { 
      idTextField, 
      imeiTextField, 
     }; 

     for(int i = 0; i < labels.length; i++){ 
      labels[i] = new JLabel(labelHeaders[i]); 
      rowContent.add(labels[i]); 
      rowContent.add(content[i]); 
      labels[i].setLabelFor(content[i]); 
     } 

     JButton save = new JButton("Save"); 
     save.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       closeWindow(); 
      } 
     }); 
     JButton cancel = new JButton("Cancel"); 
     cancel.addActionListener(new ActionListener() { 

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

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

     buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     diagTemplate.add(buttonPane, BorderLayout.PAGE_END); 
     diagTemplate.add(rowContent, BorderLayout.CENTER); 

     add(diagTemplate); 
    } 

    public void closeWindow(){ 
     Window win = SwingUtilities.getWindowAncestor(this); 
     if(win != null) { 
      win.dispose(); 
     } 
    } 
} 

あなたはペイン内のボタンの両方が表示される場合がありますように(または「編集行」「行の追加」)最初に作成されたダイアログを作成します。私は、データの編集がデータの追加と矛盾しないように、異なるダイアログを作成するようにします。

ありがとうございます。あなたは2つのダイアログを開くことができるように

答えて

1

は、各ボタンに異なるダイアログの作成方法を割り当てます。

public void openRowPane(String name){ 
     if(dialog == null){ 
      Window win = SwingUtilities.getWindowAncestor(this); 
      //change modality 
      dialog = new JDialog(win, name, ModalityType.MODELESS); 
      dialog.getContentPane().add(diagPanel); 
      dialog.pack(); 
      dialog.setLocationRelativeTo(null); 
     } 
     dialog.setVisible(true); 
    } 

    public void openRowPane2(String name){ 
     if(dialog2 == null){ 
      Window win = SwingUtilities.getWindowAncestor(this); 
      //change modality  
      dialog2 = new JDialog(win, name, ModalityType.MODELESS); 
      dialog2.getContentPane().add(diagPanel2); 
      dialog2.pack(); 
      dialog2.setLocationRelativeTo(null); 

     } 
     dialog2.setVisible(true); 
    } 
+0

を私はすでにこれを試みたが、私はつもりはそれを余分にやってみるんだと信じています。 – Zeliax

+0

これで問題は解決しました。これで、開いているアクションごとに新しいパネルを作成する方法を理解する必要があります。ありがとう! :D – Zeliax

+0

助けてくれてうれしいです。モダリティの変更はしないでください。 diagPanel2は実際には新しいパネルです。 – c0der

関連する問題