简体   繁体   English

如何从组合框更新表格的单元格?

[英]How to update the cells of a table from a combobox?

在此输入图像描述

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import net.java.dev.designgridlayout.DesignGridLayout;
import java.io.*;
import net.java.dev.designgridlayout.Tag;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;  
import java.sql.*;

class table1
{
JFrame JF;
Container C;
JPanel JP;
JLabel creditLabel;
JComboBox credit;
String[] Credit = {"Vasan Phalke", "Pansare", "Anil Kg", "Suresh"};
String[] Names = {"Name", "Qty", "Rate/ Kg", "Rate/Dzn.", "Total Amt."};
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;

public table1()
{
JF = new JFrame();
JP = new JPanel();
C= JF.getContentPane();
JF.pack();
JF.setLocationRelativeTo(null); 
JF.setVisible(true);    

DesignGridLayout layout = new DesignGridLayout(C);

creditLabel = new JLabel("Credit");
credit = new JComboBox<String>(Credit);
model = new DefaultTableModel(Names,5);
    table =new JTable(model){@Override
public boolean isCellEditable(int arg0, int arg1)
{
    return true;
}
               };


  scrollPane= new JScrollPane(table);

layout.row().grid(creditLabel).add(credit);
layout.emptyRow();
layout.row().grid().add(table);

C.add(JP);
}

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

By clicking on the value of combobox, it should appear in the name column of the table, and what changes should be made to the table for automatic calculation, ie, when i enter qty and rate, total amount should automatically be calculated. 通过单击组合框的值,它应该出现在表的名称列中,并且应该对表进行哪些更改以进行自动计算,即,当我输入数量和速率时,应自动计算总金额。 How all these things can be done, please help. 如何完成所有这些事情,请帮忙。 Thanks in advance. 提前致谢。

Read the section from the Swing tutorial on How to Use Tables 阅读关于如何使用表的Swing教程中的部分

The tutorial shows you how to use a combo box as an editor for a column in a table. 本教程将向您展示如何使用组合框作为表中列的编辑器。 This is what you should be doing instead of having a separate combo box. 这是你应该做的,而不是一个单独的组合框。

To calculate the amount, you need to create a custom TableModel and override the setValueAt() method. 要计算金额,您需要创建自定义TableModel并覆盖setValueAt()方法。 Whenever the quantity or rate changes you recalculate the amount. 每当数量或费率发生变化时,您都会重新计算金额。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM