创建动态表以使用按钮添加新条目

Posted

技术标签:

【中文标题】创建动态表以使用按钮添加新条目【英文标题】:Create dynamic table to add new entry with button 【发布时间】:2013-10-28 13:56:08 【问题描述】:

我正在尝试构建一个寄存器,以便在单击按钮时,表格上会出现一个新条目。理想情况下,我希望在屏幕左侧构建一个表格,当单击按钮时,会出现两行和新列。我希望表格具有固定大小,但在一定数量的条目后应该可以滚动。到目前为止,我已经创建并格式化了我希望单击以显示新条目的JButton 对象。我也知道我应该使用JTable 来追求这个。

我应该如何制作这个动态表格?

到目前为止的代码:

private void addRegister(JPanel pane) 

             JPanel everythingPane = new JPanel();
             JPanel pluPane = new JPanel();

             Dimension button = new Dimension(200,150);

             JTable pluTable = new JTable();


             JPanel buttonPane = new JPanel();

             buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.Y_AXIS));

             //JPanel buttonPane = new JPanel (new FlowLayout (FlowLayout.LEFT));

             JPanel subPane1 = new JPanel();
             subPane1.setAlignmentX(LEFT_ALIGNMENT);
             JPanel subPane2 = new JPanel();
             subPane2.setAlignmentX(LEFT_ALIGNMENT);
             JPanel subPane3 = new JPanel();
             subPane3.setAlignmentX(LEFT_ALIGNMENT);
             JPanel subPane4 = new JPanel();
             subPane4.setAlignmentX(LEFT_ALIGNMENT);
             JPanel subPane5 = new JPanel();
             subPane5.setAlignmentX(LEFT_ALIGNMENT);
             JPanel alignmentLayer = new JPanel();

             JButton frappuccino = new JButton("Frappuccino");
             JButton icedCoffee = new JButton("Iced Coffee");
             frappuccino.setPreferredSize(button);
             icedCoffee.setPreferredSize(button);

             JButton arizona = new JButton("Arizona Green Tea");
             JButton izze = new JButton("Izze");
             arizona.setPreferredSize(button);
             izze.setPreferredSize(button);

             JButton snapple = new JButton("Snapple");
             JButton gatorade = new JButton("Gatorade");
             snapple.setPreferredSize(button);
             gatorade.setPreferredSize(button);

             JButton water = new JButton("Water");
             JButton appleJuice = new JButton("Apple Juice");
             water.setPreferredSize(button);
             appleJuice.setPreferredSize(button);

             JButton orangeJuice = new JButton("Orange Juice");
             JButton mentos = new JButton("Mentos");
             orangeJuice.setPreferredSize(button);
             mentos.setPreferredSize(button);

             JButton gum = new JButton("Gum");
             JButton cliffBar = new JButton("Cliff Bar");
             gum.setPreferredSize(button);
             cliffBar.setPreferredSize(button);

             JButton littleBites = new JButton("Little Bites");
             JButton welchs = new JButton("Welch's Fruit Snacks");
             littleBites.setPreferredSize(button);
             welchs.setPreferredSize(button);

             JButton fiberOneBar = new JButton("Fiber One Bar");
             JButton fiberOneBrownie = new JButton("Fiber One Brownie");
             fiberOneBar.setPreferredSize(button);
             fiberOneBrownie.setPreferredSize(button);

             JButton cheezeIts = new JButton("Cheeze Its");
             JButton goldFish = new JButton("Gold Fish");
             cheezeIts.setPreferredSize(button);
             goldFish.setPreferredSize(button);

             JButton teaBag = new JButton("Tea Bag");
             JButton poptarts = new JButton("Poptarts");
             teaBag.setPreferredSize(button);
             poptarts.setPreferredSize(button);

             JButton sampleButton = new JButton("Sample Button");
             JButton sampleButton2 = new JButton("Sample Button");
             sampleButton.setPreferredSize(button);
             sampleButton2.setPreferredSize(button);

             JButton sampleButton3 = new JButton("Sample Button");
             JButton sampleButton4 = new JButton("Sample Button");
             sampleButton3.setPreferredSize(button);
             sampleButton4.setPreferredSize(button);


             subPane1.add(frappuccino);
             subPane1.add(icedCoffee);

             subPane1.add(arizona);
             subPane1.add(izze);

             subPane2.add(snapple);
             subPane2.add(gatorade);

             subPane2.add(water);
             subPane2.add(appleJuice);

             subPane3.add(orangeJuice);
             subPane3.add(mentos);

             subPane3.add(gum);
             subPane3.add(cliffBar);

             subPane4.add(littleBites);
             subPane4.add(welchs);

             subPane4.add(fiberOneBar);
             subPane4.add(fiberOneBrownie);

             subPane5.add(cheezeIts);
             subPane5.add(goldFish);

             subPane5.add(teaBag);
             subPane5.add(poptarts);             

             alignmentLayer.add(sampleButton);
             alignmentLayer.add(sampleButton2);
             alignmentLayer.add(sampleButton3);
             alignmentLayer.add(sampleButton4);


             buttonPane.add(subPane1);
             buttonPane.add(subPane2);
             buttonPane.add(subPane3);
             buttonPane.add(subPane4);
             buttonPane.add(subPane5);
             buttonPane.add(Box.createRigidArea(new Dimension(5,100)));
             buttonPane.add(alignmentLayer);


             pluPane.add(pluTable);

             everythingPane.add(pluPane);
             everythingPane.add(buttonPane);

             pane.add(everythingPane);

      

【问题讨论】:

如需尽快获得更好的帮助,请发布SSCCE。请注意,trashgod 的 SSCCE 比问题中看到的不可编译代码 sn-p 短。 你可以在这个sscce中试验addRow()addColumn() 【参考方案1】:

JTable 更常用的方式是添加,而不是。我重构了这个example 来说明下面的基本思想。每次单击该按钮时,都会在表格模型中添加一个新行,从而更新表格本身。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * @see https://***.com/a/19472190/230513
 * @see https://***.com/a/7519403/230513
 */
 public class TableAddTest extends JPanel 

    private static final int N_ROWS = 8;
    private static String[] header = "ID", "String", "Number", "Boolean";
    private DefaultTableModel dtm = new DefaultTableModel(null, header) 

        @Override
        public Class<?> getColumnClass(int col) 
            return getValueAt(0, col).getClass();
        
    ;
    private JTable table = new JTable(dtm);
    private JScrollPane scrollPane = new JScrollPane(table);
    private JScrollBar vScroll = scrollPane.getVerticalScrollBar();
    private int row;
    private boolean isAutoScroll;

    public TableAddTest() 
        this.setLayout(new BorderLayout());
        Dimension d = new Dimension(320, N_ROWS * table.getRowHeight());
        table.setPreferredScrollableViewportSize(d);
        for (int i = 0; i < N_ROWS; i++) 
            addRow();
        
        scrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        vScroll.addAdjustmentListener(new AdjustmentListener() 

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) 
                isAutoScroll = !e.getValueIsAdjusting();
            
        );
        this.add(scrollPane, BorderLayout.CENTER);
        JPanel panel = new JPanel();
        panel.add(new JButton(new AbstractAction("Add Row") 

            @Override
            public void actionPerformed(ActionEvent e) 
                addRow();
            
        ));
        this.add(panel, BorderLayout.SOUTH);
    

    private void addRow() 
        char c = (char) ('A' + row++ % 26);
        dtm.addRow(new Object[]
                Character.valueOf(c),
                String.valueOf(c) + String.valueOf(row),
                Integer.valueOf(row),
                Boolean.valueOf(row % 2 == 0)
            );
    

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

            @Override
            public void run() 
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                TableAddTest nlt = new TableAddTest();
                f.add(nlt);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            
        );
    

【讨论】:

以上是关于创建动态表以使用按钮添加新条目的主要内容,如果未能解决你的问题,请参考以下文章

创建动态表以比较数据列

用于动态多组条目的组件

动态添加基于工作表名称的新表单控件复选框(Excel VBA)

动态创建新 Vimeo 播放器列表并添加事件以暂停每个按钮单击的最佳方法是啥?

通过asp.net中的代码向网页添加按钮并删除动态数据库条目

带有表变量的动态查询以循环遍历所有表以在数据库中更新