weixin_35695907 2016-08-25 18:10 采纳率: 0%
浏览 1644

在swing的Jtable初始化中已经添加过数据,查询一条或者多条数据,Jtbale里数据不更新

import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import java.awt.event.ActionEvent;

//创建主窗口
public class mainFace {

private JFrame mainframe;
private JTextField chepaiField;
private Vector cars;
private SelectCar sc=new SelectCar() ;
private JTable ResultTable;
private JScrollPane scrollPaneForRusultTable ;
private DefaultTableModel tableModel;
private Vector<String> columnNames=new Vector<>();
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mainFace window = new mainFace();
                window.mainframe.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public mainFace() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    mainframe = new JFrame();
    mainframe.setTitle("阜建路段客车升降档查询系统");
    mainframe.getContentPane().setLayout(null);

    JLabel chepauLable = new JLabel("请输入车牌");
    chepauLable.setBounds(54, 32, 80, 15);
    mainframe.getContentPane().add(chepauLable);

    chepaiField = new JTextField();
    chepaiField.setBounds(144, 29, 99, 21);
    mainframe.getContentPane().add(chepaiField);
    chepaiField.setColumns(10);


    //查询按钮
    JButton selectButton = new JButton("查询");
    selectButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if(chepaiField.getText().length()<1)
            {
                /*chepaiField(车牌)输入长度小于1,表示未输入车牌,弹出对话框*/
                  JOptionPane.showMessageDialog(null, "查询所有车辆","未输入车牌",JOptionPane.ERROR_MESSAGE);
                //长度小于1,查询所有车牌
                cars=null;
                cars=sc.selectAll();
            }
            else
            {
                //长度不小于1,执行查询指定车牌的车
                cars=null;
                cars=sc.selectCar(chepaiField.getText());       
            }   
            ResultTable=null;
            ResultTable = new JTable(cars,columnNames);
            scrollPaneForRusultTable.setViewportView(ResultTable);


        }
    });

    selectButton.setBounds(285, 28, 93, 23);
    mainframe.getContentPane().add(selectButton);


    System.out.println("1");
    Vector<String> columnNames=new Vector<>();
    columnNames.addElement("序号");
    columnNames.addElement("车牌");
    columnNames.addElement("车类型");
    columnNames.addElement("座位数");
    columnNames.addElement("查询方式");
    columnNames.addElement("备注");
    columnNames.addElement("添加人");
    columnNames.addElement("图片");
    //={"序号","车牌","车型","座位数","查询方式","录入人","录入时间","备注"};//列名字
    Vector tableValue=sc.selectAll();
    ResultTable = new JTable(tableValue,columnNames);
    //创建显示表格滚动的面板
    scrollPaneForRusultTable = new JScrollPane();
    scrollPaneForRusultTable.setBounds(60, 90, 800, 800);
    mainframe.getContentPane().add(scrollPaneForRusultTable);
    scrollPaneForRusultTable.setViewportView(ResultTable);

    //单机ResultTable窗体弹出对话框
    ResultTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    ResultTable.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent e)
                {

                    JOptionPane.showMessageDialog(null, ResultTable.getValueAt(ResultTable.getSelectedRow(),1),"错误提示",JOptionPane.ERROR_MESSAGE);
                    /*if(e.getClickCount()==2)
                    {
                        System.out.println(x);
                    }*/
                }
            });


    //结果显示的表窗体
    /*
    String[] columnNames={"序号","车牌","车型","座位数","查询方式","录入人","录入时间","备注"};
    tableModel.addColumn(columnNames);
    cars=sc.selectAll();
    tableModel.addRow(cars);*/
    mainframe.setBounds(100, 100, 1000, 1000);
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}


  • 写回答

1条回答 默认 最新

  • dabocaiqq 2017-02-04 15:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿