如何在 Swing GUI 中启动 JTable? [英] How to initiate a JTable in a Swing GUI?

查看:36
本文介绍了如何在 Swing GUI 中启动 JTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 GUI 中填充 JTable,但我很难做到.我知道我错过了一些简单的东西,但我不知道是什么.我已经创建了自己的 abstractTableModel 并将 GUI Jtable 设置为模型......但它不起作用......这是我的代码:

I am trying to populate a JTable in a GUI and am having a hard time doing it. I know I am missing something simple but I can't figure out what. I have created my own abstractTableModel and I set the GUI Jtable to the model..but it doesn't work....Here is my code:

//Here I Try to start and populate the JTable
myTableModel tModel = new myTableModel(a)
transTable.setModel(tModel);

这是我制作的模型:

package edu.byu.isys.rmyers4.gui;

import javax.swing.table.AbstractTableModel;


public class myTableModel extends AbstractTableModel {

Account a = null;

public myTableModel (Account c){

    this.a = c;

}

@Override
public int getRowCount() {
    return a.getTransactions().size();
}

@Override
public int getColumnCount() {
    return 4;
}

@Override
public Object getValueAt(int row, int col) {
   if(col == 0)
   {
       if(a.getTransactions().get(row).isDebit())
       return "Deposit";
   }
   else
   {
       return "Withdrawal";
   }
   if(col == 1){
       return a.getTransactions().get(row).getAmount();

   }
   else if(col == 2){
       return a.getTransactions().get(row).getMemo();

   }
   else if(col == 3){
       return a.getTransactions().get(row).getDate();

   }

   else{
       return null;
   }
}

}

推荐答案

也许您没有正确地将 JTable 添加到屏幕?您可以尝试一个简单的测试:

Maybe you are not adding the JTable to the screen properly? You can try a simple test:

JFrame frame = new JFrame();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setContentPane(transTable);
frame.setVisible(true);

如果 getTransactions() 确实返回行 - 您将能够看到它们(没有列标题,因为它们未在您的代码中定义).

If indeed getTransactions() return rows - you will be able to see them (without column headers as they are not defined in your code).

这篇关于如何在 Swing GUI 中启动 JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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