如何为多个按钮使用模板对话框面板



我正在用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();
        }
    }
}

您可能会看到面板中的两个按钮都创建了首先创建的对话框("Add Row"或"Edit Row")。我希望他们创建不同的对话框,这样数据的编辑就不会与数据的添加冲突。

为每个按钮分配不同的对话框创建方法,以便您可以打开两个对话框:

    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);
    }  

相关内容

  • 没有找到相关文章

最新更新