The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem in JTable.. pls help me..

3 replies on 1 page. Most recent reply: Apr 17, 2004 12:17 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Betsy

Posts: 2
Nickname: fiddy
Registered: Apr, 2004

Problem in JTable.. pls help me.. Posted: Apr 16, 2004 5:45 PM
Reply to this message Reply
Advertisement
i've to retrieve the data from the textfile and display it into the JTable with two columns which name as "Expense" and "Amount". i already did one JTable but the outcome is not wat i wan..my Jtable table now is just one column with all the data.. i noe i need to have 2 dimensional array.. but i not sure where sld i place that.. pls help me.. thanks alot..
this is my coding so far:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;

class ViewExpense extends JFrame {

private JTable table;
private DefaultTableModel model;

public ViewExpense() {

String aLine ;
Vector colExpense = new Vector();
Vector colAmt = new Vector();
Vector data = new Vector();

try {


File inputFile = new File("Expenses.txt");

FileInputStream in = new FileInputStream(inputFile);
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);

// extract column Expense
StringTokenizer tokenizer = new StringTokenizer(br.readLine(), "|");
while( tokenizer.hasMoreTokens() ) {
colExpense.addElement(tokenizer.nextToken());
}

// extract data
while ((aLine = br.readLine()) != null) {
StringTokenizer tokenizer1 = new StringTokenizer(aLine, "|");
Vector row = new Vector();
while(tokenizer1.hasMoreTokens()) {
row.addElement(tokenizer1.nextToken());
}
data.addElement( row );
}
br.close();
}
catch (Exception e) {
e.printStackTrace();
}

// Create table using the DefaultTableModel
model = new DefaultTableModel(data, colExpense);
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );

// Empty Row button
JButton button1 = new JButton( "Add Empty Row" );
buttonPanel.add( button1 );
button1.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {
model.addRow( new Vector() );
}
});

// Save table to file button
JButton button2 = new JButton( "Save Table to File" );
buttonPanel.add( button2 );
button2.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e) {

// Finish editing the current cell before saving
if ( table.isEditing() ) {
int row = table.getEditingRow();
int col = table.getEditingColumn();
table.getCellEditor(row, col).stopCellEditing();
}

int rows = table.getRowCount();
int columns = table.getColumnCount();

// Write out the Column Headers
TableColumnModel header = table.getColumnModel();
for (int k = 0; k < columns; k++) {
TableColumn column = header.getColumn(k);
String value = (String)column.getHeaderValue();
System.out.print( value );
System.out.print( "|" );
}

System.out.println();

// Write out the table data
for (int j = 0; j < rows; j++) {
for (int k = 0; k < columns; k++) {
String value = (String)table.getValueAt(j, k);
System.out.print( value );
System.out.print( "|" );
}
System.out.println();
}
}
});
}

public static void main(String[] args) {
ViewExpense frame = new ViewExpense();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Problem in JTable.. pls help me.. Posted: Apr 16, 2004 10:15 PM
Reply to this message Reply
How is the data in ur Expenses.txt file.

I created a file expenses.txt and ran your programme and it worked fine.

The data in the expenses file was like

Expenses|Amount
10|20
30|40

Betsy

Posts: 2
Nickname: fiddy
Registered: Apr, 2004

Re: Problem in JTable.. pls help me.. Posted: Apr 16, 2004 11:50 PM
Reply to this message Reply
thanks for ur reply..
my text files is like..
aaa;10
ddd;20
how do ur text files look like??

thanks alot..

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Problem in JTable.. pls help me.. Posted: Apr 17, 2004 12:17 AM
Reply to this message Reply
So the problem is that in the file u are sepeareting the items with semicolon (;) but in programme u are using | .

Change ; with | in file or | with ; in programme

Flat View: This topic has 3 replies on 1 page
Topic: Problem with configuring Tomcat 5 Previous Topic   Next Topic Topic: Java Mail problem in WAS 5.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use