Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

can u figure out the error plz

  Asked By: Lucas    Date: Feb 01    Category: Java    Views: 707
  

In the following code i am displaying a JTable and i able to view the
rows but not able to view the column names, can u plz help me whats
the error is.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.event.*;
import java.io.*;
public class JTableExample extends JFrame
{
JTable table;
MyTableModel tablemodel;
ListSelectionModel listselection;
public void JTableExample()
{
String colNames[] =
{ "CompanyName", "FirstQ", "SecondQ", "ThirdQ","FourthQ"};
Object[][] tableRowData =
{{"IBM" ,"1.00" ,"2.00" , "3.00" , "4.00"},
{"SUN" ,"1.00" , "2.00" , "3.00" , "4.00"},
{"MS","1.00" , "2.00" , "3.00" , "4.00"},
{"Oracle","1.00" , "2.00" , "3.00" , "4.00"}};
tablemodel = new MyTableModel(tableRowData,colNames);
table = new JTable(tablemodel);
listselection = table.getSelectionModel();
listselection.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged
(ListSelectionEvent e)
{
ListSelectionModel
model = (ListSelectionModel) e.getSource();
model =
(ListSelectionModel)e.getSource();
TableModel model1 =
(TableModel) table.getModel();
String buffer= "";
if(!
(model.isSelectionEmpty()))
{

int minIndex =
model.getMinSelectionIndex();
int maxIndex =
model.getMaxSelectionIndex();
for(int i=minIndex;
i<=maxIndex; i++)
{
if(model.isSelectedIndex(i))
{
buffer= buffer + (String)
model1.getValueAt(i,0)+","+(String)model1.getValueAt(i,1)+","+(String)
model1.getValueAt(i,2)+","+(String)model1.getValueAt(i,3)+","+(String)
model1.getValueAt(i,4)+",";
}
}
JOptionPane.showMessageDialog
(null,buffer,"",1);
}
else
{
JOptionPane.showMessageDialog(null,"ERROR, No
Rows Selected","Invalid Selection Error",JOptionPane.ERROR_MESSAGE);
}

}}
);
getContentPane().add(table);
setSize(400,500);
setVisible(true);

}
public static void main(String[] args)
{
JTableExample view = new JTableExample();
view.JTableExample();



}


class MyTableModel extends AbstractTableModel {
private String[] columnNames = null;
private Object[][] rowData = null;
public MyTableModel(Object[][] rowData, String[] columnNames)
{
this.rowData = rowData;
this.columnNames = columnNames;
} //end constructor
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
if (this.rowData == null)
return 0;
return this.rowData.length;
}
public boolean isCellEditable(int row, int column) {
return false;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return this.rowData[row][col];

}
public void setValueAt(String value, int row, int col)
{
this.rowData[row][col] = value;


}
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on can u figure out the error plz Or get search suggestion and latest updates.




Tagged: