JTable的问题,我可以添加到表中,但看不到所有行 [英] Issues with JTable, I can add to the table but can't see all rows

查看:81
本文介绍了JTable的问题,我可以添加到表中,但看不到所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Netbeans中,我创建了一个表,并向其中添加了"film"和"director"列.这可以正常工作,但是我无法正确地看到表的最后一行的一小部分,尽管提醒我已添加了所有行.我仍然看不到它们.

Within Netbeans I have created a table and added columns "film" and "director" to it. This works fine, but I can't see the table properly only a small section of the last row, which, although alerts me that all rows have been added. I still can't see them fully.

代码和屏幕截图如下.

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {  
DefaultTableModel MovieTable = (DefaultTableModel) MovieTable.getModel();
 MovieTable.getDataVector().removeAllElements();
                   Column1 ="Film1";
                   Column2 ="Director";
                   MovieTable.addRow(new Object[]{Column1, Column2}); } 

屏幕截图

推荐答案

不确定您在做什么错,因为您没有提供相关代码.但是,即使您这样做了,由于GUI Builder生成的所有凌乱代码,它甚至可能对您有所帮助(我想这就是您所使用的).因此,请参见下文.

Not sure what you're doing wrong because you have not provided relevant code. But even if you did, it may bot even help because of all the messy code generated by the GUI Builder (I assume that's what you're using). So see below.

简单的分步教程,不会复制您的问题.希望你能从中得到一些好处.

Simple step by step tutorial, that doesn't replicate your problem. Hope you can gain something from it.

  1. 创建您的JFrame表单

JPanel拖放到框架中.扩大框架的大小.

Drag and drop a JPanel to the frame. Expand it the size for the frame.

JTable拖放到面板上.无需JScrollPane,因为GUI构建器会自动将表包装在滚动窗格中.

Drag and Drop a JTable to the panel. There's no need for a JScrollPane as GUI builder will automatically wrap your table in a scroll pane.

突出显示/选择设计视图中的表,然后转到设计视图右侧的属性窗口.

Highlight/select the table in the design view and go to the Properties window to the right of the design view.

单击 model 属性右侧的 ... .

在对话框中创建模型,即设置行数和列数,并命名列标题.完成后,单击确定".

In the dialog create your model i.e. set the number of rows and columns and name your column headers. Click OK when done.

为电影"和导演"列拖放两个JTextFeild

Drag and drop two JTextFeilds for the Film and Director columns

拖放JButton.右键单击该按钮,为其创建actionPerformed,然后选择Event -> action -> actionPerformed

Drag and drop a JButton. Create the actionPerformed for that button by right clicking it and select Event -> action -> actionPerformed

在自动生成的actionPerformed中执行此操作.

In the auto-generated actionPerformed do this.

private void jButton1ActionPerformed(ActionEvent evt) {                                         
    DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
    model.getDataVector().removeAllElements();

    String column1 = jTextField1.getText();
    String column2 = jTextField2.getText();

    model.addRow(new Object[] {column1, column2});
}    

  • 如果您只想添加行而不删除所有其他行,请摆脱

  • If you just wanted to add row without deleting all the other rows, get rid of

    model.getDataVector().removeAllElements();
    

  • 运行程序,在文本字段中添加一些文本,然后按按钮.遵循上述所有这些步骤,一切都应该正常工作.每次都为我工作.

  • Run your program, add some text to the text field and press the button. Everything should work fine is all these steps above were followed. Works for me every time.

    这篇关于JTable的问题,我可以添加到表中,但看不到所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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