gpt4 book ai didi

java - Jdialog 中的组件未显示

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

我的应用程序是使用 Netbeans IDE (8.0.2) 创建的。我创建了一个 JFrame,其中包含绑定(bind)到数据库的 JTable(使用 JPA)。

我添加了一个“刷新”按钮,用于直接从数据库“刷新”JTable 数据。

我希望在获取数据时显示“请稍候”消息。

为此,我实现了一个扩展 JDialog 的 JDialog_PleaseWait 类。

出于某种奇怪的原因,尽管显示了 JDialog,但它包含的 jLabel 并未显示...

JDialog_PleaseWait 类是:

 public class JDialog_PleaseWait extends javax.swing.JDialog {

//constructor for PleaseWait jDialogs

public JDialog_PleaseWait(String messageToDisplay){
initComponents();
this.jLabel_WaitMessage.setText(messageToDisplay);

}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jLabel_WaitMessage = new javax.swing.JLabel();

setTitle("Please wait...");
setAlwaysOnTop(true);
setBackground(new java.awt.Color(227, 248, 115));
setModalityType(java.awt.Dialog.ModalityType.MODELESS);
setResizable(false);
setType(java.awt.Window.Type.POPUP);

jLabel_WaitMessage.setBackground(new java.awt.Color(242, 253, 153));
jLabel_WaitMessage.setText("WaitMessage");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel_WaitMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel_WaitMessage)
);

pack();
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel_WaitMessage;
// End of variables declaration//GEN-END:variables

}

刷新 JButton 调用名为“reload”的方法,该方法最初必须显示 jDialog,然后执行其余任务。更具体地说:

public void reload(){

jTable_Activities.setEnabled(false); // freezes the JTable

JDialog_PleaseWait pleaseWaitDialog = new JDialog_PleaseWait("Communicating with database server...."); // create a new PleaseWait JDialog

pleaseWaitDialog.pack();
pleaseWaitDialog.setLocationRelativeTo(this); //relative to this frame
pleaseWaitDialog.setVisible(true); //display the JDialog

.... ....
// runs a DB query and updates a JTable
.... ....

因此,由于某种原因,JDialog 窗口弹出,但 jLabel 未显示...

我(认为我)已经对其他 JDialog 做了(确切的?)同样的事情,这些 JDialog 工作正常,但由于某些奇怪的原因,这个 JDialog 无法正常工作......

有什么提示吗?

最佳答案

您可能的问题是您正在 Swing 事件线程上获取数据(我在上面没有看到任何使用 Thread/Runnable/SwingWorker 等的代码,因此是我的假设),这是绑定(bind)事件线程并阻止它执行其杂务——包括将标签绘制到 JDialog。解决方案:在后台线程中获取数据,例如使用 SwingWorker。

这是一个演示我的意思的例子。该代码创建两个 JButton,其中一个显示 JDialog 2 秒,其中包含 Thread.sleep(...)在那 2 秒内,在 Swing 事件线程上运行,另一个使用 Thread.sleep(...)在后台线程中运行。编译并运行代码看看会发生什么。

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class PleaseWaitDialogTest extends JPanel {
protected static final long SLEEP_TIME = 2000L;

public PleaseWaitDialogTest() {
add(new JButton(new ShowWaitDialog("Without Thread", KeyEvent.VK_O, false)));
add(new JButton(new ShowWaitDialog("With BG Thread", KeyEvent.VK_W, true)));
}

private class ShowWaitDialog extends AbstractAction {
private boolean useBackgroundThread;
private JDialog dialog;

public ShowWaitDialog(String name, int mnemonic,
boolean useBackgroundThread) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.useBackgroundThread = useBackgroundThread;
}

@Override
public void actionPerformed(ActionEvent e) {
// create dialog in a lazy way
if (dialog == null) {
Window ancestorWindow = SwingUtilities
.getWindowAncestor(PleaseWaitDialogTest.this);
String title = "Dialog: " + getValue(NAME);
dialog = new JDialog(ancestorWindow, title,
ModalityType.MODELESS);
dialog.getContentPane().setLayout(new GridBagLayout());
dialog.add(new JLabel("Please Wait"));
dialog.setPreferredSize(new Dimension(250, 150));
dialog.pack();
dialog.setLocationByPlatform(true);
}
dialog.setVisible(true);

// since the dialog is non-modal, this code will run immediately after
// the dialog has been set visible
CloseRunnable closeRunnable = new CloseRunnable(dialog, SLEEP_TIME);
if (useBackgroundThread) {
// run the Runnable in a background thread
new Thread(closeRunnable).start();
} else {
// run the Runnable directly on the Swing event thread
closeRunnable.run();
}
}
}

private class CloseRunnable implements Runnable {
protected JDialog dialog;
private long sleepTime;

public CloseRunnable(JDialog dialog, long sleepTime) {
this.dialog = dialog;
this.sleepTime = sleepTime;
}

@Override
public void run() {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}

// the dialog *must* be closed on the Swing event thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (dialog != null) {
dialog.setVisible(false);
}
}
});
}
}

private static void createAndShowGui() {
PleaseWaitDialogTest mainPanel = new PleaseWaitDialogTest();

JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - Jdialog 中的组件未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32796931/

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