当前位置: 代码迷 >> Java相关 >> 神经塔进 jtable test
  详细解决方案

神经塔进 jtable test

热度:818   发布时间:2006-10-10 21:18:25.0
神经塔进 jtable test

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;

public SimpleTableDemo() {
super(new GridLayout(1,0));

String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};

Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
add(scrollPane);
}

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));
}
System.out.println();
}
System.out.println("--------------------------");
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

搜索更多相关的解决方案: test  jtable  神经  

----------------解决方案--------------------------------------------------------
这个是sun公司提供的例子
你看看吧
----------------解决方案--------------------------------------------------------
把你的数据放在构造jtable的数组里不就行了吗!!!
----------------解决方案--------------------------------------------------------

恩,我知道在jtable后面要放上自己所要构建的数组,但是你看我的那个题目我的对象是怎么建呢,我一直再考虑!无从下手!郁闷!
package jdbc;
import java.sql.*;

public class SqlDemo {
public SqlDemo() {
}

public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ce){
ce.printStackTrace();
}
try{
Connection con=DriverManager.getConnection("jdbc:odbc:jwt");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from sysfiles");
while(rs.next()){
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
System.out.println();
}
rs.close();
st.close();
con.close();
}catch(SQLException se){
se.printStackTrace();
}


}
}

[此贴子已经被作者于2006-10-10 21:56:45编辑过]


----------------解决方案--------------------------------------------------------
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
System.out.println();

----------------解决方案--------------------------------------------------------

package jdbc;
import java.sql.*;
import javax.swing.JTable;

public class SqlDemo {
public SqlDemo() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException ce){
ce.printStackTrace();
}
try{
Connection con=DriverManager.getConnection("jdbc:odbc:jwt");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from sysfiles");
while(rs.next()){
String[] slo = {"课程","编号","原因"};
Object[][] cells = {
{rs.getString(2)},
{rs.getString(3)},
{rs.getString(4)},
};
JTable jTable1 = new JTable(cells,slo);
}
rs.close();
st.close();
con.close();
}catch(SQLException se){
se.printStackTrace();
}
}

private void jbInit() throws Exception {
}

}
还是不对!帮忙改下!麻烦了


----------------解决方案--------------------------------------------------------

你的程序也没有输出啊
有他的这个方法
(你没仔细看我给的那个程序吧)
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));
}
System.out.println();
}
System.out.println("--------------------------");
}


----------------解决方案--------------------------------------------------------

你要建一个 JTable,一个很好的方法就是利用MVC模式,先构造一个TableModel,在表格的数据模型中保存所有需要保存的数据,数据模型的数据从哪里取,这可以是动态的,这样一来,就可以动态的显示你的表格而不用事先构造一大堆数据出来了,这样就可以做到,需要时才去取.
建议好好看看JTable,在Swing中,这个JTable算是比较复杂的一个,看懂了对看肯定有帮助,并且它用于显示数据很实用


----------------解决方案--------------------------------------------------------
这个题目我是一定要自己做出来的,谢谢你们的帮忙!你们等着看答案吧!
----------------解决方案--------------------------------------------------------

加油


----------------解决方案--------------------------------------------------------
  相关解决方案