Java Swing 刷新 JPanel
我有一个 Applet 类(扩展 JApplet
)。在类中,我实例化一个 JPanel 并初始化一个 JButton setEnabled(true)
。用户在面板中单击此按钮并进行一些处理后,我调用 JPanel 内部的方法来更新面板。然后,我执行 setEnabled(false)
单击 JPanel 上的按钮。
但是,在我在主面板上调用 add(ScrollPane)
后,JPanel 未正确“刷新”。在处理并将 JButton 设置为未启用(并且我确认那里有正确的数据等)之后,JPanel 仍然处于其初始化形式。
换句话说,我需要做什么才能在小程序内的 JPanel 上调用 add(JScrollPane)
实际上刷新面板?
基本上我想知道:如果您更新嵌套在 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()");
}
}
I have a Applet class (extends JApplet
). Inside the class I am instantiating a JPanel and initializing a JButton whit setEnabled(true)
. After the user clicks this button in the Panel and does some processing, I call a method inside of the JPanel to update the panel. I then do setEnabled(false)
the button clicked on the JPanel.
However, the JPanel is not "refreshing" correctly after I call add(ScrollPane)
on main panel. After the processing and setting the JButton to not enabled (and I confirmed that the right data is there etc), the JPanel still is in its initialized form.
In other words, what do I need to do so that calling add(JScrollPane)
on a JPanel within a applet actually refreshes the Panel?
Basically i'm wondering: if you update the panel inside a swing component which is nested inside of a JApplet, should the update be visible? What needs to be done to refresh if not?
THIS IS THE CODE:
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()");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不应该在操作中打开连接。使用 SwingWorker 或其他执行器来执行此操作。
其次,尝试在添加滚动窗格后在面板上调用 revalidate() 。这两个修复应该可以解决问题。
First off, you shouldn't be opening a connection within the action. Use a SwingWorker or some other executor to do this.
Secondly, try calling revalidate() on the panel after adding the scrollpane. These two fixes should solve the problem.