gpt4 book ai didi

java - 使用 JTable+focusLost() 错误显示数据

转载 作者:行者123 更新时间:2023-12-01 22:23:12 25 4
gpt4 key购买 nike

我希望在从多个表中选择任何行时在文本框中正确显示数据。

  • 当我右键单击表 1 的任意行时,我会得到 table1: 1。如果我单击表 2 的任何行,我也会得到 table1: 1 (但它应该是 table2: 1。)。
  • 当我左键单击表 1 或 2,然后右键单击时,我没有得到任何结果。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FocusTable extends JFrame {

private JPanel contentPane;
private JTextField textField; //Show selected row data here
private JTable table1;
private JTable table2;
private JTable table3;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FocusTable frame = new FocusTable();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FocusTable() {
setTitle("Test Focus Lost Table");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

textField = new JTextField();
contentPane.add(textField, BorderLayout.SOUTH);
textField.setColumns(10);

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);

//----------------Create JPopupMenu-----------------
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItemOpen = new JMenuItem("Show!");
popupMenu.add(menuItemOpen);
//---------------------------------------------------

table1 = new JTable();
table1.setComponentPopupMenu(popupMenu);
//Show menu by pressing the right button
table1.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {

if (SwingUtilities.isRightMouseButton( e )) {
Point point = e.getPoint();
int column = table1.columnAtPoint(point);
int row = table1.rowAtPoint(point);
table1.changeSelection(row, column, false, false);
}
}
});
//Clear selected row
table1.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
//textField.setText("Focus Lost table 1");
table1.clearSelection();
}
});
//Default model of Table 1
table1.setModel(new DefaultTableModel(
new Object[][] {
{"table1: 1", null, null, null, null},
{"table1: 2", null, null, null, null},
{"table1: 3", null, null, null, null},
{"table1: 4", null, null, null, null},
{"table1: 5", null, null, null, null},
{"table1: 6", null, null, null, null},
{"table1: 7", null, null, null, null},
{"table1: 8", null, null, null, null},
{"table1: 9", null, null, null, null},
},
new String[] {
"New column", "New column", "New column", "New column", "New column"
}
)
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
});
tabbedPane.addTab("TabTable1", null, table1, null);

table2 = new JTable();
table2.setComponentPopupMenu(popupMenu);
//Show menu by pressing the right button
table2.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {

if (SwingUtilities.isRightMouseButton( e )) {
Point point = e.getPoint();
int column = table2.columnAtPoint(point);
int row = table2.rowAtPoint(point);
table2.changeSelection(row, column, false, false);
}
}
});
//Clear selected row
table2.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
//textField.setText("Focus Lost table 2");
table2.clearSelection();
}
});
//Default model of Table 2
table2.setModel(new DefaultTableModel(
new Object[][] {
{null, "table2: 1", null, null, null},
{null, "table2: 2", null, null, null},
{null, "table2: 3", null, null, null},
{null, "table2: 4", null, null, null},
{null, "table2: 5", null, null, null},
{null, "table2: 6", null, null, null},
{null, "table2: 7", null, null, null},
{null, "table2: 8", null, null, null},
{null, "table2: 9", null, null, null},
},
new String[] {
"New column", "New column", "New column", "New column", "New column"
}
){
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
});
tabbedPane.addTab("TabTable2", null, table2, null);

table3 = new JTable();
//Clear selected row when focus lost
table3.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
table2.clearSelection();
}
});
//Show menu by pressing the right button
table3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {

if (SwingUtilities.isRightMouseButton( e )) {
Point point = e.getPoint();
int column = table3.columnAtPoint(point);
int row = table3.rowAtPoint(point);
table3.changeSelection(row, column, false, false);
}
}
});
//Default model of Table 3
table3.setModel(new DefaultTableModel(
new Object[][] {
{null, null, "table 3: 1", null, null},
{null, null, "table 3: 2", null, null},
{null, null, "table 3: 3", null, null},
{null, null, "table 3: 4", null, null},
{null, null, "table 3: 5", null, null},
{null, null, "table 3: 6", null, null},
{null, null, "table 3: 7", null, null},
{null, null, "table 3: 8", null, null},
{null, null, "table 3: 9", null, null},
},
new String[] {
"New column", "New column", "New column", "New column", "New column"
}
){
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
});
table3.setComponentPopupMenu(popupMenu);

tabbedPane.addTab("TabTable3", null, table3, null);

//---------------------------Checking the selected table-------------------------------
menuItemOpen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(table1.getSelectedRow()!=-1){
textField.setText(table1.getValueAt(table1.getSelectedRow(), 0).toString());
}else if(table2.getSelectedRow()!=-1){
textField.setText(table2.getValueAt(table2.getSelectedRow(), 1).toString());
}else if(table3.getSelectedRow()!=-1){
textField.setText(table3.getValueAt(table3.getSelectedRow(), 2).toString());
}
}
});
}

}

最佳答案

不要摆弄 FocusListener。无需清除表选择。用户不喜欢在从一个选项卡移动到另一个选项卡时行选择消失。因此,摆脱 FocusListener 并保持行选择不变。

相反,您可以通过执行以下操作来获取当前聚焦的表格:

menuItemOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

JMenuItem mi = (JMenuItem)e.getSource();
JPopupMenu popup = (JPopupMenu)mi.getParent();
JTable table = (JTable)popup.getInvoker();

if (table.equals(table1))
{
textField.setText(table1.getValueAt(table1.getSelectedRow(), 0).toString());
}
else if (table.equals(table2))
{
textField.setText(table2.getValueAt(table2.getSelectedRow(), 1).toString());
}
else if (table.equals(table3))
{
textField.setText(table3.getValueAt(table3.getSelectedRow(), 2).toString());
}
}
});

关于java - 使用 JTable+focusLost() 错误显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com