开发者

Cannot find symbol error on java?

开发者 https://www.devze.com 2023-02-04 06:58 出处:网络
I am getting this error: symbol: constructor JTable(float[][],java.lang.String[]) location: class javax.swing.JTable

I am getting this error:

   symbol  : constructor JTable(float[][],java.lang.String[])
    location: class javax.swing.JTable
    table = new JTable(dataValues, columnNames );

below is the code

import java.awt.*;
import开发者_Go百科 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.table.*;

public class benchtesting extends JFrame
{

    private     JTabbedPane tabbedPane;
    private     JPanel      panel1;
    private     JPanel      panel2;
    private     JPanel      panel3;
private JTable table;
    private JScrollPane scrollPane;


    public static void main( String args[] )
    {
        benchtesting mainFrame  = new benchtesting();
        mainFrame.setVisible( true );
    }

public benchtesting()
    {

        setSize(280,200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocation(1300,280);
        setTitle("Photoreceptor Analysis");

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the tab pages
        createPage1();
    //  createPage2();
    //  createPage3();

        // Create a tabbed pane
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab( "Table", panel1 );
        tabbedPane.addTab( "Intensity Map", panel2 );
        tabbedPane.addTab( "Notes", panel3 );
        topPanel.add( tabbedPane, BorderLayout.CENTER );
}


    public void createPage1()
    {

panel1 = new JPanel();
        panel1.setLayout( new BorderLayout() );

    float dataValues_all[]= new float[400]; 

BufferedReader inputFile2=null;
BufferedReader inputFile=null;

            try {
    FileReader freader =new FileReader("results.txt");
        inputFile2 = new BufferedReader(freader); 


    String read = "";
    String number ="";

    for (int linenum=0; linenum<400; linenum++) {

    read = inputFile2.readLine();


  if(read != null && read.startsWith("D"))
     {

                number = read.substring(9,15);  
                float real_numbers = Float.parseFloat(number);  

            real_numbers= real_numbers*2817217;
            System.out.println(Math.round(real_numbers)+" cells/mm^2"); 
        dataValues_all[linenum] = real_numbers; 
     } 
         }

         // System.out.println("hmm "+dataValues_all[398]); 

        String columnNames[] = {"1","2","3","4","5","6","7","8","9","10"};
    float dataValues[][]= new float [1][10]; //1 row, 10 coloumns 


dataValues[0][0] = dataValues_all[2];
dataValues[0][1] = dataValues_all[6];
dataValues[0][2] = dataValues_all[10];
dataValues[0][3] = dataValues_all[14];
dataValues[0][4] = dataValues_all[18];
dataValues[0][5] = dataValues_all[22];
dataValues[0][6] = dataValues_all[26];
dataValues[0][7] = dataValues_all[30];
dataValues[0][8] = dataValues_all[34];
dataValues[0][9] = dataValues_all[38];


table = new JTable(dataValues, columnNames );

        scrollPane = new JScrollPane(table);
panel1.add( scrollPane, BorderLayout.CENTER );



      } catch( Exception y ) {  y.printStackTrace();    } 

}}


The constructor of JTable expects the arguments (Object[][] rowData, Object[] columnNames). So when calling that constructor you need to call it with values of that types.

Passing String[] as Object[] is no problem (because a String is an Object, but passing float[][] as Object[][] is not valid (because a float is not an Object).

With Java5 the "boxing" of primitive type (like float) into wrapper objects (like Float) became possible. This means, that Float a = 0.1f; is valid. However boxing is not available for arrays.

Probably the best solution for your problem is to change your variable dataValues to type Float[][].


You can't use a multidimensional array of primitive types. You need to use a multidimensional array of objects.

This is invalid:

float[][] arr;

This is valid:

Float[][] arr;


float is a primitive type, not an object.


See the Javadocs. You probably need to make a Float[][] (the object)


JTable class really does not have constructor that accepts 2 dimensional float array and regular string array. It has constructor

JTable(Object[], String[])

So, just change

float dataValues[][]= new float [1][10];

to

Object dataValues[][]= new Object [1][10];

and enjoy. Say "thanks" to autoboxing...


JTable(Object[][] rowData, Object[] columnNames) 

http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/JTable.html

Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.

As stated already, constructor takes object, you can't pass primitive multidimensional array.


There is no matching constructor for JTable which expects the first parameter to extend Object[][]. float is a primitive type and does not extend Object. Float is an object which does. Use it instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消