当前位置: 代码迷 >> J2SE >> swing下拉框显示表格内容解决方案
  详细解决方案

swing下拉框显示表格内容解决方案

热度:85   发布时间:2016-04-24 01:18:04.0
swing下拉框显示表格内容
哪位好心的雷锋帮哈小弟,我做了一个界面,有一个表格和一些文本框和下拉框,当我点击表格的某一行时可以让这一行的数据显示在文本框里面,但是我想问哈怎样可以让它显示在下拉框里面啊,这个问题小弟我解决不了,谁能帮帮哈!

------解决方案--------------------
Java code
import java.awt.FlowLayout;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JTable;public class ComboBoxText {    private static String[] data1 = {"北京","天津","上海"};    private static String[] data2 = {"东北","华北","华南"};    public static void main(String[] args) {        final ComboBoxText ct = new ComboBoxText();        final JTable t = new JTable(2,2);        final JComboBox cb = new JComboBox();        t.setValueAt("1", 0, 0);        t.setValueAt("2", 0, 1);        t.setValueAt("3", 1, 0);        t.setValueAt("4", 1, 1);                t.addMouseListener(new MouseListener() {            public void mouseClicked(MouseEvent e) {            }            public void mousePressed(MouseEvent e) {                int rowIndex = t.getSelectedRow();                int colIndex = t.getSelectedColumn();                if(rowIndex==0){                    cb.removeAllItems();                    for (int i = 0; i < data1.length; i++) {                        cb.addItem(data1[i]);                    }                }                                if(rowIndex==1){                    cb.removeAllItems();                    for (int i = 0; i < data2.length; i++) {                        cb.addItem(data2[i]);                    }                }                            }            public void mouseReleased(MouseEvent e) {            }            public void mouseEntered(MouseEvent e) {            }            public void mouseExited(MouseEvent e) {            }        });        JFrame f = new JFrame();        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        f.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));        f.add(t);        f.add(cb);        f.setVisible(true);        f.setSize(200, 100);    }}
------解决方案--------------------
Java code
import java.awt.FlowLayout;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JTable;public class ComboBoxText {    public static void main(String[] args) {        final int numRows = 4;        final int numColumns = 2;        final ComboBoxText ct = new ComboBoxText();        final JTable t = new JTable(numRows,numColumns);        final JComboBox cb = new JComboBox();        t.setValueAt("北京", 0, 0);        t.setValueAt("天津", 1, 0);        t.setValueAt("上海", 2, 0);        t.setValueAt("重庆", 3, 0);                t.setValueAt("1", 0, 1);        t.setValueAt("2", 1, 1);        t.setValueAt("3", 2, 1);        t.setValueAt("4", 3, 1);                t.addMouseListener(new MouseListener() {            public void mouseClicked(MouseEvent e) {            }            public void mousePressed(MouseEvent e) {//              int rowIndex = t.getSelectedRow();                int colIndex = t.getSelectedColumn();                cb.removeAllItems();                for (int i = 0; i < numRows; i++) {                    Object o = t.getValueAt(i, colIndex);                    cb.addItem(o);                }            }            public void mouseReleased(MouseEvent e) {            }            public void mouseEntered(MouseEvent e) {            }            public void mouseExited(MouseEvent e) {            }        });        JFrame f = new JFrame();        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        f.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));        f.add(t);        f.add(cb);        f.setVisible(true);        f.setSize(400, 400);    }}
  相关解决方案