Reputation: 159
My JTable is not showing up. I don't have a clue why. maybe you can help me. I have a Window Class which has a right panel and the right panel should show a table with model data. I have used dummy data to start off with such as Month and Days.
Here is the Right Panel Class
import model.*;
import java.awt.*;
import java.awt.event.*;
//import java.text.*;
import javax.swing.*;
public class RightPanel extends JPanel implements View
{
private TablePanel songsnartists = new TablePanel();
Shop shop;
public RightPanel(Shop ourShop)
{
shop = ourShop;
setup();
build();
setVisible(true);
}
private void setup()
{
setPreferredSize(new Dimension(300, 300));
setLayout(new GridLayout(3,2));
setBorder(BorderFactory.createLineBorder(Color.blue));
}
private void build()
{
add(songsnartists);
}
public void update()
{}
}
/* simple enough so far just trying to add the table panel to my right panel */
Here is the TablePanel class that I created. It inherits JTable and uses a private class MyTableModel which inherits AbstractTableModel in order for me to make an uneditable table.
import javax.swing.*;
import javax.swing.table.*;
public class TablePanel extends JTable
{
//private int ROWS = 3;
//private int COLUMNS = 2;
private final String [][] data = {{"Jan", "31"}, {"Feb","28"},{"Mar","31"}};
private final String[] headers = {"Month", "Days"};
JTable table;
public TablePanel()
{
table = new JTable(new MyTableModel(data, headers));
JTableHeader header = table.getTableHeader();
//header.setBackground(Color.blue);
setVisible(true);
}
private class MyTableModel extends AbstractTableModel
{
private String[] columnNames = {"Month", "Days"};
private String[][] data = {{"Jan", "31"}, {"Feb","28"},{"Mar","31"}};
public MyTableModel(String [][] data, String [] header)
{
int rows, cols, rowCounter, colCounter;
rows = getRowCount();
cols = getColumnCount();
for (rowCounter=0; rowCounter < rows; rowCounter++)
{
for (colCounter=0; colCounter < cols; colCounter++)
{
setValueAt(data[rowCounter][colCounter],rowCounter,colCounter);
}
}
}
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return data[row][col];
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col)
{
return false;
}
}
}
As you can see the abstract methods are overridden in the private class. Can anyone help me to show me why my JTable is not showing up?
Upvotes: 0
Views: 14869
Reputation: 109813
read tutorial How to Use Tables
put JTable to the JScrollPane
don't forget to put JScrollPane
to the JPanel by using proper LayoutManager, I can't see there that you added JTable
to the JPanel
, not sure if new GridLayout(3,2)
could be proper LayoutManager
for JTable view
Upvotes: 4