- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试用 JDialog
做的是......
JTable
的列根据this post和 this page ,可以通过单击标题并设置 JTableHeader
来选择列。
但是,它们似乎都不适合我想做的事情。
首先,我不确定将JTableHeader
放在哪里。上面的示例似乎已将其用于初始化,但我在编码中没有看到任何合适的空间来执行此操作。
至少我知道第二个例子是JPanel
。因此,为了在 JDialog
中拥有 JTableHeader
,JTableHeader
需要设置在完全不同的位置,因为默认情况下无法手动修改 JDialog 的 initComponents()
。
此外,我找不到如何选择标题(与单个单元格不同)。我假设我需要事先设置一个 JTableHeader
。
最后,我没有看到任何方法来检测选择了哪一列。至少我找到了jTable.getValueAt(int, int)
方法,但这个方法似乎是为了获取单个单元格。
现在我怀疑用 JTable 和 JDialog 可能无法完成它们。如果您能提供任何见解,我将不胜感激。
我添加了 initComponents()
的一部分,以便您轻松理解它。
private void initComponents() {
//here are irrelevant codes
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("MS UI Gothic", 3, 18)); // NOI18N
jLabel1.setText("Choose level(s) or unit(s)");
//irrelevant codes
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"EN6", "EN3", "EN5", "IN1"},
{"EN2", "EN3", null, "IN4"},
{null, null, null, "IN1"},
{null, null, null, "IN2"},
new String [] {
"EN2", "EN3", "EN5", "IN1"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
//lots of lines, seem to be irrelevant
pack();
}
最佳答案
"[...]it would be possible to select a column by clicking the header, by setting a JTableHeader."
根据您的要求,我认为您不需要提供自己的表头,而是附加 MouseListener改为默认值。这样,并使用行和列选择模型,您就可以轻松实现您的目标。
final JTable table = new JTable(tableModel);
table.setColumnSelectionAllowed(true);
table.getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Get the right column based on MouseEvent#getPoint()
int columnIndex = table.columnAtPoint(e.getPoint());
// Set this column as the selected one in the columns selection model
table.getColumnModel().getSelectionModel().setSelectionInterval(columnIndex, columnIndex);
// Set all the rows as the selected ones in the rows selection model
table.getSelectionModel().setSelectionInterval(0, table.getRowCount() - 1);
// Print the values in selected column
for (int rowIndex = 0; rowIndex < table.getRowCount(); rowIndex++) {
System.out.println(table.getValueAt(rowIndex, columnIndex));
}
}
});
注意:不要忘记允许选择列。
参见:
关于java - 如何通过选择标题来选择 JDialog 中 JTable 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28210022/
如何在另一个 JDialog 中添加 JDialog? 最佳答案 JDialog secondDialog = new JDialog(this); // ("this" is the first J
我有一个 JDialog,其中有一个可打开新窗口的按钮。我想要做的是每当其他窗口打开时阻止此 JDialog。当我说阻止时,我的意思是用户无法操纵它,无法移动它或最大化或任何东西。 顺便问一下,对于带
我有一个从 JFrame 上的按钮打开的父 JDialog。父 JDialog 本身有一个从父对话框上的按钮打开的子 JDialog。当我关闭父对话框并使用框架上的按钮再次打开它时,我不希望子对话框也
抱歉这个奇怪的标题,但这是解释。所以我有一个带有学生列表的 StudentRepository 类,这些学生是在 GUI 上选择的(通过 TableModel)。学生对象的属性是: int stude
我有一个未修饰的模态 JDialog,当用户在模态对话框外单击时,我想将其设置为 setVisible(false)。 这在 Swing 中可能吗? 我正在做的是为日期选择器之类的文本字段弹出一个自定
我有一个模式设置对话框,它是一个 JDialog。在这个设置窗口中,我放置了一些组件,包括一个按钮到另一个模态设置对话框,它也是一个 JDialog。我制作了它们 JDialogs,因为这是我所知道的
我只想在单击 JDialog 之外时关闭 JDialog import javax.swing.JDialog; import javax.swing.JLabel; public class Dia
我正在尝试重现我在多个应用程序中看到的功能:我有一个带有多个 JDialog 的 GUI 应用程序。我想轻松地将它们紧密地组织在屏幕上:当我移动一个 JDialog,并且它的一个边界变得“接近”(例如
对 Java 相当陌生,并且遇到了 z 顺序问题。我有一个旧版 Java 应用程序,它有一个主窗口 A,它会弹出一个模式 JDialog B。单击 B 上的按钮后,会弹出一个模式对话框 C。 对于从
我的应用程序中有多个 JDialogs 存储在 map 中。这些JDialogs都有 setModel(false); 当这些对话框失去焦点并且我想将特定的 JDialog 带到前面时,所有 JDia
我有一个扩展 JDialog 的类。当 JDialog 显示时,我单击其启动 Jframe 的显示按钮,但在关闭 JDialog 之前我无法访问 JFrame。当屏幕上存在 JDialog 时,如何访
场景是这样的我的 JFrame 有一个按钮,单击它会打开一个 JDialog,它是一个模型对话框。JDialog 有另一个按钮,我想打开另一个 JFrmae 点击它打开。 结果:另一个 Jframe
我有一个带有主 JFrame 的小型应用程序,它以模态方式打开 JDialog。在这个 JDialog 中,我启动了一个 javax.swing.Timer,它应该在 JDialog 关闭时停止。 p
如何将用户凭据传回到包含的 JFrame,以便 JFrame 知道特定用户? JFrame 有一个 main 方法。 包含的 JFrame 能否以某种方式从 Dialog 中获取用户? 当jbtOk
我基本上创建的是一个 JDialog,它在表单上有一个关键事件。因此,当例如按下空间时,它会做一些事情。在我在同一个对话框上创建一个可编辑的 JTextArea 之前,这种方法工作得很好。当我这样做时
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在构建我的第一个 gui,到目前为止一切正常,除了 JDialog 的故障。 .它在第一次使用时相应地接受名称和进程列表。但是当我把它拉回来输入新的输入时,它仍然没有响应。我认为这不是线程问题,因
如何创建一个 Modal JDialog,在任务正在处理时显示“正在加载”,并在超过 3 秒后显示? 最佳答案 为了扩展 Paul 的回答,SwingWorker 可以很好地运行您的后台任务。然后您可
该程序大部分运行正常,但没有打开任何窗口。它应该在桌面右下角显示一个小对话框。但是对于另一个人来说,编译相同的代码没有问题。我们有相同的 Java 运行时 (1.8_u40)。我该如何解决这个问题?
我正在使用 Eclipse 的 Window Builder 插件。 当我执行以下代码时,它正确显示JDialog。我原本希望 JDialog 也能显示在设计选项卡中(在设计时),但它不会。 pack
我是一名优秀的程序员,十分优秀!