开发者

Java Swing refresh JPanel

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 !开发者_StackOverflow中文版= 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()");
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜