wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How adding new row record in table??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How adding new row record in table???" Watch "How adding new row record in table???" New topic
Author

How adding new row record in table???

mar yam
Greenhorn

Joined: May 13, 2005
Posts: 4
from JTable,how we adding new row record in table??

_______________________
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionListener;
import javax.swing.event.CellEditorListener;

public class MyDictionry extends JFrame implements ActionListener
{
private JButton adding = new JButton("ADD");
private JButton exit = new JButton("Exit");
private JButton edit = new JButton("Edit");
private JButton delete = new JButton("Delete");
private JButton game = new JButton("Game");

JTable table;
JPanel southPanel;
DefaultCellEditor singleclick ;
DefaultTableModel model;
public MyDictionry() {
super("Personal Dictionry");
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable (model);
Container container = getContentPane();
JPanel southPanel = new JPanel();




container.setLayout(new GridLayout(1,5,6,7));
container.setLayout(new FlowLayout()) ;

String[] columnNames = {"Keywords","Meaning","Synonym","Antonym","Context"};
String[][] data =new String[90][5];
table = new JTable(data, columnNames);

JScrollPane obj=new JScrollPane(table);
container.add(obj);


table.setBackground(Color.white);
table.setForeground(Color.BLACK);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setValueAt("test", 0, 0);
table.setValueAt("take exam", 0, 1);
int index = table.getSelectedRow() ;


southPanel.add(adding);
adding.addActionListener(this);



southPanel.add(edit);
edit.addActionListener(this);

southPanel.add(delete);
delete.addActionListener(this) ;

southPanel.add(game);

southPanel.add(exit);

exit.addActionListener(this);

container.add(southPanel,BorderLayout.SOUTH);
setSize(550, 500);
setVisible(true);

}
public void actionPerformed(ActionEvent e) {
Add ad;

int index = table.getSelectedRow() ;
if (e.getSource() ==exit ){
System.exit(0) ;
}
else if (e.getSource() == adding){
ad= new Add(model);
}


}

public static void main(String[] args) {
MyDictionry m = new MyDictionry();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


}
*****************************
******************************
public class Add extends JFrame {

private JTable table;

private JTextField key, mea, syn, ant, con;
private JButton k = new JButton("ADD");
private JButton c = new JButton("Cancel");

private DefaultTableModel model;
public Add(DefaultTableModel model) {
super("Add new record");

Container cont = getContentPane();

JTable table = new JTable(model);

cont.setLayout(new FlowLayout());
cont.setBackground(Color.WHITE);
k.addActionListener(
new ActionListener() {

// handle button event
public void actionPerformed(ActionEvent event) {
MyDictionry d;
if (event.getSource() == k) { //add textfiled on the table with new record
d = new MyDictionry();

}
}
}
);
cont.add(new JLabel("Keyword", SwingConstants.RIGHT));
key = new JTextField(10);
cont.add(key);
cont.add(new JLabel("Meaning", SwingConstants.RIGHT));
mea = new JTextField(10);
cont.add(mea);
cont.add(new JLabel("Synonym", SwingConstants.RIGHT));
syn = new JTextField(10);
cont.add(syn);
cont.add(new JLabel("Antonym", SwingConstants.RIGHT));
ant = new JTextField(10);
cont.add(ant);
cont.add(new JLabel("Context", SwingConstants.RIGHT));
con = new JTextField(10);
cont.add(con);
c.addActionListener(new ActionListener() {

// handle button event
public void actionPerformed(ActionEvent event) {
if (event.getSource() == c) {
System.exit(0);
}

}

//
}
);

cont.add(k);

JTextField text[] = {
key, mea, syn, ant, con};

cont.add(c);

}
}


[ May 13, 2005: Message edited by: mar yam ]
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
You have two table= statements, take out the second one and put its arguments in the new model call.


The nice thing about Standards is that there are so many to choose from!
mar yam
Greenhorn

Joined: May 13, 2005
Posts: 4
thanks >>> Eddie Vanda ,,,
I make some correction for the code and its run ok but the new record have empty!!!
can you help me PLZ...
*********************
import java.io.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.GridLayout;
import java.lang.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;
import javax.swing.event.TableModelListener;
import javax.swing.BorderFactory;
import javax.swing.table.TableModel;

public class MyDictionry extends JFrame implements ActionListener
{
private JButton adding = new JButton("ADD");
private JButton exit = new JButton("Exit");
private JButton edit = new JButton("Edit");
private JButton delete = new JButton("Delete");
private JButton game = new JButton("Game");
private DefaultTableModel model;
private JTable table;
private Vector header = new Vector();
private Object[] columnNames = {
"Keywords", "Meaning", "Synonym", "Antonym", "Context"};
private int index;

public MyDictionry() {

super("Personal Dictionry");


Container container = getContentPane();

JPanel southPanel = new JPanel();

container.setLayout(new GridLayout(1, 5, 6, 7));
container.setLayout(new FlowLayout());
model = new DefaultTableModel(columnNames, 0);
initVectors();
table = new JTable(model);
JScrollPane obj = new JScrollPane(table);
container.add(obj);

table.setBackground(Color.white);
table.setForeground(Color.BLACK);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
String []st= {"word1","word2","word3","word4","word5"};
model.addRow(st) ;

southPanel.add(adding);
adding.addActionListener(this);

southPanel.add(edit);
edit.addActionListener(this);
////////////////////////////////////////////////////////////////////////

southPanel.add(delete);
delete.addActionListener(this);

//////////////////////////////////////////////////////////////////////

southPanel.add(game);
game.addActionListener(this);
///////////////////////////////////////////////////////////////////////

southPanel.add(exit);
exit.addActionListener(this);
///**************************************************************
container.add(southPanel, BorderLayout.SOUTH);

}
public void initVectors() {
String aLine ;
Vector data = new Vector();


try {
FileInputStream fin = new FileInputStream("Dword.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fin));

// extract data
while ((aLine = br.readLine()) != null)
{
StringTokenizer st2 = new StringTokenizer(aLine, ",");
while(st2.hasMoreTokens())
{
data.addElement(st2.nextToken());


}
}

br.close();
}
catch (Exception e) {
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent e) {
AddClass q;
if (e.getSource() == adding){
q=new AddClass(model);
}
else if (e.getSource() == game){
new MyGame();
}
else if (e.getSource() ==delete){
index = table.getSelectedRow();
model.removeRow(index) ;

}
else if (e.getSource() == exit){

System.exit(0);
}


}


///////////////////////////////////////////////////////////////////
public static void main(String[] args) {
MyDictionry m = new MyDictionry();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(550, 500);
m.setVisible(true);
}
}
**************************************************************

public class AddClass extends JFrame implements ActionListener {

private JTextField key = new JTextField(20);
private JTextField mea = new JTextField(20);
private JTextField syn = new JTextField(20);
private JTextField ant = new JTextField(20);
private JTextField con = new JTextField(20);


private JButton button1 = new JButton("ADD");
private JButton button2= new JButton("Cancel");


private DefaultTableModel model;

public AddClass(DefaultTableModel model) {

super("Add new record");
this.model = model;

Container cont = getContentPane();
cont.setLayout(new GridLayout());

cont.setLayout(new FlowLayout());
cont.setBackground(Color.WHITE);

cont.add(new JLabel ("Keyword",SwingConstants.RIGHT ));
key= new JTextField(10);
cont.add(key) ;
cont.add(new JLabel ("Meaning",SwingConstants.RIGHT ));
mea= new JTextField(10);
cont.add(mea) ;
cont.add(new JLabel ("Synonym",SwingConstants.RIGHT ));
syn= new JTextField(10);
cont.add(syn) ;
cont.add(new JLabel ("Antonym",SwingConstants.RIGHT ));
ant= new JTextField(10);
cont.add(ant) ;
cont.add(new JLabel ("Context",SwingConstants.RIGHT ));
con= new JTextField(10);
cont.add(con) ;

cont.add(button2);
cont.add(button1);

button1.addActionListener(this);
button2.addActionListener(this);



JPanel main = new JPanel();
main.setLayout(new GridLayout(7, 12, 5, 5));

cont.add(main, BorderLayout.SOUTH);
setVisible(true) ;
setSize(550,500) ;

}
public void actionPerformed(ActionEvent event) {

if (event.getSource() == button1)

addrecord();
else
this.dispose();

}
public void addrecord(){
Collection coo = new Collection();
Word record = new Word();

String file1[]={record.getk() ,record.getm() ,record.gets() ,record.geta() ,record.getc() };
String file2[] = new String[file1.length];


record.setk(key.getText() +",");
record.setm(mea.getText() +",");
record.sets(syn.getText() +",");
record.seta(ant.getText() +",");
record.setc(con.getText() +",");
coo.addword(record) ;

model.addRow(file2);
this.dispose();
}
}
*****************************************************

public class Collection {
private static Vector m =new Vector();
public Collection(){

}
public void addword(Word record) {
m.add(record);
}

public void removeMeeting(Word record) {
m.remove(record);
}

public boolean isEmpty() {
return m.isEmpty();
}
public Vector run()
{
return m;
}

}
***********************************************

public class Word {
private String KEYWORD ;
private String MEANING;
private String SYNONYM;
private String ANTONYM;
private String CONTEXT;
int Account = 20;
public Word() {

}
public Word(String k,String m,String s,String a,String c){
setk(k);
setm( m);
sets( s);
seta(a);
setc( c);
}
public String getk() {
return KEYWORD;
}
public void setk(String k){
KEYWORD = k;
}


public String getm() {
return MEANING;
}

public String gets() {
return SYNONYM;
}
public String geta() {
return ANTONYM;
}
public String getc() {
return CONTEXT;
}

public void setm(String m){
MEANING = m;
}
public void sets(String s){
SYNONYM=s;


}
public void seta(String a){
ANTONYM = a;
}
public void setc(String c){
CONTEXT = c;
}
public void setAccount(int record){
Account= record;

}
public String toString()
{
return KEYWORD + MEANING + SYNONYM + ANTONYM + CONTEXT;
}


}

*****************************
And I have two class for (StartPlayand) and (MyGame) but its no problems for this class..
mar yam
Greenhorn

Joined: May 13, 2005
Posts: 4
NOBADY help me!!!

really I need help... just try this code and I need some mothed for add record by vector

PLZ,,,PLZ
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Mar

Swap over the group of code starting with


with:



That way you get the info from the textfields before you plug it into the array.

Then change the reference to file2 into file1 because file2 has only elements without info.

hth
Ed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How adding new row record in table???