Problem in Row and column.

Hi all,

This is the output for this snippet.

oneTwo> Column header

AB -->Row datas.

CD

EF

I am missing "G" in the above table.

What should i to get the output like this?

oneTwo> Column header

AB -->Row datas.

CD

EF

G

File: DataFileTable .java

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

import java.io.*;

import java.util.*;

public class DataFileTable extends JPanel {

public DataFileTable() {

JTable table;

DataFileTableModel model;

Font f;

f = new Font("SanSerif",Font.PLAIN,24);

setFont(f);

setLayout(new BorderLayout());

model = new DataFileTableModel();

table = new JTable();

table.setModel(model);

table.createDefaultColumnsFromModel();

JScrollPane scrollpane = new JScrollPane(table);

add(scrollpane);

}

public Dimension getPreferredSize(){

return new Dimension(400, 300);

}

public static void main(String s[]) {

JFrame frame = new JFrame("Data File Table");

DataFileTable panel;

panel = new DataFileTable();

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

frame.setForeground(Color.black);

frame.setBackground(Color.lightGray);

frame.getContentPane().add(panel,"Center");

frame.setSize(panel.getPreferredSize());

frame.setVisible(true);

frame.addWindowListener(new WindowCloser());

}

}

class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent e) {

Window win = e.getWindow();

win.setVisible(false);

System.exit(0);

}

}

File:DataFileTableModel.java

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.event.*;

import java.io.*;

import java.util.*;

public class DataFileTableModel extends AbstractTableModel {

protected Vector data;

protected Vector columnNames ;

public DataFileTableModel(){

initVectors();

}

public void initVectors() {

String aLine ;

data = new Vector();

columnNames = new Vector();

columnNames.addElement("one");

columnNames.addElement("two");

data.addElement("A");

data.addElement("B");

data.addElement("C");

data.addElement("D");

data.addElement("E");

data.addElement("F");

data.addElement("G");

}

public int getRowCount() {

return data.size() / getColumnCount();

}

public int getColumnCount(){

return columnNames.size();

}

public String getColumnName(int columnIndex) {

String colName = "";

if (columnIndex <= getColumnCount())

colName = (String)columnNames.elementAt(columnIndex);

return colName;

}

public Class getColumnClass(int columnIndex){

return String.class;

}

public boolean isCellEditable(int rowIndex, int columnIndex) {

return false;

}

public Object getValueAt(int rowIndex, int columnIndex) {

return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);

}

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

return;

}

}

[3340 byte] By [hack_javaa] at [2007-10-3 9:42:39]
# 1

When posting code, it is a good idea to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url]

so the code retains its original formatting.

That said, your problem is public int getRowCount() {

return data.size() / getColumnCount();

}

7 / 2 = 3 for ints. You could probably change it to

public int getRowCount() {

int size = data.size();

return size / getColumnCount() + ( size & 1 );

}

for this particular example.

JayDSa at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thx JayDs.

My problem is not solved.

It is giving the following error

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9 >= 9

at java.util.Vector.elementAt(Vector.java:432)

at DataFileTableModel.getValueAt(DataFileTableModel.java:63)

at javax.swing.JTable.getValueAt(JTable.java:1852)

at javax.swing.JTable.prepareRenderer(JTable.java:3902)

at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1645)

at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1547)

hack_javaa at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...
# 3

Here I would guess your problem is with

public Object getValueAt(int rowIndex, int columnIndex) {

return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);

}

Even though you only have seven items in your data Vector, the table will

still try to populate all the cells in the number of rows you have. So, you

need to check if the index is greater than the Vector size and return an

empty string in that case. Something like

public Object getValueAt(int rowIndex, int columnIndex) {

int index = rowIndex * getColumnCount() + columnIndex;

return index < data.size() ? data.elementAt( index ) : "";

}

JayDSa at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...
# 4

Your usage of the table is incorrect. You are trying to using a single Vector to may to a 2-Dimensional table. Why are you doing this? What is your real requirement? To use the table correctly you should be doing something like:

DefaultTableModel model = new DefaultTableModel(0, 2);

model.add( {"A", "B"} );

model.add( {"C", "D"} );

model.add( {"E", "F"} );

model.add( {"G", ""} );

JTable table = new JTable( model );

If you want to use a component for displaying lists then use a JList. You can tell the list to wrap after two items. [url http://java.sun.com/docs/books/tutorial/uiswing/components/list.html]How to Use Lists[/url].

camickra at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...
# 5
hi jayDs,I am passing empty strings already but user can able to select that cell.But user is not suppose to select that emply cell.
hack_javaa at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...
# 6
hi camickr,but the last cell could able to selected by user if u r passing empty string.But it shlouldn't be the case.Thanks for suggestions
hack_javaa at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...
# 7
> but the last cell could able to selected by user Which is why you should be using a JList.
camickra at 2007-7-15 4:58:52 > top of Java-index,Desktop,Core GUI APIs...