在java中怎么点击表格中的一行数据,就可以将这些数据以及在数据库中的与之同源的数据显示到表格旁的控件上
4个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.datatransfer.*;
public class TableRespondToMouseClick {
private JTable table; // 表格
private JTextArea area; // 显示信息
private String[][] tableContent; // 表格内容
private int selectedRow; // 选中行
private int selectedColumn; // 选中列
public TableRespondToMouseClick() {
JFrame f = new JFrame();
init(f);
}
public void init(JFrame f) {
setLookAndFeel();
addTable(f);
f.setAlwaysOnTop(true);
f.pack();
f.setDefaultCloseOperation(0x3);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
}
public void addTable(JFrame f) {
JLabel title = new JLabel("图 书 信 息 一 览", JLabel.CENTER) {
{
setFont(new Font("微软雅黑", Font.BOLD, 18));
}
};
String[] tableName = { "书名", "作者", "出版社", "出版日期", "索取号" };
tableContent = new String[][] {
{ "思想是舍利子:流行于亚洲腹地千余年的静思法则", "戴逸如", "华东师范大学出版社", "2001-1",
"J228/4334" },
{ "穷人的幸福", "( 韩 ) 柳时和", "上海科学技术文献出版社", "2004-4-1",
"I312.665/4762" },
{ "法拉利传: F1 世界的引领者", "理查德 . 威廉姆斯", "国际文化出版公司", "2006 年 2 月",
"K835.465.47=535/5304" },
{ "陈墨评金庸.细品金庸", "陈墨", "东方出版社", "2008-07", "I207.425/7460" },
{ "恰到好处的生活", "纳什", "高等教育", "08-5-1", "B848.4-49/2424" },
{ "自主学习:学与教的原理和策略", "庞维国", "华东师范大学出版社", "2004-5-1", "G791/0026" },
{ "创新与魅力:现代日本零售业发展概览", "朱桦", "上海科学技术文献出版社", "2008-1-1",
"F733.134.2/2544" } };
DefaultTableModel model = new DefaultTableModel();
model.setDataVector(tableContent, tableName);
table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer,
int rowIndex, int vColIndex) {
Component c = super.prepareRenderer(renderer, rowIndex,
vColIndex);
if (c instanceof JLabel) {
JLabel jc = (JLabel) c;
jc.setToolTipText((String) getValueAt(rowIndex, vColIndex));
}
return c;
}
};
table.setModel(model);
table.setRowHeight(30);
table.getTableHeader().setBackground(new Color(252, 168, 251));
table.setCellSelectionEnabled(true);
table.setGridColor(new Color(166, 228, 143));
table.getTableHeader().setReorderingAllowed(false);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setSelectionBackground(new Color(198, 220, 255));
table.setSelectionForeground(new Color(232, 48, 56));
table.setPreferredScrollableViewportSize(new Dimension(550, 150));
table.addMouseListener(new MouseAdapter() {
private int r, c;
@Override
public void mousePressed(MouseEvent e) {
r = table.rowAtPoint(e.getPoint());
c = table.columnAtPoint(e.getPoint());
selectedRow = r;
selectedColumn = c;
if (r >= 0 && r < table.getRowCount()
&& (c >= 0 && c <= table.getColumnCount())) {
area.setText((String) tableContent[r][c]);
}
}
@Override
public void mouseReleased(MouseEvent e) {
r = table.rowAtPoint(e.getPoint());
c = table.columnAtPoint(e.getPoint());
selectedRow = r;
selectedColumn = c;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
JPopupMenu popup = new createPopUp();
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
JScrollPane scr = new JScrollPane(table);
f.getContentPane().add(title, BorderLayout.NORTH);
f.getContentPane().add(scr, BorderLayout.CENTER);
area = new JTextArea(5, 0);
f.getContentPane().add(area, BorderLayout.SOUTH);
}
class createPopUp extends JPopupMenu {
public createPopUp() {
JMenuItem myMenuItem = new JMenuItem(new Information());
JMenuItem myMenuItem2 = new JMenuItem(new Copy());
myMenuItem.setText("行信息");
myMenuItem2.setText("复制到剪贴板");
add(myMenuItem);
add(myMenuItem2);
}
}
class Copy2Clipboard implements ClipboardOwner {
public void toClipboard() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkSystemClipboardAccess();
} catch (Exception e) {
}
}
Toolkit tk = Toolkit.getDefaultToolkit();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tableContent[selectedRow].length; i++) {
sb.append(tableContent[selectedRow][i]).append(" ");
}
StringSelection st = new StringSelection(sb.toString());
Clipboard cp = tk.getSystemClipboard();
cp.setContents(st, this);
}
public void lostOwnership(Clipboard clip, Transferable tr) {
// 什么也不做
}
}
class Copy extends AbstractAction {
public void actionPerformed(ActionEvent e) {
new Copy2Clipboard().toClipboard();
}
}
class Information extends AbstractAction {
public void actionPerformed(ActionEvent arg0) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tableContent[selectedRow].length; i++) {
sb.append(tableContent[selectedRow][i]).append(" ");
}
area.setText("");
area.setText(sb.toString());
}
}
public static void main(String[] args) {
new TableRespondToMouseClick();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.datatransfer.*;
public class TableRespondToMouseClick {
private JTable table; // 表格
private JTextArea area; // 显示信息
private String[][] tableContent; // 表格内容
private int selectedRow; // 选中行
private int selectedColumn; // 选中列
public TableRespondToMouseClick() {
JFrame f = new JFrame();
init(f);
}
public void init(JFrame f) {
setLookAndFeel();
addTable(f);
f.setAlwaysOnTop(true);
f.pack();
f.setDefaultCloseOperation(0x3);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
}
public void addTable(JFrame f) {
JLabel title = new JLabel("图 书 信 息 一 览", JLabel.CENTER) {
{
setFont(new Font("微软雅黑", Font.BOLD, 18));
}
};
String[] tableName = { "书名", "作者", "出版社", "出版日期", "索取号" };
tableContent = new String[][] {
{ "思想是舍利子:流行于亚洲腹地千余年的静思法则", "戴逸如", "华东师范大学出版社", "2001-1",
"J228/4334" },
{ "穷人的幸福", "( 韩 ) 柳时和", "上海科学技术文献出版社", "2004-4-1",
"I312.665/4762" },
{ "法拉利传: F1 世界的引领者", "理查德 . 威廉姆斯", "国际文化出版公司", "2006 年 2 月",
"K835.465.47=535/5304" },
{ "陈墨评金庸.细品金庸", "陈墨", "东方出版社", "2008-07", "I207.425/7460" },
{ "恰到好处的生活", "纳什", "高等教育", "08-5-1", "B848.4-49/2424" },
{ "自主学习:学与教的原理和策略", "庞维国", "华东师范大学出版社", "2004-5-1", "G791/0026" },
{ "创新与魅力:现代日本零售业发展概览", "朱桦", "上海科学技术文献出版社", "2008-1-1",
"F733.134.2/2544" } };
DefaultTableModel model = new DefaultTableModel();
model.setDataVector(tableContent, tableName);
table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer,
int rowIndex, int vColIndex) {
Component c = super.prepareRenderer(renderer, rowIndex,
vColIndex);
if (c instanceof JLabel) {
JLabel jc = (JLabel) c;
jc.setToolTipText((String) getValueAt(rowIndex, vColIndex));
}
return c;
}
};
table.setModel(model);
table.setRowHeight(30);
table.getTableHeader().setBackground(new Color(252, 168, 251));
table.setCellSelectionEnabled(true);
table.setGridColor(new Color(166, 228, 143));
table.getTableHeader().setReorderingAllowed(false);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setSelectionBackground(new Color(198, 220, 255));
table.setSelectionForeground(new Color(232, 48, 56));
table.setPreferredScrollableViewportSize(new Dimension(550, 150));
table.addMouseListener(new MouseAdapter() {
private int r, c;
@Override
public void mousePressed(MouseEvent e) {
r = table.rowAtPoint(e.getPoint());
c = table.columnAtPoint(e.getPoint());
selectedRow = r;
selectedColumn = c;
if (r >= 0 && r < table.getRowCount()
&& (c >= 0 && c <= table.getColumnCount())) {
area.setText((String) tableContent[r][c]);
}
}
@Override
public void mouseReleased(MouseEvent e) {
r = table.rowAtPoint(e.getPoint());
c = table.columnAtPoint(e.getPoint());
selectedRow = r;
selectedColumn = c;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
JPopupMenu popup = new createPopUp();
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
JScrollPane scr = new JScrollPane(table);
f.getContentPane().add(title, BorderLayout.NORTH);
f.getContentPane().add(scr, BorderLayout.CENTER);
area = new JTextArea(5, 0);
f.getContentPane().add(area, BorderLayout.SOUTH);
}
class createPopUp extends JPopupMenu {
public createPopUp() {
JMenuItem myMenuItem = new JMenuItem(new Information());
JMenuItem myMenuItem2 = new JMenuItem(new Copy());
myMenuItem.setText("行信息");
myMenuItem2.setText("复制到剪贴板");
add(myMenuItem);
add(myMenuItem2);
}
}
class Copy2Clipboard implements ClipboardOwner {
public void toClipboard() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkSystemClipboardAccess();
} catch (Exception e) {
}
}
Toolkit tk = Toolkit.getDefaultToolkit();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tableContent[selectedRow].length; i++) {
sb.append(tableContent[selectedRow][i]).append(" ");
}
StringSelection st = new StringSelection(sb.toString());
Clipboard cp = tk.getSystemClipboard();
cp.setContents(st, this);
}
public void lostOwnership(Clipboard clip, Transferable tr) {
// 什么也不做
}
}
class Copy extends AbstractAction {
public void actionPerformed(ActionEvent e) {
new Copy2Clipboard().toClipboard();
}
}
class Information extends AbstractAction {
public void actionPerformed(ActionEvent arg0) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < tableContent[selectedRow].length; i++) {
sb.append(tableContent[selectedRow][i]).append(" ");
}
area.setText("");
area.setText(sb.toString());
}
}
public static void main(String[] args) {
new TableRespondToMouseClick();
}
}
展开全部
不知道你为什么要两个都显示。点击时间中可以先更新本行数据,在把数据写到控件上。更新时可以根据行数据的主键字段查询 获取到对象 然后传值
追问
那怎么响应点击表格行数据的事件呢?
追答
那就详细说下吧 知道的可以跳过不看
public class WgGUI extends JFrame implements ActionListener {..} 实现ActionListener,然后获得你表格行的控件对象,然后比如对象为o ,用
o.addMouseListener(
new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO 这里写要实现的操作
}
.....还有其他的MouseEvent 的Override方法 这里省略
);
注意 这里不是先添加事件后在actionPerformed方法实现。这个写法是java的around模式
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有听明白
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-07-14
展开全部
我也遇到同样的问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询