3.2.0

How to get data of a row?

Thank you.
Angel
July 12,
Below is my implementation...

I asked the question here as well, but after a few minutes I reaslized, it is extremly easy

// Return all the values of a row, based on the current cell row id
function getRowArray(row){
var aantal_kolommen = obj.getColumnCount();
RowArray = new Array();
for (k=0 ; k < aantal_kolommen ; k++ ) {
RowArray[k] = obj.getCellText(k ,row);
}
return RowArray;
}
stef
July 13,
how i go to new page with java script function
ali
July 20,
How to create a dialog box with button like ok and cancle in java.
Prashant
July 26,
the following is my code i want to get data in to table if i click on find button the find button must capture data from text field

package Myproj;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.undo.*;
import javax.swing.table.*;
import javax.swing.text.AbstractDocument;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;



/**
* <p>Title: NEJ- BerT Project</p>
* <p>Description: BerT Application</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class MyExample extends JFrame implements ActionListener,DocumentListener,TableModelListener {
Container c=null;
public static final String Numeric="0123456789";
javax.swing.JTextField t1;
JScrollPane scrollPane;
JTable table;
JLabel label;
Button b1,b2,b3,b4;
String s="";
String s1;

Vector rows,columns;
DefaultTableModel tabModel;


private IntTextField intFiled;
public MyExample() {



JFrame fr=new JFrame();
c = getContentPane();
c.setLayout(new BorderLayout());
setSize(400, 300);
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);





JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();

//JTextField t1 = new JTextField(10);

label = new JLabel("Enter id:", JLabel.RIGHT);

label.setDisplayedMnemonic('L');
label.setLabelFor(t1);
label.setOpaque(true);
p1.add(label);
intFiled = new IntTextField(0, 3);
p1.add(intFiled);
intFiled.getDocument().addDocumentListener(this);

// p1.add(t1);


Panel toolbar = new Panel();


b1=new Button("Find");


b1.addActionListener(this);

b2=new Button("Clear");

b2.addActionListener(this);

p1.add(b1);
p1.add(b2);
p1.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4));

c.add(p1, BorderLayout.NORTH);



// Create a new table instance
String[] columnNames = {"ItemId", "ItemName", "Description","Price"};

Object[][] data = {
{"1", "Bat",
"Cricket", new Integer(450)},
{"2", "Football",
"Soccer", new Integer(250)},{"1", "Bat",
"Cricket", new Integer(450)},
{"2", "Football",
"Soccer", new Integer(250)},
{"1", "Bat",
"Cricket", new Integer(450)},
{"2", "Football",
"Soccer", new Integer(250)},

};

//table = new JTable(data, columnNames);
tabModel=new DefaultTableModel();
tabModel.setDataVector(data,columnNames);


table = new JTable(tabModel);
scrollPane= new JScrollPane(table);//ScrollPane
//table.setPreferredScrollableViewportSize(new Dimension(200, 150));
table.setRowSelectionAllowed(false);

table.getModel().addTableModelListener(this);

p2.add(table);
p2.setLayout(new GridLayout(4,0,0,0));

c.add(p2);
// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
p2.add( scrollPane, BorderLayout.CENTER );


b3=new Button("Save");
b4=new Button("Close");
b4.addActionListener(this);
b3.addActionListener(this);


p3.add(b3,BorderLayout.CENTER);
p3.add(b4);
p3.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 8));
c.add(p3, BorderLayout.SOUTH);



show();
}


private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();

System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}

}

}


public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand());

if(ae.getActionCommand()=="Close"){
System.exit(0);
}
if (ae.getActionCommand()== "Clear")
{
//t1.setText(s);
clear();
}
if (ae.getSource()== "Save")
{
String CName=JOptionPane.showInputDialog(null,"Enter Value to be set at Cell 0,2 ","Simple Table Example",JOptionPane.INFORMATION_MESSAGE);
if(!CName.trim().equals("") && table.getRowCount()>0)
{
selectCell(0,3);

table.setValueAt(CName,0,3);
}
}
if (ae.getActionCommand()== "Find")
{



}





}

public void clear(){
intFiled.setText("");
}

public static void main(String a[]){
MyExample me=new MyExample();
me.setVisible(true);
}

public void insertUpdate(DocumentEvent e) {
setLabel();
}

public void removeUpdate(DocumentEvent e) {
setLabel();
}

public void changedUpdate(DocumentEvent e) {
}

public void setLabel() {
if (intFiled.isValid() ) {
int value = intFiled.getValue();
//label.setText(Integer.toString(value));
}
}

public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
setLabel();
}
public void tableChanged(javax.swing.event.TableModelEvent source) {
String msg="";
TableModel tabMod = (TableModel)source.getSource();
switch (source.getType())
{
case TableModelEvent.UPDATE:

msg="Table Value Updated for cell "+table.getSelectedRow()+","+table.getSelectedColumn()+"\nWhich is "+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
break;

}

}//Table Changed Method

public void selectCell(int row,int col)
{
if(row!=-1 && col !=-1)
{

table.setRowSelectionInterval(row,row);
table.setColumnSelectionInterval(col,col);

}
}



}
class IntTextField extends JTextField {
public IntTextField(int defval, int size) {
super("" + defval, size);
}

protected Document createDefaultModel() {
return new IntTextDocument();
}

public boolean isValid() {
try {
Integer.parseInt(getText());
return true;
} catch (NumberFormatException e) {
return false;
}
}

public int getValue() {
try {
return Integer.parseInt(getText());
} catch (NumberFormatException e) {
return 0;
}
}
}
class IntTextDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null)
return;
String oldString = getText(0, getLength());
String newString = str
+ oldString;
try {
Integer.parseInt(newString + "0");
super.insertString(offs, str, a);
} catch (NumberFormatException e) {
}
}
}




help me out






rams
July 28,

This topic is archived.

See also:


Back to support forum