can't show JTable
I'm having small problem (I guess) of showing the JTable panel. I have class contains Object array with:
public class Item
{
String itemDesc = "";
float price = 0;
private itemType enmItemType;
Object[][] data = {{itemDesc, enmItemType , new Float(price)}};
.
.
.
.
}
here is the Table class contains the JTable:
class Table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private JButton update_Button;
// Constructor of main frame
public Table()
{
// Set the frame characteristics
setTitle("Add new item" );
setSize(300, 200);
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create columns names
S开发者_如何学JAVAtring columnNames[] = {"Item Description", "Item Type", "Item Price"};
// Create some data
Object dataValues[][] ;
Item itm = new Item();
dataValues = itm.data;
// Create a new table instance
table = new JTable( dataValues, columnNames );
////////////////////////////
JComboBox itemTypeCombobox = new JComboBox();
TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setCellEditor(new DefaultCellEditor(itemTypeCombobox));
////////////////////////////
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
JButton button = new JButton("Add Item");
topPanel.add( button, BorderLayout.SOUTH );
}
}
The main program is:
public static void main(String[] args)
{
Menu m = new Menu();
m.chooseMenu();
// Create an instance of the test application
Table mainFrame = new Table();
mainFrame.setVisible( true );
}
I did not receive any error/warning, but still, I don't see any table. Can someone direct me what causing the problem?
Thanks.
I cant tell what has gone wrong. But I changed your code a bit (since it had compile time errors)
It works fine for me. Following is the screenshot
public class Item{
String itemDesc = "";
float price = 0;
Object[][] data = {{"test","test","test"},
{"test","test","test"},
{"test","test","test"},
{"test","test","test"}};
}
Your main table class
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Menu;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Table extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
private JButton update_Button;
// Constructor of main frame
public Table() {
// Set the frame characteristics
setTitle("Add new item");
setSize(300, 200);
setBackground(Color.gray);
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
// Create columns names
String columnNames[] = { "Item Description", "Item Type", "Item Price" };
// Create some data
Object dataValues[][];
Item itm = new Item();
dataValues = itm.data;
// Create a new table instance
table = new JTable(dataValues, columnNames);
// //////////////////////////
JComboBox itemTypeCombobox = new JComboBox();
TableColumn column1 = table.getColumnModel().getColumn(1);
column1.setCellEditor(new DefaultCellEditor(itemTypeCombobox));
// //////////////////////////
// Add the table to a scrolling pane
scrollPane = new JScrollPane(table);
topPanel.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Add Item");
topPanel.add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
Menu m = new Menu();
// Create an instance of the test application
Table mainFrame = new Table();
mainFrame.setVisible(true);
}
}
for example if I enter new item, then I need to enter parameters of Description (String), type (Enum) and price (Float)...
To add a new row of data you need to use the addRow(...) method of the DefaultTableModel.
All updates should be done to the model, not the Array used to create the model.
精彩评论