2017-07-26 83 views
1

我知道,如何在Scrollpane裏面添加表格。但我已經介紹了一個循環,這是創建問題,所以請幫助我擺脫這個問題(我想添加滾動窗格內的表),似乎它的工作,只要我不介紹scrollpane,但每當我把表內的scrollpane,它不起作用。請幫忙。如何在jscrollpane內添加jtable?

在此先感謝。

這裏是我的代碼:

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(); 
    } 
    } 
+0

您想添加滾動窗格內千個表? – Lars

+0

1000行不是表 – kartik

回答

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(); 
    } 

} 
+0

仍然沒有工作 – kartik

+0

這是因爲您還需要在循環外部創建JTable。 – jeff6times7

+0

:(不幸的是它不工作 – kartik