Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JTable

  Asked By: Richard    Date: Aug 23    Category: Java    Views: 655
  

my question is that how can I get columns value of table when I clicks a button
?

I tried in my codes below but I couldn't.

package mypackage2;

import javax.swing.JInternalFrame.*;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.util.*;
import javax.swing.table.*;
import javax.swing.JLabel;
import java.awt.BorderLayout;

public class SimpleInternalFrame extends Frame implements ActionListener{
JTable table;
TableModel model;
private String icerik1,icerik2,icerik3;
private Integer rowsayisi;
JButton openButton, macButton, javaButton, motifButton, winButton;
JTextField text1,text2,text3;
JLayeredPane desktop,desktop1;
JInternalFrame internalFrame;
public SimpleInternalFrame() {
super("Internal Frame Demo");
setSize(500,400);
text1=new JTextField();
text2=new JTextField();
text3=new JTextField();
text1.setText(" ");
text1.setBounds(new Rectangle(105, 10, 59, 20));
text2.setText(" ");
text2.setBounds(new Rectangle(105, 35, 59, 20));
text3.setText(" ");
text3.setBounds(new Rectangle(105, 60, 59, 20));
openButton = new JButton("Open");
macButton = new JButton("Mac");
//ekleme1
PersistenceManager persistMgr = new PersistenceManager();

try {
ResultSet rs = persistMgr.executeSQL("SELECT * FROM addr;");

model = new DataModel(rs);
} catch (SQLException e) {
e.printStackTrace();
return;
}

JTable table = new JTable(model);
javaButton = new JButton("Metal");
motifButton = new JButton("Motif");
winButton = new JButton("Windows");
Panel p1 = new Panel();
Panel p = new Panel();
p1.add(text1);
p1.add(text3);
p1.add(text2);
p1.add(text3);
p.add(openButton);
p.add(macButton);
p.add(javaButton);
p.add(motifButton);
p.add(winButton);
add(p1, BorderLayout.NORTH);
add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

openButton.addActionListener(this);
macButton.addActionListener(this);
javaButton.addActionListener(this);
motifButton.addActionListener(this);
winButton.addActionListener(this);
// Set up the layered pane
desktop = new JDesktopPane();
desktop1 = new JDesktopPane();
desktop.setOpaque(true);
desktop1.setOpaque(true);
//add(desktop1, BorderLayout.NORTH);
add(desktop, BorderLayout.CENTER);


internalFrame = new JInternalFrame("Internal Frame",
true, true, true, true);
internalFrame.setBounds(50, 50, 200, 100);
internalFrame.getContentPane().add(table);

//desktop.add(p1, new Integer(1));
desktop.add(internalFrame, new Integer(1));
internalFrame.setVisible(true);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}

}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Open"))
{

int rowsayisi=this.table.getRowCount();
String icerik1 = ""+rowsayisi;
text1.setText(icerik1);
//text2.setText(table.getModel().getValueAt(rowsayisi,2));
//text3setText(table.getModel().getValueAt(rowsayisi,3));
}
}
public static void main(String args[]) {
SimpleInternalFrame sif = new SimpleInternalFrame();
sif.setVisible(true);
}
private void jbInit() throws Exception
{
}
}
class DataModel1 extends AbstractTableModel {
String[] columnNames;
Vector rows = new Vector();

DataModel1(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
columnNames = new String[numberOfColumns];
columnNames[0]="ID";
columnNames[1]="ACCESS";
columnNames[2]="NAME";
rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= getColumnCount(); i++) {
newRow.addElement(rs.getObject(i));
/
}
rows.addElement(newRow);
}
}
/
public String getColumnName(int column) {
if (columnNames[column] != null) {
return columnNames[column];
} else {

return "";
}
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rows.size();
}
public Object getValueAt(int aRow, int aColumn) {
Vector row = (Vector)rows.elementAt(aRow);
return row.elementAt(aColumn);
}

}
class PersistenceManager1 {
Statement stmt;

PersistenceManager1() {
try {

Class.forName("com.mysql.jdbc.Driver").newInstance();


// Connection info for MySQL
String dbserver = "localhost";
String dbname = "deneme1";
String user = "root";
String password = "";
String cs =
"jdbc:mysql://"+dbserver+"/"+dbname+"?user="+user+"&password="+password;
// The SQL statement to execute

// Open a connection to the database
Connection con = DriverManager.getConnection(cs);
stmt = con.createStatement();
//


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

ResultSet executeSQL(String str) throws SQLException {
return stmt.executeQuery(str);
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Darrell Harvey     Answered On: Aug 23

what do you mean with "how can I get columns  value of table  when I clicks  a
button?" ? can you explain it exactly please?...

 
Didn't find what you were looking for? Find more on JTable Or get search suggestion and latest updates.




Tagged: