Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教几个JTable的问题(看过以前的帖子没找到合适的答案)
0010





发贴: 6
积分: 0
于 2006-05-31 16:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1:我现在有一表 运行后查询数据库里的数据 然后我按关键字来查询 但是表头我想换掉 问题就在这 这个表头怎么换呢
用了table.getColumnModel().getColumn(0).setHeaderValue("column1");
确实是可以换 但是必须我手动的拉一下或最小化才可以 不知道有没有什么更好的方法了

2:更新的问题(也就是刷新)
table.repaint(); 还是data.fireTableDataChanged();
就是在其他窗体里修改了数据 关闭后 想让JTable里显示出来

3:我原先的表头有10列 按关键字查询后 表头只要5列就够了 请问是隐藏呢 还是有什么方法可以去掉
table.getColumnModel().removeColumn(""); 这个方法试了好象不管用

4:列与列可以互相的拉动 我用了一方法 可老报我需要标识符 请问是怎么回事?
table.getTableHeader().setReorderingAllowed(false);

就以上4问题 谢谢了



作者 Re:请教几个JTable的问题(看过以前的帖子没找到合适的答案) [Re:0010]
0010





发贴: 6
积分: 0
于 2006-05-31 16:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

class IntegratedEnquiries extends JDialog implements ActionListener
{
JLabel la,la2,la3;
JTextField txt,txt2;
JRadioButton rad,rad2,rad3,rad4,rad5;员工编号
JComboBox cb;
JButton b1,b2,b3;
ButtonGroup bg=new ButtonGroup();
String strs[]={"无","姓名","身高","出生日期","籍贯","户口"};

DefaultTableModel data;
JTable table;
JScrollPane scroll;
SimpleDateFormat bartDateFormat =new SimpleDateFormat("yyyy-MM-dd");
// JPanel p1,p2;
IntegratedEnquiries(Point point)
{
Container c=getContentPane();
c.setLayout(null);

Font f=new Font("华文细黑",Font.BOLD,15);

la=new JLabel("请选择查询项目:");
la.setBounds(300,5,200,20);
la.setFont(f);

la2=new JLabel("请输入要查询数据:");
la2.setBounds(300,65,200,20);
la2.setFont(f);

la3=new JLabel("到");
la3.setBounds(360,95,30,20);
la3.setFont(f);

rad=new JRadioButton("员工编号");
rad.setFont(f);
rad.setBounds(100,30,90,30);

rad2=new JRadioButton("学历");
rad2.setFont(f);
rad2.setBounds(240,30,60,30);

rad3=new JRadioButton("入职日期");
rad3.setFont(f);
rad3.setBounds(380,30,90,30);

rad4=new JRadioButton("工龄");
rad4.setFont(f);
rad4.setBounds(520,30,90,30);

rad5=new JRadioButton("其他");
rad5.setFont(f);
rad5.setBounds(100,60,60,30);

cb=new JComboBox(strs);
cb.setFont(f);
cb.setBounds(170,68,90,20);

txt=new JTextField();
txt.setFont(f);
txt.setBounds(240,95,90,20);

txt2=new JTextField();
txt2.setFont(f);
txt2.setBounds(400,95,90,20);

c.add(la);
c.add(la2);
c.add(la3);
c.add(rad);
c.add(rad2);
c.add(rad3);
c.add(rad4);
c.add(rad5);
c.add(cb);
c.add(txt);
c.add(txt2);

bg.add(rad);
bg.add(rad2);
bg.add(rad3);
bg.add(rad4);
bg.add(rad5);

b1=new JButton("查询");
b1.setBounds(200,120,70,30);
b1.setFont(f);
c.add(b1);

b2=new JButton("重置");
b2.setBounds(330,120,70,30);
b2.setFont(f);
c.add(b2);

b3=new JButton("取消");
b3.setBounds(450,120,70,30);
b3.setFont(f);
c.add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

data=new DefaultTableModel();
table=new JTable(data)
{
public boolean isCellEditable(int rowIndex, int vColIndex)
{
return false;
}
//需要标识符??
// table.getTableHeader().setReorderingAllowed(false);
// table.getTableHeader().setResizingAllowed(false);
};
data.setRowCount(30);
scroll=new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scroll.setBounds(1,160,739,305);

c.add(scroll);
// createDataColumns();

setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

this.setTitle("综合查询");
setResizable(false);
this.setBounds(120,126,750,500);
this.setModal(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
if(rad.isSelected())
{
Integrated();
}
else if(rad2.isSelected())
{
// table.getColumnModel().getColumn(0).setHeaderValue("column1");
table.setModel(new DefaultTableModel());
table.repaint();
createDataColumns1();
// data.fireTableDataChanged();
//去除列
// table.getColumnModel().removeColumn("备注");
radd2();
}
else if(rad3.isSelected())
{
radd3();
}
else if(rad4.isSelected())
{
radd4();
}
else if(rad5.isSelected())
{
radd5();
}
else
{
createDataColumns();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:emp";
Connection cn=DriverManager.getConnection(url);
String st="select * from Information order by 员工编号";
PreparedStatement p=cn.prepareStatement(st);
ResultSet rs=p.executeQuery();
int a=data.getRowCount();
for(int i=0;i<a;i++)
{
data.removeRow(0);
}
while(rs.next())
{
String s1=rs.getString(1).toString();
String s2=rs.getString(2).toString();
String s3=rs.getString(3).toString();
String s4=rs.getString(4).toString();
data.addRow(dataRow(s1,s2,s3,s4));
}
rs.close();
cn.close();
}
catch(Exception iie){}
if(data.getRowCount()<30)
{
data.setRowCount(30);
}
}
}
if(e.getSource()==b2)
{
txt.setText(null);
txt2.setText(null);
cb.setSelectedIndex(0);
}
if(e.getSource()==b3)
{
int a=JOptionPane.showConfirmDialog(this,"确定要退出吗?","提示",JOptionPane.INFORMATION_MESSAGE);
if(a==JOptionPane.YES_OPTION)
dispose();
}
}
public void createDataColumns()
{
data.addColumn("员工编号");
data.addColumn("姓名");
data.addColumn("身高");
data.addColumn("性别");
}
public void createDataColumns1()
{
data.addColumn("员工编号111");
data.addColumn("姓名");
data.addColumn("身高");
data.addColumn("性别");
}
public void Integrated()
{
int num=0;
String st1,st2;
st1=txt.getText();
st2=txt2.getText();
if(st1.length()!=0)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:emp";
Connection cn=DriverManager.getConnection(url);
String st="select * from Information where 员工编号 ='" + txt.getText() + "'";
PreparedStatement p=cn.prepareStatement(st);
ResultSet rs=p.executeQuery();
while(rs.next())
{
num++;
}
if(num==0)
{
JOptionPane.showMessageDialog(null,"你所查询的数据不存在,请确认后查询");
txt.setText(null);
txt2.setText(null);
return;
}
else
{
rs.close();
rs=p.executeQuery();
int a=data.getRowCount();
for(int i=0;i<a;i++)
{
data.removeRow(0);
}
while(rs.next())
{
String s1=rs.getString(1).toString();
String s2=rs.getString(2).toString();
String s3=rs.getString(3).toString();
String s4=rs.getString(4).toString();
data.addRow(dataRow(s1,s2,s3,s4));
}
rs.close();
cn.close();
}
}
catch(Exception iie){};
if((st1.length()!=0) && (st2.length()!=0))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:emp";
Connection cn=DriverManager.getConnection(url);
String st="select * from Information where (员工编号 >='"
+ txt.getText() + "') and (员工编号 <='" + txt2.getText() + "') order by 员工编号";
PreparedStatement p=cn.prepareStatement(st);
ResultSet rs=p.executeQuery();
while(rs.next())
{
num++;
}
if(num==0)
{
JOptionPane.showMessageDialog(null,"你所查询的数据不存在,请确认后查询");
txt.setText(null);
txt2.setText(null);
return;
}
else
{
rs.close();
rs=p.executeQuery();
int a=data.getRowCount();
for(int i=0;i<a;i++)
{
data.removeRow(0);
}
while(rs.next())
{
String s1=rs.getString(1).toString();
String s2=rs.getString(2).toString();
String s3=rs.getString(3).toString();
String s4=rs.getString(4).toString();
data.addRow(dataRow(s1,s2,s3,s4));
}
rs.close();
cn.close();
}
}
catch(Exception iie){};
}
}
else
{
JOptionPane.showMessageDialog(this,"请输入查询数据","提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(data.getRowCount()<30)
{
data.setRowCount(30);
}
}
//学历选项
public void radd2()
{
int num=0;
String st1,st2;
st1=txt.getText();
st2=txt2.getText();
if(st1.length()!=0)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:emp";
Connection cn=DriverManager.getConnection(url);
String st="select * from Information where 学历 ='" + txt.getText() + "' order by 员工编号";
PreparedStatement p=cn.prepareStatement(st);
ResultSet rs=p.executeQuery();
while(rs.next())
{
num++;
}
if(num==0)
{
JOptionPane.showMessageDialog(null,"你所查询的数据不存在,请确认后查询");
txt.setText(null);
txt2.setText(null);
return;
}
else
{
rs.close();
rs=p.executeQuery();
int a=data.getRowCount();
for(int i=0;i<a;i++)
{
data.removeRow(0);
}
while(rs.next())
{
String s1=rs.getString(1).toString();
String s2=rs.getString(2).toString();
String s3=rs.getString(3).toString();
String s4=rs.getString(4).toString();
data.addRow(dataRow(s1,s2,s3,s4));
}
rs.close();
cn.close();
}
}
catch(Exception iie){};
}
else
{
JOptionPane.showMessageDialog(this,"请输入查询数据","提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(data.getRowCount()<30)
{
data.setRowCount(30);
}
}
public void radd3()
{

}
public void radd4()
{

}
public void radd5()
{

}
public Object[] dataRow(String s1,String s2,String s3,String s4)
{
Object row[]=new Object[4];
row[0]=s1;
row[1]=s2;
row[2]=s3;
row[3]=s4;
return row;
}
}

这是写的这个类的代码 麻烦大家看看


why edited on 2006-05-31 19:39

作者 Re:请教几个JTable的问题(看过以前的帖子没找到合适的答案) [Re:0010]
joelwx





发贴: 97
积分: 2
于 2006-06-04 11:24 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1.javax.swing.table.DefaultTableModel(Vector data,Vector ColumnLabel)
2.javax.swing.table.DefaultTableModel.setDataVector(Vector data,Vector ColumnLabel)
javax.swing.JTable.setModel(TableModel tm)
3.bu hui
4.mei kan dong.
you yong ma??
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/DefaultTableModel.html



好多不会唉...
作者 Re:请教几个JTable的问题(看过以前的帖子没找到合适的答案) [Re:0010]
joelwx





发贴: 97
积分: 2
于 2006-06-04 11:27 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
4.table=new JTable(data)
{ public boolean isCellEditable(int rowIndex, int vColIndex)
{return false;}
//需要标识符??
// table.getTableHeader().setReorderingAllowed(false);
// table.getTableHeader().setResizingAllowed(false);
};
===============
4.table=new JTable(data)
{ public boolean isCellEditable(int rowIndex, int vColIndex)
{return false;}
//需要标识符??
// table.getTableHeader().setReorderingAllowed(false);
// table.getTableHeader().setResizingAllowed(false);
};
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
============
try 1 xia!!!!



好多不会唉...
作者 Re:请教几个JTable的问题(看过以前的帖子没找到合适的答案) [Re:0010]
joelwx





发贴: 97
积分: 2
于 2006-06-04 11:29 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
3.JTable.setModel((TableModel)DefaultTableModel.setDataVector(Vector newTableData,Vector newTableCoumnLabel))

try 1 xia again!!



好多不会唉...
作者 Re:请教几个JTable的问题(看过以前的帖子没找到合适的答案) [Re:0010]
joelwx





发贴: 97
积分: 2
于 2006-06-04 11:31 user profilesend a private message to usersend email to joelwxsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
下次贴关键代码就行了.
看得头晕!



好多不会唉...

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923