Refreshing JTable
I am still having many of the same problems as before and am looking for more help. I am beginning to wonder if I created my JTable correctly.
First of all, my program receives an integer from another class to define the number of rows for the table. The user will then enter in their numbers into the table. However, I cannot get the fireTableCellUpdated() method to work as the same with others inherited by AbstractTable Model. The numbers entered by the user turn back to 0 and are not refreshed.
Please check my coding methods, I am totally lost! I know there is a lot of code, but I know my problem is within the second class. That is where I need help.
import javax.swing.*;
import javax.swing.JPanel.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.AbstractTableModel;
publicclass RegressInputextends JFrame
{
private JComboBox listJComboBox;
private JButton answerButton;
private JPanel textFieldPanel;
private CardLayout cardSelector;/
private JPanel cardDeck;
public RegressInput(String title)
{
super(title);
//CREATE A CONTAINER
Container container = getContentPane();
cardSelector =new CardLayout();
cardDeck =new JPanel();
cardDeck.setLayout(cardSelector);
Label question =new Label("How many PAIRS of data would you like to enter?");
answerButton =new JButton("OK");
listJComboBox =new JComboBox( getArray() );
listJComboBox.setMaximumRowCount(10);
//BUILD CARD DECK
JPanel comboBoxCard =new JPanel();
comboBoxCard.add(question);
comboBoxCard.add(listJComboBox);
comboBoxCard.add(answerButton); /
cardDeck.add(comboBoxCard,"Step 1");
container.add(cardDeck);
//DEFINE BUTTON HANDLER OBJECT
ButtonHandler buttonHandler =new ButtonHandler();
//ADD ACTION LISTENER FOR BUTTONS
answerButton.addActionListener(new ButtonHandler());
}//END RegressInput() CONSTRUCTOR
//METHOD TO CREATE AND RETURN AN ARRAY OF VALUES FOR JComboBox
private String[] getArray()
{
int numbers[] =newint[49];
int number = 2;
//ASSIGN VALUES FROM 2 TO 30 TO numbers[] ARRAY
for(int count=0; count < 49; ++count)
{
numbers[count] = number;
number++;
}//END for LOOP
//CREATE pairs[] ARRAY TO HOLD 30 STRINGS
String pairs[] =new String[49];
//ASSIGN VALUES 1 TO 30 IN STRING ARRAY FOR COMBO BOX
for(int count = 0; count < 49; ++count)
{
pairs[count] ="" + numbers[count];
}//END for LOOP
return pairs;
}//END getArray() METHOD8
//RETURNS THE ITEM SELECTED BY THE USER FROM THE JComboBox
privateint getValue()
{
//ASSIGNS STRING VALUE OF THE JComboBox TO A WRAPPER
Integer v =new Integer((String) listJComboBox.getSelectedItem());
//ASSIGNS WRAPPER VALUE TO int
int value = v.intValue();
//RETURN VALUE OF SELECTED ITEM
return value;
}//END getValue()
//BUTTON EVENT HANDLER CLASS
privateclass ButtonHandlerimplements ActionListener
{
//PROCESS EVENT
publicvoid actionPerformed(ActionEvent e)
{
//WHICH BUTTON CAUSED THE EVENT?
if(e.getSource() == answerButton)
{
Table table =new Table(getValue());
cardDeck.add(table,"Step 2");//ADD SECOND CARD TO DECK
cardSelector.last(cardDeck);
cardDeck.setSize(600,600);
}//END if STATEMENT
}//END actionPerformed()
}//END ButtonHandler CLASS
}//END RegressInput CLASS
My problem is somewhere in this class, and I know the problem is not the definition of the table in the constructor.
import javax.swing.*;//FOR SWING COMPONENT CLASSES
import javax.swing.JPanel.*;
import java.awt.*;//FOR CONTAINER CLASS
import java.awt.event.*;//FOR EVENT HANDLING
import javax.swing.table.AbstractTableModel;
publicclass Tableextends JPanel
{
privateboolean DEBUG =false;
public Table(int value)
{
super(new GridLayout(1,0));
int row = value;
JTable table =new JTable(new InputTable(row));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane =new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
class InputTableextends AbstractTableModel{
privateboolean DEBUG =false;
private String[] columnNames ={"X Values","Y Values"};
privatefinalint COLUMNS = getColumnCount();
privateint[][] data;
public InputTable(int row)
{
int rows = 2;
rows = row;
data =newint[rows][COLUMNS];
}
publicint getColumnCount(){
return columnNames.length;
}
publicint getRowCount(){
return data.length;
}
public String getColumnName(int col){
return columnNames[col];
}
public Integer getValueAt(int row,int col){
return data[row][col];
}
public Class getColumnClass(int c){
return getValueAt(0, c).getClass();
}
publicboolean isCellEditable(int row,int col){
returntrue;
}
publicvoid setValueAt(Integer value,int row,int col){
if (DEBUG){
System.out.println("Setting value at " + row +"," + col
+" to " + value
+" (an instance of "
+ value.getClass() +")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG){
System.out.println("New value of data:");
printDebugData();
}
}
privatevoid printDebugData(){
int numrows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numrows; i++){
System.out.print("row " + i +":");
for (int j=0; j < numCols; j++){
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--");
}
}
}
And finally, main.
import javax.swing.JFrame;
publicclass TestSharpStats
{
publicstaticvoid main(String[] args)
{
//DEFINE FRAME OBJECT
RegressInput window =new RegressInput("Hi");//SETS TITLE BAR
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//CLOSES WINDOW
window.setSize(400, 600);//SET FRAME SIZE
window.setResizable(true);//PREVENTS USER FROM RESIZING WINDOW
window.setVisible(true);//SETS window TO VISIBLE
}//END main()
}//END TestSharpStats CLASS

