Maggie
Maggie

Reputation: 1

JTable object [][] data

I am trying to create a table based on my trial class. When I try to run my table class the table prints with the proper heading, but it does not print anything in the body of the table. How should I fix my for loop so the data enters the table? Do I need to add a parameter to the constructor?

I understand there are more getters and setters than necessary. I was just trying to go about the for loop in different ways.

public class Trial extends JFrame{

static int counter = 0;
int laps = 0;
String lapTime;
double currentTime = 0;
String difference;
int lapsCount = 0;
String[] array1;
double[] array2;
double[] array3;

 public int getLapsCount() {
    return lapsCount;
}

public void setLapsCount(int lapsCount) {
    this.lapsCount = lapsCount;
}

public static int getCounter() {
    return counter;
}

public static void setCounter(int counter) {
    Trial.counter = counter;
}

public int getLaps() {
    return laps;
}

public void setLaps(int laps) {
    this.laps = laps;
}

public String getLapTime() {
    return lapTime;
}

public void setLapTime(String lapTime) {
    this.lapTime = lapTime;
}

public double getCurrentTime() {
    return currentTime;
}

public void setCurrentTime(double currentTime) {
    this.currentTime = currentTime;
}

public String getDifference() {
    return difference;
}

public void setDifference(String difference) {
    this.difference = difference;
}

public String[] getArray1() {
    return array1;
}

public void setArray1(String[] array1) {
    this.array1 = array1;
}

public double[] getArray2() {
    return array2;
}

public void setArray2(double[] array2) {
    this.array2 = array2;
}

public double[] getArray3() {
    return array3;
}

public void setArray3(double[] array3) {
    this.array3 = array3;
}

public static void main(String[] args) {

    //drop down for number of laps
     String lapsString;
     String[] selections = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        lapsString = (String) JOptionPane.showInputDialog(null, 
                                                      "Choose a number of laps:",
                                                      "Lap Counter",
                                                      JOptionPane.INFORMATION_MESSAGE,
                                                      null, /* icon */
                                                      selections,
                                                      "0");
        System.out.printf("Number of laps = \"%s\"\n", lapsString);

    int lapsCount = Integer.parseInt(lapsString);
    String[] array1 = new String[lapsCount+1];
    double[] array2 = new double[lapsCount+1];
    double[] array3 = new double[lapsCount+1];

    // showInputDialog with for lap time
    for (int i = 1; i <= lapsCount; i++){
        String lapTime;
        lapTime = 
            JOptionPane.showInputDialog("Enter lap " + i + " total time.");
        System.out.printf("Lap " + i + " = \"%s\"\n", lapTime);
        array1[i] = lapTime;
        double currentTime = inputConvert(array1[i]);
        array2[i] = currentTime;
        array3[i] = (array2[i] - array2[i-1]);
        counter++;

        //confirm each time 
            int result;
            String[] results = { "Yes", "No", "Cancel" };

            result = JOptionPane.showConfirmDialog(null, 
                                           "Please confirm lap " + i + " is " + lapTime);
            if (result == -1)
                System.out.printf("user closed window\n");
            else if (result == 0)
                System.out.printf("result = %d, user pressed \"%s\"\n", result, results[result]);
            else {
                lapTime = 
                        JOptionPane.showInputDialog("Please re-enter lap " + i);
                System.out.printf("Lap " + i + " = \"%s\"\n", lapTime);
                counter++;
        }
    }
}

    public static double inputConvert(String s) {
        double convert = 0.0;
        int minutes = Integer.parseInt(s.substring(0, 1));
        double seconds = Double.parseDouble(s.substring(2, s.length()));
        minutes = minutes * 60;
        convert = minutes + seconds;
        return convert;

    }

    private static String getDifference(int j, double sign) {
        String difference = "";
        if(j == 0) {
            difference = "---";
        }
        else if(sign > 0.0) {
            difference = "+";
        }
        return difference;
    }

    public static String displayTime(double time) {
        String result = null;
        int minutes = (int) (time/60);
        double seconds = (time % 60);
        seconds = Math.round(seconds*100.0)/100.0;
        if(seconds < 10){
            result = minutes + ":0" + seconds;  
        }
        else{
            result = minutes + ":" + seconds;   
        }

        return result;
    }
 }

This is my table class where the problem arises.

public class table { 

public static void main(String[] args) { 

    EventQueue.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
            new table(); 
            } 
        }); 
}

public table() { 
    JFrame guiFrame = new JFrame(); 

    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    guiFrame.setTitle("Table to record lap times"); 
    guiFrame.setSize(700,200); 

    guiFrame.setLocationRelativeTo(null); 

    JTable table = new JTable(new ExampleTableModel()); 

    table.setAutoCreateRowSorter(true); 

    table.setGridColor(Color.YELLOW); 
    table.setBackground(Color.CYAN); 

    JScrollPane tableScrollPane = new JScrollPane(table); 

    guiFrame.add(tableScrollPane); 
    guiFrame.setVisible(true); 

} 


class ExampleTableModel extends AbstractTableModel{ 

    int laps = 0;
    String lapTime;
    double currentTime = 0;
    String difference;

    Trial t = new Trial();

     //Two arrays used for the table data 
    private String[] columnNames = {"Lap #", "Total Time", "Lap Time" , "Difference" };
    private Object[][] data;

    public ExampleTableModel(){

        data = new Object[laps][4];
        for(int i = 0; i < laps; i++){
            data[i][0] = "Lap " + i;
            data[i][1] = t.getArray1() [i]; 
            data[i][2] = t.getArray2()[i]; 
            data[i][3] = t.getArray3()[i]; 
        }
    }

    @Override public int getRowCount() { 
        return data.length; 
        } 

    @Override public int getColumnCount() { 
        return columnNames.length; 
        } 

    @Override public Object getValueAt(int row, int column) { 
        return data[row][column]; 
        } 


    @Override public String getColumnName(int column) { 
        return columnNames[column]; 
        } 

    @Override public Class getColumnClass(int c) { 
        return getValueAt(0, c).getClass(); 
        } 

    @Override public boolean isCellEditable(int row, int column) { 
        if (column == 0 || column == 1) { 
            return false; 
            } 
        else { 
            return true; 
        } 
      } 
    }
  }    

I made those changes but I need my output to look like this Lap #: Total time Lap time Difference Lap1: 2:10 2:10 ---

I fixed the lap number so it starts at 1. I made these changes in the hopes that it would fix the lap time and difference cells but it is giving me an error.

public ExampleTableModel(Trial trial){

        int laps = trial.getLapsCount();
        data = new Object[laps][4];
        for(int i = 0; i < laps; i++){
            if (i == 0){
                data[i][0] = "Lap " + (i + 1);
                data[i][1] = trial.getArray1() [i + 1]; 
                data[i][2] = trial.getArray2() [i + 1]; 
                data[i][3] = trial.getDifference(i, (trial.getArray3()[i + 1] - trial.getArray3()[i]));
            }

            else {
                data[i][0] = "Lap " + (i + 1);
                data[i][1] = trial.getArray1() [i + 1]; 
                data[i][2] = trial.displayTime(trial.getArray2() [i + 1] - trial.getArray2() [i]); 
                data[i][3] = trial.getDifference(i, (trial.getArray3()[i + 1] - trial.getArray3()[i])) + (trial.getArray3()[i + 1] - trial.getArray3()[i]);

            }
        }
    }                

Upvotes: 0

Views: 327

Answers (1)

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

Edit: combining the code from the Trial.main and the Table.main methods

To make the code from your Trial class easier to use from the Table class, you can make the following changes. This way you can create a Trial object and call the getUserInput to let the user initialize it:

public static void main(String[] args) {
    new Trial().getUserInput();
}

public void getUserInput() {
    // [Code from the old main method...]

    // Use the fields of the class instead of local variables:
    lapsCount = Integer.parseInt(lapsString);
    array1 = new String[lapsCount+1];
    array2 = new double[lapsCount+1];
    array3 = new double[lapsCount+1];

    // [Code from the old main method...]
}

Now you can modify the Table model like this:

public Table() {
    // [...]

    Trial trial = new Trial();
    trial.getUserInput();
    JTable table = new JTable(new ExampleTableModel(trial));

    // [...]
}

And let the ExampleTableModel constructor use a trail parameter (the laps and t fields are no longer used):

public ExampleTableModel(Trial trial){
    int laps = trial.getLapsCount();
    data = new Object[laps][4];
    for(int i = 0; i < laps; i++){
        data[i][0] = "Lap " + i;
        data[i][1] = trial.getArray1()[i];
        data[i][2] = trial.getArray2()[i];
        data[i][3] = trial.getArray3()[i];
    }
}

There was also an issue with the getColumnClass method:

@Override public Class getColumnClass(int column) {
    //return  getValueAt(0, column).getClass();
    return (column <= 1) ? String.class : Double.class;
}

Now the table looks like this:

Screenshot of table

The output was:

Number of laps = "2"
Lap 1 = "2:10"
result = 0, user pressed "Yes"
Lap 2 = "2:20"
result = 0, user pressed "Yes"

Old answer

There seem to be a few issues that prevent the table from being filled:

In the ExampleTableModel class the laps field should be something higher than zero (for the constructor to fill the data array):

class ExampleTableModel extends AbstractTableModel {

    //int laps = 0;
    int laps = 6;

In the constructor of the ExampleTableModel class:

        public ExampleTableModel(){

            data = new Object[laps][4];
            for(int i = 0; i < laps; i++){
                data[i][0] = "Lap " + i;
                data[i][1] = t.getArray1()[i];
                data[i][2] = t.getArray2()[i];
                data[i][3] = t.getArray3()[i];
            }
        }

the calls to the t.getArray1, t.getArray2, and t.getArray3 methods return null. The Trial object appears to have no data yet.

Do you want your program to execute both the code of the Trial.main method and the Table.main method?

Upvotes: 1

Related Questions