//So for a school project I have to add a JTable to a JPanel in a separate class
ID: 3877261 • Letter: #
Question
//So for a school project I have to add a JTable to a JPanel in a separate class without modifying the latter. This is the class where the JPanel is located.
public class ThisSwingView extends SwingView implements ThisTCRMView {
public ThisSwingView() {
super();
setTitle("This");
JScrollPane centerScrollPane = new JScrollPane();
centerScrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
centerScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
centerScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
setCenterPanel(centerScrollPane);
JPanel centerGrid = new JPanel();
centerScrollPane.setViewportView(centerGrid);
GridBagLayout gbl_centerGrid = new GridBagLayout();
gbl_centerGrid.columnWidths = new int[]{100, 475, 0};
gbl_centerGrid.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_centerGrid.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_centerGrid.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
centerGrid.setLayout(gbl_centerGrid);}}
This is the class I can modify:
public class NewThisSwingView extends ThisSwingView implements ThisTCRMSales{
public NewThisSwingView(){
super();
JPanel centerGrid = new JPanel();
JTable table;
Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
Object columnNames[] = { "Column One", "Column Two", "Column Three" };
table = new JTable(rowData, columnNames);
GridBagConstraints gbc_Table = new GridBagConstraints();
gbc_contactsTable.insets = new Insets(0, 0, 5, 0);
gbc_contactsTable.fill = GridBagConstraints.HORIZONTAL;
gbc_contactsTable.gridx = 1;
gbc_contactsTable.gridy = 12;
centerGrid.add(table, gbc_Table);
}}
//However, when I do this, my table will not print on the screen. I have already modified the main class to run the class I am modifying but it won't work. Thanks in advance.
Explanation / Answer
Solution:
Since you have not provided the full code, hence i could not run it, but there seems to be two issues:
table = new JTable(rowData, columnNames);
table.setVisible(true);
..
table = new JTable(rowData, columnNames);
table.setVisible(true);
JScrollPane pane = new JScrollPane(table);
GridBagConstraints gbc_Table = new GridBagConstraints();
gbc_contactsTable.insets = new Insets(0, 0, 5, 0);
gbc_contactsTable.fill = GridBagConstraints.HORIZONTAL;
gbc_contactsTable.gridx = 1;
gbc_contactsTable.gridy = 12;
centerGrid.add(panel, gbc_Table);
...
..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.