jtable选中事件的问题
使用jtable选中一行后在使用JComboBox的选择事件返回-1-1我是根据JComboBox来控制jtable里的数据,每次选择JComboBox都会刷新表里的内容...
使用jtable选中一行后 在使用JComboBox 的选择事件 返回-1-1
我是根据JComboBox来控制jtable里的数据,每次选择JComboBox都会刷新表里的内容
打个比方: 我用JComboBox选择一班,显示一班的学生,选择二班,显示二班的学生
本来都是可以的`
但加了jtable选择行事件后 选中jtable里的一行 在使用JComboBox就会返回-1-1
是什么原因呢????
下面那个是可以 但不是我要的答案啊
我要的是jtable的选择事件选择一行后
在使用JCombobox换个班级时就返回-1-1 不知道是什么原因
这是jtable选择事件行事件的代码
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e)
{
int rows=table.getSelectedRow();
jid.setText(table.getValueAt(rows, 0).toString());
jname.setText(table.getValueAt(rows, 1).toString());
jsex.setText(table.getValueAt(rows, 2).toString());
jdate.setText(table.getValueAt(rows, 3).toString());
jam.setText(table.getValueAt(rows, 4).toString());
jbj.setText(table.getValueAt(rows, 5).toString());
}
}); 展开
我是根据JComboBox来控制jtable里的数据,每次选择JComboBox都会刷新表里的内容
打个比方: 我用JComboBox选择一班,显示一班的学生,选择二班,显示二班的学生
本来都是可以的`
但加了jtable选择行事件后 选中jtable里的一行 在使用JComboBox就会返回-1-1
是什么原因呢????
下面那个是可以 但不是我要的答案啊
我要的是jtable的选择事件选择一行后
在使用JCombobox换个班级时就返回-1-1 不知道是什么原因
这是jtable选择事件行事件的代码
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e)
{
int rows=table.getSelectedRow();
jid.setText(table.getValueAt(rows, 0).toString());
jname.setText(table.getValueAt(rows, 1).toString());
jsex.setText(table.getValueAt(rows, 2).toString());
jdate.setText(table.getValueAt(rows, 3).toString());
jam.setText(table.getValueAt(rows, 4).toString());
jbj.setText(table.getValueAt(rows, 5).toString());
}
}); 展开
展开全部
我看不到你的代码,不能具体的说明,我按照你的描述,给你做了一个实例,你看看,或许对你会有帮助:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test extends javax.swing.JFrame {
private JPanel jPanel1;
private JTextField jTextField1;
private JLabel jLabel1;
private JComboBox jComboBox1;
private JTable jTable1;
private JScrollPane jScrollPane1;
String colname[] = { "班级", "姓名" };
public static void main(String[] args) {
Test inst = new Test();
}
public Test() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jScrollPane1 = new JScrollPane();
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(12, 80, 362, 83);
{
TableModel jTable1Model = new DefaultTableModel(
new String[][] {}, colname);
jTable1 = new JTable();
jTable1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
jTextField1
.setText(jTable1.getValueAt(
jTable1.getSelectedRow(), 1)
.toString());
}
});
jScrollPane1.setViewportView(jTable1);
jTable1.setModel(jTable1Model);
}
}
{
ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
new String[] { "", "一班", "二班" });
jComboBox1 = new JComboBox();
jPanel1.add(jComboBox1);
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String classname = jComboBox1.getSelectedItem()
.toString();
// 你可以去查询数据库,从而得到你想要的值,这里我就直接写了
if (classname.equals("一班")) {
String values[][] = { { "一班", "张三" },
{ "一班", "张三2" } };
DefaultTableModel model = (DefaultTableModel) jTable1
.getModel();
model.setDataVector(values, colname);
}
if (classname.equals("二班")) {
String values[][] = { { "二班", "李四i" },
{ "二班", "李四2" }, { "二班", "李四4" } };
DefaultTableModel model = (DefaultTableModel) jTable1
.getModel();
model.setDataVector(values, colname);
}
}
});
jComboBox1.setModel(jComboBox1Model);
jComboBox1.setBounds(48, 18, 95, 22);
}
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("\u60a8\u9009\u4e2d\u7684\u503c\u4e3a:");
jLabel1.setBounds(33, 203, 78, 15);
}
{
jTextField1 = new JTextField();
jPanel1.add(jTextField1);
jTextField1.setText("");
jTextField1.setBounds(128, 203, 221, 22);
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test extends javax.swing.JFrame {
private JPanel jPanel1;
private JTextField jTextField1;
private JLabel jLabel1;
private JComboBox jComboBox1;
private JTable jTable1;
private JScrollPane jScrollPane1;
String colname[] = { "班级", "姓名" };
public static void main(String[] args) {
Test inst = new Test();
}
public Test() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jScrollPane1 = new JScrollPane();
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(12, 80, 362, 83);
{
TableModel jTable1Model = new DefaultTableModel(
new String[][] {}, colname);
jTable1 = new JTable();
jTable1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
jTextField1
.setText(jTable1.getValueAt(
jTable1.getSelectedRow(), 1)
.toString());
}
});
jScrollPane1.setViewportView(jTable1);
jTable1.setModel(jTable1Model);
}
}
{
ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
new String[] { "", "一班", "二班" });
jComboBox1 = new JComboBox();
jPanel1.add(jComboBox1);
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String classname = jComboBox1.getSelectedItem()
.toString();
// 你可以去查询数据库,从而得到你想要的值,这里我就直接写了
if (classname.equals("一班")) {
String values[][] = { { "一班", "张三" },
{ "一班", "张三2" } };
DefaultTableModel model = (DefaultTableModel) jTable1
.getModel();
model.setDataVector(values, colname);
}
if (classname.equals("二班")) {
String values[][] = { { "二班", "李四i" },
{ "二班", "李四2" }, { "二班", "李四4" } };
DefaultTableModel model = (DefaultTableModel) jTable1
.getModel();
model.setDataVector(values, colname);
}
}
});
jComboBox1.setModel(jComboBox1Model);
jComboBox1.setBounds(48, 18, 95, 22);
}
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("\u60a8\u9009\u4e2d\u7684\u503c\u4e3a:");
jLabel1.setBounds(33, 203, 78, 15);
}
{
jTextField1 = new JTextField();
jPanel1.add(jTextField1);
jTextField1.setText("");
jTextField1.setBounds(128, 203, 221, 22);
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询