将来自外部类的新数据插入到Jtable中 [英] Inserting new data from External class into Jtable

查看:53
本文介绍了将来自外部类的新数据插入到Jtable中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Stack Overflow的新手,正在我上大学的Java应用程序上寻求帮助.

i am new to Stack overflow and looking for some help on a java app i have been working on at college.

我的问题是,如何使用按钮动作侦听器事件将Jtextfield(外部类中)的整数插入Jtable.

My questions is, how do I insert an integer from a Jtextfield (in external class) into a Jtable using a button action listener event.

我的代码是:

用于将jtextfield整数插入表中的外部类按钮代码

   package banknew;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;



public class CheckingAccount extends BANKNEW
{

final JButton DepositAmount = new JButton("Deposit");  
final JTextField tAmount = new JTextField();

    public void CheckingAccount() {
        String title = "Checking Account";
        JFrame checkingAccount = new JFrame(title);

        checkingAccount.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        checkingAccount.setSize(400, 200);
        checkingAccount.setLocation(checkingAccount.getHeight() / 2, checkingAccount.getWidth() / 2);

        final JLabel error = new JLabel("");
        checkingAccount.add(error, BorderLayout.SOUTH);
        error.setVisible(true);
        error.setLocation(5, 600);


        JMenuItem file1 = new JMenuItem("Checking Account");
        JMenuItem file2 = new JMenuItem("Checking Accounts");
        JMenuItem file3 = new JMenuItem("Checking Accounts");
        JMenuItem file4 = new JMenuItem("Bank Account");
        JMenuItem file5 = new JMenuItem("Close");



        JMenu filemenu = new JMenu("File");
        filemenu.add(file1);
        filemenu.add(file2);
        filemenu.add(file3);
        filemenu.addSeparator();
        filemenu.add(file4);
        filemenu.addSeparator();
        filemenu.add(file5);

        JMenuBar menubar = new JMenuBar();
        menubar.add(filemenu);
        checkingAccount.setJMenuBar(menubar);
        BorderLayout border = new BorderLayout();
        filemenu.setLayout(border);
        checkingAccount.setVisible(true);





        /**
         * ****************************************
         * Create Second JPanel - Buttons & ComboBox .
 *****************************************
         */
        JPanel abuttons1 = new JPanel();
        checkingAccount.add(abuttons1);
        //abuttons1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        abuttons1.setBorder(BorderFactory.createTitledBorder(""));

        JButton Withdraw = new JButton("Withdraw");
        JLabel transBankName1 = new JLabel("Account Name:");
        JLabel transAmount = new JLabel("Amount:");


        final JTextField tBankName1 = new JTextField(20);
        final JComboBox AccountName = new JComboBox();
        AccountName.setEditable(false);
        AccountName.setMaximumSize(new java.awt.Dimension(100, 20));
        AccountName.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        transBankName1.setMaximumSize(new java.awt.Dimension(100, 20));
        transAmount.setMaximumSize(new java.awt.Dimension(100, 20));
        tAmount.setMaximumSize(new java.awt.Dimension(100, 20));
        tBankName1.setMaximumSize(new java.awt.Dimension(00, 20));

        abuttons1.setLayout(new GridLayout(5, 1, 5, 5));
        abuttons1.add(transBankName1);
        abuttons1.add(AccountName);
        abuttons1.add(transAmount);
        abuttons1.add(tAmount);

        abuttons1.add(Withdraw);
        abuttons1.add(DepositAmount);
        abuttons1.setLocation(0, 0);
        abuttons1.setSize(300,200);

         DepositAmount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (table.getSelectedRow() > -1) {
                    // assuming from your code that you want to set the
                    // textfield's value at the table's selected row
                    try {
                        Integer amount = Integer.parseInt(tAmount.getText());
                        table.getModel().setValueAt(amount, table.getSelectedRow(), 4);
                    } catch (NumberFormatException nfe) {
                        // User did not provide a number.
                        // do nothing? show dialog? you name it!
                    }
                }
            }
        });
    }


    }

  • External类具有一个带有Textfield和"Deposit"按钮的GUI.
  • 主类具有一个带有5列JTable的GUI
  • 我想做的就是拥有一个带有Button的外部扩展类,然后单击按钮,将本地文本字段中的数量插入到主Jtable中,如"abc"表模型中的代码所示.

    All i want to be able to do is have an external extended class with a Button, and on button click insert amount from local textfield into the main Jtable as shown in the code with the 'abc' table model.

    我已经搜索过Google和堆栈溢出,但是大多数帖子都与SQL或数据库链接有关.

    I have searched google and stack overflow but most posts are related to SQL or database linkage.

    如果有人能指出我正确的方向,我将不胜感激.

    If anyone can point me in the right direction, i would be very grateful.

    谢谢

    复制并粘贴代码,然后尝试上传主类,复制完后让我知道...

    推荐答案

    有点不确定类的全局设置. 但是,如果应该将CheckingAccount写入表,则需要在其构造函数中为其提供对表的引用.

    Am a bit unsure of the more global setup of your classes. But if the CheckingAccount is supposed to write to the table, it'll need to be provided a reference to the table in its constructor.

    public class CheckingAccount {
        final JButton depositAmount = new JButton("Deposit");  
        final JTextField tAmount = new JTextField();
    
        //Provide the JTable to the CheckingAccount when you construct it!
        public CheckingAccount(final JTable table) {
            depositAmountButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (table.getSelectedRow() > -1) {
                        // assuming from your code that you want to set the
                        // textfield's value at the table's selected row
                        try {
                            Integer amount = Integer.parseInt(textField.getText());
                            table.getModel().setValueAt(amount, table.getSelectedRow(), 4);
                        } catch (NumberFormatException nfe) {
                            // User did not provide a number.
                            // do nothing? show dialog? you name it!
                        }
                    }
                }
            });
        }
    }
    

    要了解有关如何使用Swing的更多信息,请查看 Oracle教程

    To learn a bit more about how to use Swing, take a look at the Oracle tutorials

    这篇关于将来自外部类的新数据插入到Jtable中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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