我当前正在构建一个应用程序,它有一个 JFrame
和一个 JDialog
。 JFrame
有一个 JList
名为:
JList lstMainVenuesEvents = new JList();
我正在尝试使用以下方法获取 lstMainVenuesEvents 的值:
lstMainVenuesEvents.getSelectedIndex();
我可以在 JFrame
上完美地获取该值,但如何将其传递到我的 JDialog
?我考虑过在我的一个类文件中创建一个 setter 方法,然后从我的 JDialog
文件中获取该值,但肯定有一种简单的方法吗?是否可以使用某种方法将数据从 JFrame 传递到 JDialog ,就像 PHP 中的 POST 请求一样?
如果我错过了任何重要的内容,请道歉。
更新:这是我的 JList 和 JDialog 显示的代码。
JList lstMainVenuesEvents = new JList();
lstMainVenuesEvents
.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// stop from firing twice
if (e.getValueIsAdjusting()) {
EventModify evtWindow = new EventModify();
evtWindow.setVisible(true);
}
}
});
最佳答案
我不能确信它是“正确的”,但控制反转类型的方法通常会减少传递值。
假设值lstMainVenuesEvents.getSelectedIndex()
用于JDialog
中的特定操作/事件,您可以从JFrame
。
// some where in the JFrame
jDialog.setButtonPressed(new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
// lstMainVenuesEvents.getSelectedIndex() is accessible in this block
// put code logic here where
}
});
关于java - 将 JList 值从 JFrame 传递到 JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574586/