Hello, i made a basic java desktop application.
After building the interface in the form, i compiled it and it worked perfectly.
But after i added an event to a button (on click) my app won't close. I use the X button from the title bar, or ctrl+q from the menu but in vain.

I am a beginner in swing, would someone tell me what i did wrong?

private void buttonGetMouseClicked(java.awt.event.MouseEvent evt) {
        try {
            URL url = new URL(jTextField1.getText());
            Writer head = new StringWriter();
            Writer body = new StringWriter();
            alexHttp httpCon = new alexHttp(); 
            httpCon.httpGet(head, body);
            HeaderPane.setText(head.toString()); //headerpane [jtextpane]
            webpage.setText(body.toString());  //webpage[jeditorpane]
            head.close();
            body.close();

        }catch(Throwable e){
            e.printStackTrace();
        }
    }

Edited 6 Years Ago by Alex_: n/a

Great, but where to put it? Because i don't want my buttons to close my application. When i said the "x"button , i meant the one in the top right of the window.

I think i didn't finish something, or did it wrong, because my app worked fine with just the frame. After i added some code (post #1) it didn't close normaly. I have to do kill from the task manager and then close it from the ide.

Edited 6 Years Ago by Alex_: n/a

Please try this code after the statement

body.close();

. The statement is

setDefaultCloseOperation(EXIT_ON_CLOSE)

;

You mean like this:

setDefaultCloseOperation(EXIT_ON_CLOSE);
body.close();

?

Hm i used a form, and it inherited from JFrameView ..or something like this. I guess i have to do it by hand.

Edited 6 Years Ago by Alex_: n/a

You told us this is a desktop application. What is a form? Just tell us what type of Objects you are using for your window... otherwise how are we supposed to help you?

O form is where you visually edit your application's GUI.

[S] It does close now, anyway. I guess it has been an OS or java virtual machine's problem. it works fine now with setDefaultCloseOperation(EXIT_ON_CLOSE);
hope i won't have problems again. [/S]
ok,sorry, it actually closes only if i don't do anything than just pressing the X button right after it's launch. It still does not close after i click the button.

here is the full code

/*
 * HttpClientView.java
 */

package httpclient;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import java.net.URL;
import java.io.Writer;
import java.io.StringWriter;
/**
 * The application's main frame.
 */
public class HttpClientView extends FrameView {

    public HttpClientView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
        getFrame().setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = HttpClientApp.getApplication().getMainFrame();
            aboutBox = new HttpClientAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        HttpClientApp.getApplication().show(aboutBox);
    }

    /** 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">                          
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        buttonGet = new javax.swing.JButton();
        buttonPost = new javax.swing.JButton();
        buttonDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        HeaderPane = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        webpage = new javax.swing.JEditorPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(httpclient.HttpClientApp.class).getContext().getResourceMap(HttpClientView.class);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        buttonGet.setText(resourceMap.getString("buttonGet.text")); // NOI18N
        buttonGet.setName("buttonGet"); // NOI18N
        buttonGet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGetActionPerformed(evt);
            }
        });

        buttonPost.setText(resourceMap.getString("buttonPost.text")); // NOI18N
        buttonPost.setName("buttonPost"); // NOI18N
        buttonPost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPostActionPerformed(evt);
            }
        });

        buttonDelete.setText(resourceMap.getString("buttonDelete.text")); // NOI18N
        buttonDelete.setName("buttonDelete"); // NOI18N
        buttonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        HeaderPane.setName("HeaderPane"); // NOI18N
        jScrollPane1.setViewportView(HeaderPane);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        webpage.setName("webpage"); // NOI18N
        jScrollPane2.setViewportView(webpage);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonGet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonPost)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDelete)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonGet)
                    .addComponent(buttonPost)
                    .addComponent(buttonDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(httpclient.HttpClientApp.class).getContext().getActionMap(HttpClientView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 351, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>                        

    private void buttonGetActionPerformed(java.awt.event.ActionEvent evt) {                                          
        try {
            URL url = new URL(jTextField1.getText());
            Writer head = new StringWriter();
            Writer body = new StringWriter();
            alexHttp httpCon = new alexHttp(url);
            httpCon.httpGet(head, body);
            HeaderPane.setText(head.toString());
            webpage.setText(body.toString());
            head.close();
            body.close();
            //jTextField1.

        }catch(Throwable e){
            e.printStackTrace();
        }
    }                                         

    private void buttonPostActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
            URL url = new URL(jTextField1.getText());
            Writer head = new StringWriter();
            Writer body = new StringWriter();
            alexHttp httpCon = new alexHttp(url);
            httpCon.httpPost(head, body);
            HeaderPane.setText(head.toString());
            webpage.setText(body.toString());
            head.close();
            body.close();

        }catch(Throwable e){
            e.printStackTrace();
        }
    }                                          

    private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                             
        try {
            URL url = new URL("http://localhost:8084/WebApplication1/doDelete");
            Writer head = new StringWriter();
            alexHttp httpCon = new alexHttp(url);
            int serverResponseCode =  httpCon.httpDelete(head);
            httpCon.httpDelete(head);
            HeaderPane.setText(head.toString());
            webpage.setText(""+serverResponseCode);
            head.close();

        }catch(Throwable e){
            e.printStackTrace();
        }
    }                                            


    // Variables declaration - do not modify                     
    private javax.swing.JTextPane HeaderPane;
    private javax.swing.JButton buttonDelete;
    private javax.swing.JButton buttonGet;
    private javax.swing.JButton buttonPost;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JEditorPane webpage;
    // End of variables declaration                   

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}

Edited 6 Years Ago by Alex_: n/a

And here is the screenshot. Could not edit the previous post 'cause of the 30 min limit.

Attachments Screenshot-AlexandruZaim%27sApplication.png 11.96 KB
This question has already been answered. Start a new discussion instead.