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;
}
}

