KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Best books about JAVA programming:

JAVA JTable Add Row Data

This JApplet demos how to add row data to a JTable dynamically. User input row data in a JTextField,which is delimited by comma. If input data has more columns, extra values will be ignored.

iimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;

public class appendrow extends JApplet implements ActionListener
{
JTextField textfield;
DefaultTableModel model ;
public static void main(String[] args)
{
appendrow ar=new appendrow();
ar.init();
}
public void init()
{
JFrame frame = new JFrame("Add row JTable");


model = new DefaultTableModel();
JTable table = new JTable(model);

// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");

// Append a row
model.addRow(new Object[]{"123.4", "money"});
// there are now 2 rows with 2 columns

// Append a row with fewer values than columns.
// The left-most fields in the new row are populated
// with the supplied values (left-to-right) and fields
// without values are set to null.

model.addRow(new Object[]{"GBP"});


// Append a row with more values than columns.
// The extra values are ignored.

model.addRow(new Object[]{"DSL", "156", "half"});
// there are now 4 rows with 2 columns

// Set the table transparent
table.setOpaque(false);

JScrollPane sp = new JScrollPane( table );
// JPanel big=new JPanel(new BorderLayout ());
// big.add(sp,BorderLayout.CENTER );

frame.getContentPane().setLayout(new BorderLayout ());
frame.getContentPane().add( sp,BorderLayout.CENTER );

JPanel rowPanel=new JPanel(new BorderLayout ());
textfield=new JTextField(15);
rowPanel.add(textfield,BorderLayout.WEST );

JButton button=new JButton("Add Row");
button.addActionListener(this);
rowPanel.add(button,BorderLayout.EAST );
frame.getContentPane().add( rowPanel,BorderLayout.SOUTH );

frame.pack();
frame.show();
}

public void actionPerformed(ActionEvent e)
{
//check input, if empty, prompt.
if(textfield.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null,"Please input row values"); return;
}
StringTokenizer st=new StringTokenizer(textfield.getText(),",");

Object[] obj=new Object[st.countTokens ()];
int i=0;
while(st.hasMoreTokens ())
{ obj[i]=st.nextToken (); i++;}

//Append a row with more values than columns.
// The extra values are ignored.

model.addRow(obj);
}

}


Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer