• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextfield data in JTable row

 
tadi raja
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,

My problem in the code is that if i enter text in textfields and click on the button we get the data from textfield and displayed it on table row,can you please suggest me how it is possible ??

Here is the code ....
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand() == "ABC")
{
String st = _drug1.getText();
String st1 = _vehicle1.getText();
String st2 = _drug2.getText();
String st3 = _vehicle2.getText();
String st4 = _solution.getText();
}
}*****************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import java.util.*;

public class CustomTableModel extends AbstractTableModel
{
final String[] columnNames = {"Study","Drug1","Vehicle1","Drug2","Vehicle2","Solution","Finding"};

Vector rowVect = new Vector();

public int getColumnCount()
{
return columnNames.length;
}

public int getRowCount()
{
return rowVect.size();
}

public String getColumnName(int col)
{
return columnNames[col];
}

public Object getValueAt(int row, int col)
{
String[] rowData = (String[])rowVect.elementAt(row);
return rowData[col];
}

public boolean isCellEditable(int row, int col)
{
return false;
}

public void addRow(String[] str)
{
//str =
// str = GetDetails.str;
rowVect.addElement(str);
fireTableDataChanged();
//fireTableRowsInserted((rowVect.size()) - 1, rowVect.size());
}

public void removeRow(int row)
{
rowVect.remove(row);
fireTableDataChanged();
}

}

Thanks in advance
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have mentioned this before too. Please use CODE tags to make it easier for others to understand your code.

Instead of extending AbstractTableModel, use the DefaultTableModel.
It is two convenient methods which will be useful for you.


Also, if you haven't done so already, take a look at the JTable tutorial which can be found here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic