i know,how to add table inside of Scrollpane. But i've introduced a loop there which is creating problem so please help me to get rid of this problem (i want to add table inside of scrollpane) it seems like it's working whenever i don't introduce scrollpane but whenever I put table inside of scrollpane, it doesn't work. Please help.
Thanks in advance.
Here is my code:
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();
}
}
Instead of
mainScroll=new JScrollPane();
...
mainScroll.add(listTable);
you need to do this after the loop
mainScroll = new JScrollPane(listTable);
Addendum 1:
You also need to pull the creation of the JTable outside the loop. Inside the loop only add rows to the table.
The core of the solution is like this:
JTable listTable = new JTable(listTableModel);
for (int i = 0; i < 1000; i++) {
// add rows to the table
}
mainScroll = new JScrollPane(listTable);
Addendum 2 (figured I'd need identifiers)
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.