如何在jscrollpane中添加jtable?

时间:2017-07-26 06:25:36

标签: java jtable jscrollpane

我知道,如何在Scrollpane中添加表格。但是我已经在那里引入了一个创建问题的循环,所以请帮我摆脱这个问题(我想在滚动窗格内添加表格)它似乎无论何时我都不工作介绍滚动窗格,但每当我把桌子放在滚动窗格内时,它都不起作用。请帮忙。

提前致谢。

这是我的代码:

import javax.swing.DefaultCellEditor;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableColumn;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JFrame;

import java.awt.*;

import javax.swing.*;




    public class Simple 
    {
    JScrollPane mainScroll;
    JPanel panel;
    JFrame frame;
    public Simple()
    {
    frame=new JFrame();
    panel=new JPanel();
    mainScroll=new JScrollPane();
    Object[][] rowData = {};
    Object[] columnNames = { "Sr.No","Subject","Department","Status" };
    JComboBox mainBox=new JComboBox();
    mainBox.addItem("Processed");
    mainBox.addItem("Pending");
    DefaultTableModel listTableModel;
    listTableModel = new DefaultTableModel(rowData, columnNames);
    for (int i = 0; i < 1000; i++) {
    String main = "" + i;
    listTableModel.addRow(new Object[] { main, "", "" ,"Choose"});
    JTable listTable;
    listTable = new JTable(listTableModel);
    TableColumn StatusColumn=listTable.getColumnModel().getColumn(3);
    StatusColumn.setCellEditor(new DefaultCellEditor(mainBox));
    mainScroll.add(listTable);

    }


    panel.setLayout(null);
    mainScroll.setBounds(37, 143, 397, 183);


   // mainBox.addItem("Processing");


    frame.add(panel); 
    panel.add(mainScroll);
    frame.setSize(600,600); 
    frame.setVisible(true);


    }
    public static void main(String arg[])
    {
    new Simple();
    }
    }

1 个答案:

答案 0 :(得分:0)

而不是

mainScroll=new JScrollPane();
...
mainScroll.add(listTable);

你需要在循环后执行此操作

mainScroll = new JScrollPane(listTable);

附录1:

您还需要在循环外部创建JTable。在循环内部只向表中添加行。

解决方案的核心是这样的:

JTable listTable = new JTable(listTableModel);
for (int i = 0; i < 1000; i++) {
    // add rows to the table
}
mainScroll = new JScrollPane(listTable);

附录2(我需要标识符)

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import javax.swing.*;

public class Simple {
    JScrollPane mainScroll;
    JPanel panel;
    JFrame frame;

    public Simple() {
        frame=new JFrame();
        panel=new JPanel();
        Object[][] rowData = {};
        Object[] columnNames = { "Sr.No","Subject","Department","Status" };
        JComboBox mainBox=new JComboBox();
        mainBox.addItem("Processed");
        mainBox.addItem("Pending");
        DefaultTableModel listTableModel;
        listTableModel = new DefaultTableModel(rowData, columnNames);
        JTable listTable = new JTable(listTableModel);
        for (int i = 0; i < 1000; i++) {
            String main = "" + i;
            listTableModel.addRow(new Object[] { main, "", "" ,"Choose"});
            TableColumn StatusColumn=listTable.getColumnModel().getColumn(3);
            StatusColumn.setCellEditor(new DefaultCellEditor(mainBox));
        }

        mainScroll = new JScrollPane(listTable);

        panel.setLayout(null);
        mainScroll.setBounds(37, 143, 397, 183);

        frame.add(panel);
        panel.add(mainScroll);
        frame.setSize(600,600);
        frame.setVisible(true);

    }

    public static void main(String arg[]) {
        new Simple();
    }

}