从给定的Form中提取值 [英] Extract values from given Form

查看:121
本文介绍了从给定的Form中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在点击按钮 save 时提取值。从 JTextField JTable 行和列中提取值。只是想大致了解如何提取和保存不同变量中的值。我想在按钮保存下执行这些保存操作。

I want to extract values on clicking the button save. Extract values from JTextField and JTable rows and columns. Just want a rough idea how to do extract and save values in different variables. I want to do these saving actions under the button save.

package build;

import java.awt.*;
import javax.swing.*;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Table_Edit {

    private JTextField textField;
    private JTable table;
    private String Table_Name = new String();

    public Table_Edit() {
        JFrame frame = new JFrame("Table");

        frame.setSize(600, 400);
        frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[]{
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            FormFactory.DEFAULT_COLSPEC,
            FormFactory.RELATED_GAP_COLSPEC,
            ColumnSpec.decode("default:grow"),
            FormFactory.RELATED_GAP_COLSPEC,
            ColumnSpec.decode("default:grow"),},
            new RowSpec[]{
            FormFactory.RELATED_GAP_ROWSPEC,
            FormFactory.DEFAULT_ROWSPEC,
            FormFactory.RELATED_GAP_ROWSPEC,
            RowSpec.decode("default:grow"),
            FormFactory.RELATED_GAP_ROWSPEC,
            RowSpec.decode("default:grow"),}));

        JLabel lblTableName = new JLabel("Table Name :");
        frame.getContentPane().add(lblTableName, "2, 2, right, default");

        textField = new JTextField(Table_Name);
        textField.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        frame.getContentPane().add(textField, "4, 2, 9, 1, left, center");
        textField.setColumns(10);

        JScrollPane scrollPane = new JScrollPane();
        frame.getContentPane().add(scrollPane, "3, 4, 29, 1, fill, fill");

        table = new JTable();
        table.setFillsViewportHeight(true);
        scrollPane.setViewportView(table);
        table.setBorder(new TitledBorder(null, "",
            TitledBorder.CENTER, TitledBorder.TOP, null, null));
        table.setColumnSelectionAllowed(true);
        table.setCellSelectionEnabled(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        table.setModel(new DefaultTableModel(
            new Object[][]{
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},},
            new String[]{
            "Column Name", "Data Type", "NN", "PK"
        }) {
            Class[] columnTypes = new Class[]{
                String.class, Object.class, Boolean.class, Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
        });
        table.getColumnModel().getColumn(0).setResizable(false);
        table.getColumnModel().getColumn(0).setPreferredWidth(106);
        table.getColumnModel().getColumn(1).setResizable(false);
        table.getColumnModel().getColumn(1).setPreferredWidth(92);
        table.getColumnModel().getColumn(2).setResizable(false);
        table.getColumnModel().getColumn(2).setPreferredWidth(26);
        table.getColumnModel().getColumn(3).setResizable(false);
        table.getColumnModel().getColumn(3).setPreferredWidth(30);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, "32, 6, right, fill");

        JButton Save = new JButton("Save");
        Save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                System.out.print(Table_Name);
            }
        });
        Save.setMaximumSize(new Dimension(33, 19));
        Save.setIcon(new ImageIcon(Table_Edit.class.getResource(
            "/com/sun/java/swing/plaf/windows/icons/TreeOpen.gif")));
        panel.add(Save);

        JButton btnNewButton_1 = new JButton("Exit");
        btnNewButton_1.setIcon(new ImageIcon(Table_Edit.class.getResource(
            "/javax/swing/plaf/metal/icons/ocean/close.gif")));
        panel.add(btnNewButton_1);


        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Table_Edit();
    }
}


推荐答案

In除了调用 textField.getText()之外,您还可以获取对表模型的引用,并使用 getValueAt()

In addition to invoking textField.getText(), you can obtain a reference to your table's model and iterate over the values using getValueAt():

JButton save = new JButton("Save");
save.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(textField.getText());
        TableModel model = table.getModel();
        for (int r = 0; r < model.getRowCount(); r++) {
            for (int c = 0; c < model.getColumnCount(); c++) {
                System.out.print(model.getValueAt(r, c) + " ");
            }
            System.out.println();
        }
    }
});

这篇关于从给定的Form中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆