我有一个Applet类(扩展JApplet
)。在课程中,我正在实例化JPanel并初始化JButton whit setEnabled(true)
。用户在Panel中单击此按钮并进行一些处理后,我调用JPanel内部的方法来更新面板。然后,我点击了JPanel上的setEnabled(false)
按钮。
但是,在主面板上调用add(ScrollPane)
后,JPanel没有正确“刷新”。在处理并将JButton设置为未启用(并且我确认正确的数据存在等)之后,JPanel仍然处于其初始化形式。
换句话说,我需要做什么才能在applet中的JPanel上调用add(JScrollPane)
实际刷新Panel?
基本上我想知道:如果更新嵌套在JApplet内部的swing组件中的面板,那么更新是否可见?如果没有需要做什么来刷新?
这是代码:
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (STAMPA_COMMAND.equals(command)) {
stampa.setEnabled(false);
JPanel areaPrint = new JPanel();
JLabel lab = new JLabel("Wait Printing...");
areaPrint.setBackground(Color.magenta);
areaPrint.add(lab);
scrollArea.getViewport().add(areaPrint); // THIS IS THE PROBLEM...THE CHANGE ARE NOT REFRESHED
try {
PrintPdf printPDFFile;
ArrayList assegniDaStampare = new ArrayList();
for (int i = 0; i < assegni.size(); i++) {
DatiAssegno datiAss = (DatiAssegno) assegni.get(i);
if (datiAss != null && datiAss.getStatoAssegno().equals(STATUS_OK)) {
printPDFFile = new PrintPdf("Stampa Assegni", datiAss);
printPDFFile.print();
String servletLocation = "http://localhost/Servlet";
// connect to the servlet
URL studentDBservlet = new URL(servletLocation);
URLConnection servletConnection = studentDBservlet.openConnection();
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
OutputStream outstream = servletConnection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(datiAss.idAssegno);
oos.flush();
oos.close();
ObjectInputStream inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
try {
String statusSave = (String) inputFromServlet.readObject();
} catch (ClassNotFoundException e4) {
e4.printStackTrace();
}
}
}
JPanel areaPrint2 = new JPanel();
JLabel lab2 = new JLabel("Print Complete");
areaPrint2.setBackground(Color.green);
areaPrint2.add(lab2);
scrollArea.getViewport().add(areaPrint2);
} catch (FileNotFoundException e1) {
//do something
} catch (IOException e2) {
//do something
} catch (PrinterException e3) {
//do something
}
}
if (EXIT_COMMAND.equals(command)) {
JSObject win = JSObject.getWindow(appletParent);
appletParent.stop();
appletParent.destroy();
win.eval("self.close()");
}
}
答案 0 :(得分:1)
首先,您不应该在操作中打开连接。使用SwingWorker或其他执行程序执行此操作。
其次,在添加滚动窗格后尝试在面板上调用revalidate()。这两个修复应该可以解决问题。