JTable getColumnName not working with numbered rows

you know when you read through the same code for a day and can't find the problem but someone who has never seen it before can find the problem in a second? i've got that going on right now.

I saw a thread, http://forum.java.sun.com/thread.jspa?forumID=57&threadID=639189 , where rows numbers are neatly added to a JTable by camickr, after I implemented this I can't get my data or ColumnHeaders to line up with the corrent columns. I have three columns, { Integer, Integer, JComboBox } it's kind of an old post but I was just wondering if any one else had run into the problem.

Also when I insert a new row { Integer, Integer, "Combo Option" } it is inserted into the table as { Integer, "Combo Option", Integer }, this isn't a real high priority but any help would be appreciated, thanks.

[818 byte] By [cwalshea] at [2007-10-3 8:44:17]
# 1

> but someone who has never seen it (the code) before can find the problem in a second?

That assumes of course that there is actually code to look at.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi,

How to pass parameter to the applet from jsp.

Rightnow i am using <jsp:param> attribute to pass the value to the applet, but its not working.

Please guide me to come out of this issue.

This is my code:

Pie1.Java

--

public class Pie1 extends Applet

{

Label inputlabel;

TextField input1, input2, input3, input4;

Button b1;

int sum=0, counter=0;

String num1[] = new String[100];

int num2[] = new int[100];

int number[] = new int[100];

int degrees[] = new int[100];

int i=0;

String dept;

public void init()

{

System.out.println("--inside init()--");

try

{

System.out.println("1");

System.out.println("dept value in init() is "+dept);

maniss();

System.out.println("2");

}

catch(Exception ex)

{

}

}

public void maniss() throws SQLException,ClassNotFoundException

{

try

{

System.out.println("--inside maniss()--");

System.out.println("dept value in maniss() is "+dept);

number[0]=2;

number[1]=2;

number[2]=3;

number[3]=2;

sum=number[0]+number[1]

+number[2]+number[3];

sum=number[0]+number[1]+number[2];

degrees[0]=number[0]*360/sum;

degrees[1]=number[1]*360/sum;

degrees[2]=number[2]*360/sum;

degrees[3]=360-degrees[0]-degrees[1]-degrees[2];

degrees[2]=360-degrees[0]-degrees[1];

repaint();

}

catch(Exception ex)

{

System.out.println("Exception in Pie1.java is "+ex);

}

}

public void paint(Graphics g)

{

System.out.println("--inside paint()--");

g.setColor(Color.red);

System.out.println("1");

System.out.println("passed dept value is "+dept);

g.drawString(dept,60,60);

g.fillArc(40,60,200,200,0,degrees[0]);

g.setColor(Color.green);

g.fillArc(40,60,200,200,

degrees[0], degrees[1]);

g.setColor(Color.blue);

g.fillArc(40,60,200,200,

degrees[0]+degrees[1],

degrees[2]);

g.setColor(Color.yellow);

g.fillArc(40,60,200,200,

degrees[0]+degrees[1]+degrees[2],

degrees[3]);

}

}

Pie1.Jsp

<jsp:plugin type="applet" code="Pie1.class" codeBase="SampleApplets" width="400" height="400>

<jsp:params>

<jsp:param name="dept" value="saurav"/>

</jsp:params>

</jsp:plugin>

My intention is to pass the value for 'dept', i programed in such a way but its not working, help me!

SauravMania at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...
# 3
camickr, seeing how it was your example I thought you might have an idea of what I was talking about.
cwalshea at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...
# 4
> camickr, seeing how it was your example I thought you might have an idea of what I was talking about. 1) what about other people using the forum2) my example works. I am not a mind reader. I don't know what you have changed.
camickra at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...
# 5

I tried to shorten this up as much as possible, sorry but it is still kind of big:

//Main class

import java.awt.*;

import java.awt.event.*;

public class TableProblem extends javax.swing.JFrame

{

BoreDataJTable table;

javax.swing.JScrollPane jsp;

public TableProblem()

{

BoreDataDefaultTableModel model = new BoreDataDefaultTableModel( 0, 3 );

table = new BoreDataJTable( model );

table.getTableHeader().setReorderingAllowed( false );

jsp = new javax.swing.JScrollPane( table );

jsp.setViewportView( table );

this.setContentPane( jsp );

}

public static void main(String args[])

{

System.out.println("Starting App");

TableProblem f = new TableProblem();

f.setSize(300,100);

f.show();

}

}

//JTable class

import javax.swing.*;

import javax.swing.table.*;

import java.awt.Component;

public class BoreDataJTable extends JTable

{

public BoreDataJTable( TableModel tm )

{

super( tm );

setAutoCreateColumnsFromModel( false );

setAutoscrolls( false );

addColumn( new TableColumn() );

getColumnModel().getColumn(0).setCellRenderer( this.getTableHeader().getDefaultRenderer() );

getColumnModel().getColumn(0).setPreferredWidth( 25 );

getColumnModel().getColumn(0).setCellEditor( null );

getColumnModel().getColumn(0).setResizable( false );

getColumnModel().getColumn(1).sizeWidthToFit();

getColumnModel().getColumn(2).sizeWidthToFit();

setPreferredScrollableViewportSize( getPreferredSize() );

initGroundColumn( this.getColumnModel().getColumn(3) );

}

public boolean isCellEditable(int row, int column)

{

if( column < 3 )

return false;

else

return true;

}

public Object getValueAt( int row, int col )

{

if( col == 0)

return new Integer( row + 1 );

else

{

DefaultTableModel model = ( ( DefaultTableModel )(

this.getModel() ) );

return model.getValueAt( row, col - 1 );

}

}

public void initGroundColumn( TableColumn groundColumn )

{

JComboBox comboBox = new JComboBox();

comboBox.addItem( "Select" );

comboBox.addItem( "Top Soil" );

comboBox.addItem( "Clay" );

comboBox.addItem( "Sand" );

comboBox.addItem( "Gravel" );

comboBox.addItem( "Pit Run" );

comboBox.addItem( "Sand Stone" );

comboBox.addItem( "Bed Rock" );

groundColumn.setCellEditor(new DefaultCellEditor(comboBox));

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();

renderer.setToolTipText("Select Ground Type");

groundColumn.setCellRenderer(renderer);

}

}

//DefaultTableModel class

import javax.swing.table.*;

import javax.swing.*;

public class BoreDataDefaultTableModel extends DefaultTableModel

{

private final Object[] emptyRow = { new Integer(0), new Integer(0), "Select" };

private final String[] colNames = { null, "Depth (cm)", "Angel (%)", "Ground" };

int cols;

int rows;

public BoreDataDefaultTableModel( int rows, int cols )

{

super( rows, cols );

this.rows = rows;

this.cols = cols;

addRow( emptyRow );

}

public String getColumnName( int col )

{

return colNames[col];

}

}

The problem is that when the JComboBox is changed, it changes the value in the "Depth" to the the JComboBox's value, also it doesn't correctly display the ColumnHeader for the JComboBox's column

cwalshea at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...
# 6

solved header problems by putting this in the JTable constructor

getColumnModel().getColumn(0).setHeaderValue( null );

getColumnModel().getColumn(1).setHeaderValue( "Depth (cm)" );

getColumnModel().getColumn(2).setHeaderValue( "Angle (%)" );

getColumnModel().getColumn(3).setHeaderValue( "Ground" );

cwalshea at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...
# 7

Your code is nothing like the code in the example you reference earlier. The line numbers are completely independent of the main table. So you build your table normally and then if you want to add line numbers to the scrollpane you use the following two lines of code:

JTable lineTable = new LineNumberTable( table );

scrollPane.setRowHeaderView( lineTable );

One other general tip. You should use " " (note the single blank character), not "null" for an empty column name.

camickra at 2007-7-15 3:53:04 > top of Java-index,Desktop,Core GUI APIs...