下面这段代码参照一下,基本上能实现你的要求,但有些细节没有完善,比如JTable的禁止编辑等等。
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class TableSelector extends javax.swing.JFrame {
private JTextField txtItem;
private JLabel jLabel1;
private JButton btnSelect;
private JScrollPane jspTable;
private boolean isshown = false;
private JTable table;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TableSelector inst = new TableSelector();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public TableSelector() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().add(getTxtItem());
getContentPane().add(getBtnSelect());
getContentPane().add(getJLabel1());
pack();
setSize(500, 400);
} catch (Exception e) {
e.printStackTrace();
}
}
private JTextField getTxtItem() {
if (txtItem == null) {
txtItem = new JTextField();
txtItem.setSize(170, 25);
txtItem.setLocation(0, 0);
}
return txtItem;
}
private JButton getBtnSelect() {
if (btnSelect == null) {
btnSelect = new JButton("");
btnSelect.setBounds(170, 0, 16, 23);
btnSelect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isshown) {
TableSelector.this.getLayeredPane().remove(jspTable);
isshown = false;
} else {
jspTable = getJspTable();
int x = txtItem.getX();
int y = txtItem.getY() + txtItem.getHeight();
jspTable.setLocation(x, y);
TableSelector.this.getLayeredPane().add(jspTable, 0);
isshown = true;
table.requestFocus();
}
TableSelector.this.getLayeredPane().validate();
TableSelector.this.getLayeredPane().repaint();
}
});
}
return btnSelect;
}
private JScrollPane getJspTable() {
if(jspTable == null) {
Vector rowdata = new Vector();
for(int i = 0; i < 10; i ++) {
Vector row = new Vector();
row.add("000" + i);
row.add("CustomName" + i);
row.add("CustomAddress" + i);
rowdata.add(row);
}
Vector header = new Vector();
header.add("自编号");
header.add("客户名称");
header.add("客户地址");
table = new JTable(rowdata, header);
table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1) {
int row = table.getSelectedRow();
txtItem.setText(table.getValueAt(row, 2).toString());
TableSelector.this.getLayeredPane().remove(jspTable);
isshown = false;
TableSelector.this.getLayeredPane().validate();
TableSelector.this.getLayeredPane().repaint();
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
table.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {
Component target = e.getOppositeComponent();
if(target == btnSelect) {
return;
} else {
TableSelector.this.getLayeredPane().remove(jspTable);
isshown = false;
TableSelector.this.getLayeredPane().validate();
TableSelector.this.getLayeredPane().repaint();
}
}
});
jspTable = new JScrollPane();
jspTable.setViewportView(table);
jspTable.setSize(new Dimension(350, 100));
}
return jspTable;
}
private JLabel getJLabel1() {
if(jLabel1 == null) {
jLabel1 = new JLabel();
jLabel1.setText("can you see this label?");
jLabel1.setBounds(0, 44, 165, 13);
}
return jLabel1;
}
}