当前位置: 代码迷 >> Java相关 >> jcombobox与jtable的连接解决方法
  详细解决方案

jcombobox与jtable的连接解决方法

热度:1061   发布时间:2016-04-22 23:19:07.0
jcombobox与jtable的连接
eg:jcombobox下拉菜单里有三个值a、b、c,
现在想要使当选择 a 时,对应显示表格 a ;
相应 选择 b 时,对应显示表格 b ;
选择 c ,显示表格 c ;

------解决方案--------------------
这是三个数据模型相同的JTable的控制显示,只是切换JTable的数据模型就可以,你先看看,待会再贴数据模型不同的控制
Java code
import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.util.List;public class JTableDemo4 extends JFrame implements ActionListener{    private JTable[] jTable=new JTable[3];    //    private JButton bn1,bn2,bn3;    private JComboBox combobox=null;    private JPanel panel=null;    private MyModel[] model=new MyModel[3];    private JScrollPane s_pane=null;    private Container contentPane=null;    public JTableDemo4(String title){        super(title);        model[0]=new MyModel(2);        jTable[0]=new JTable(model[0]);        model[1]=new MyModel(4);        jTable[1]=new JTable(model[1]);        model[2]=new MyModel(6);        jTable[2]=new JTable(model[2]);        contentPane=this.getContentPane();        combobox=new JComboBox();        combobox.addItem("table1(2 users)");        combobox.addItem("table2(4 users)");        combobox.addItem("table3(6 users)");        combobox.addActionListener(this);        panel=new JPanel();        panel.add(combobox);        List<User> list1=new ArrayList<User>();        for(int i=0;i<3;i++){            User u=new User(i,"user"+i,"list1");            list1.add(u);        }        s_pane=new JScrollPane(jTable[0]);        contentPane.add(panel,BorderLayout.SOUTH);        contentPane.add(s_pane,BorderLayout.CENTER);        pack();        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public void actionPerformed(ActionEvent e) {        // TODO Auto-generated method stub        System.out.println("action performed");        int index=combobox.getSelectedIndex();        System.out.println("index:"+index);        jTable[0].setModel(model[index]);        jTable[0].updateUI();//        s_pane.removeAll();//        s_pane.add(jTable[index]);////        s_pane=new JScrollPane();////        s_pane.validate();//        contentPane.validate();//        this.validate();//        this.repaint();    }    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        new JTableDemo4("MyDemo");    }    class MyModel extends AbstractTableModel{        private ArrayList<User> list;        private String [] title_name={"ID","用户名","密码"};        /**         * 初始化构造         */        public MyModel(){            list=new ArrayList<User>();            for(int i=0;i<5;i++){                User user=new User(i,"user"+i,"user"+i);                this.list.add(user);            }            /*                * 这里数据结构当中加进去多少的数据,                * 虚拟机会利用getValueAt(int row,int col)方法                * 全部给取出显示                */        }        public MyModel(int userNum){            list=new ArrayList<User>();            for(int i=0;i<userNum;i++){                User user=new User(i,"user"+i,"user"+i);                this.list.add(user);            }            /*                * 这里数据结构当中加进去多少的数据,                * 虚拟机会利用getValueAt(int row,int col)方法                * 全部给取出显示                */        }        public void addRow(long id,String userName,String password){            User user=new User(id,userName,password);            list.add(user);        }        public void removeRaw(int row){            list.remove(row);            this.fireTableRowsDeleted(1,row+1);        }        public int getColumnCount() {            // TODO Auto-generated method stub            System.out.println("invoke method getColumnCount");            return title_name.length;        }        public String getColumnName(int col) {            System.out.println("invoke method getColumnName");            return title_name[col];        }        public int getRowCount() {            // TODO Auto-generated method stub            return list.size();        }        public Object getValueAt(int rowIndex, int columnIndex) {            // TODO Auto-generated method stub            //System.out.println("invoke method getValueAt");            User result=list.get(rowIndex);            return result.get(columnIndex);        }        public void setValueAt(Object value, int row, int col){            if(col==0)                list.get(row).setId((Long)value);            if(col==1)                list.get(row).setUserName((String)value);            if(col==2)                list.get(row).setPassword((String)value);            this.fireTableCellUpdated(row, col);        }        public boolean isCellEditable(int rowIndex, int columnIndex){            return true;        }        /**         * 返回数据类型         */        public Class getColumnClass(int col) {            return getValueAt(0, col).getClass();        }    }    class User{        private long id;        private String userName;        private String password;        public User(){}        public User(long id,String userName,String password){            this.id=id;            this.userName=userName;            this.password=password;        }        public Object get(int i){            if(i==0)                return this.id;            if(i==1)                return this.userName;            if(i==2)                return this.password;            else                return null;        }        public String getPassword() {            return password;        }        public void setPassword(String password) {            this.password = password;        }        public String getUserName() {            return userName;        }        public void setUserName(String userName) {            this.userName = userName;        }        public long getId() {            return id;        }        public void setId(long id) {            this.id = id;        }    }}
  相关解决方案