简体   繁体   中英

how can I get my JLabel icon to refresh?

i have a program to show a static Google map after inputting data but when i want to refresh my map the JLabel doesn't change. here is my code for the buttons one just clears the map:

    JButton btnMakeMap = new JButton("Make map");
        btnMakeMap.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                Map map = new Map(txfPlace.getText(), cmbMarkerName.getSelectedItem().toString().charAt(0), cmbMrkColour.getSelectedItem().toString(), Integer.parseInt(cmbZoom.getSelectedItem().toString()), 
                        cmbMrkSize.getSelectedItem().toString(), chbCenter.isSelected(), cmbType.getSelectedItem().toString());

                if(chbCenter.isSelected()){
                map.genMarker();
                map.genURlWcenter();
                }else{
                    map.genMarker();
                    map.genURl();
                }

                lblMap.setIcon(null);
                map.genimage();
                ImageIcon im = new ImageIcon("map.jpg");
                lblMap.setIcon(im);


            }
        });
        btnMakeMap.setBounds(235, 146, 117, 49);
        pnlSettings.add(btnMakeMap);

        JButton btnRefresh = new JButton("clear map");
        btnRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

//              Map map = new Map(txfPlace.getText(), cmbMarkerName.getSelectedItem().toString().charAt(0), cmbMrkColour.getSelectedItem().toString(), Integer.parseInt(cmbZoom.getSelectedItem().toString()), 
//                      cmbMrkSize.getSelectedItem().toString(), chbCenter.isSelected(), cmbType.getSelectedItem().toString());
                lblMap.setIcon(null);
//              map.genimage();
//              ImageIcon im = new ImageIcon("map.gif");
//              lblMap.setIcon(im);
//              
            }
        });
        btnRefresh.setBounds(406, 172, 89, 23);
        pnlSettings.add(btnRefresh);

this is the code for the class it uses:

package com.mymaps;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.http.HttpConnection;
import org.jboss.remoting.transport.Connector;

public class Map {

    private String URl = "https://maps.googleapis.com/maps/api/staticmap?";
    private int Zoom = 1;
    private String Location = "";
    private String markColor = "";
    private String Marker = "";
    private char markName;
    private String markSize = "";
    private String mapSize = "&size=400x400";
    private boolean wantMarkeratCenter = false;
    private String mapType = "maptype=";

    public Map(String location, char name, String colour, int zoom,
            String mrksize, boolean center, String type) {
        Location = location;
        markName = name;
        markColor = markColor + colour;
        markSize = mrksize;
        Zoom = zoom;
        wantMarkeratCenter = center;
        mapType = mapType + type;

        fixLocation();

        // if (wantMarkeratCenter){//check if the user wants a marker at the
        // center
        // genMarker();
        // genURlWcenter();
        // }else{
        // if(!wantMarkeratCenter){
        // genMarker();
        // genURl();
        // }
        // }
    }

    public boolean validateURl() {// validates URl length
        boolean isValid = false;
        if (URl.length() < 0 || URl.length() > 2048) {
            isValid = true;
        } else {
            isValid = false;// y? just because
        }
        return isValid;
    }

    public void genURl() {// creates the URl for use

        if (wantsSize()) {
            URl = URl + mapSize + "&zoom=" + Zoom + "&" + mapType + "&"
                    + Marker;
        } else {
            URl = URl + "&" + mapType + Marker;
        }
    }

    public void genURlWcenter() {// generates URl with the center on the
                                    // location point

        String loc = Location.replace('+', ',');

        if (wantsSize()) {
            URl = URl + "center=" + loc + mapSize + "&" + mapType + "&" + Marker;
        } else {
            URl = URl;
        }
    }

    public void genMarker() {// generates the marker
        String name = "" + markName;

        Marker = "markers=color:" + markColor + "|label:" + name.toUpperCase()
                + "|" + Location;
    }

    private boolean wantsSize() {// checks if the user wanted a size or not
                                    // based on the var size
        boolean wantSize = false;
        if (mapSize.length() != 0) {
            wantSize = true;
        }
        return wantSize;
    }

    public String getMarker() {
        return Marker;
    }

    public String getURl() {
        return URl;
    }

    public void genimage() {

        URL url;
        try {
            url = new URL(URl);
            System.out.println(url);
            System.out.println();

            InputStream is = url.openStream();

            OutputStream os = new FileOutputStream("map.jpg");

            byte[] b = new byte[2048];
            int length;

            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }

            is.close();
            os.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void fixLocation() {

        Scanner sc = new Scanner(Location);
        String temp = "";
        while (sc.hasNext()) {
            temp = temp + sc.next() + "+";
        }
        temp = temp.substring(0, temp.length() - 1);
        Location = temp;
        sc.close();

    }

    public String getLocation() {
        return Location;
    }

    public ImageIcon getImage() {
        ImageIcon im = new ImageIcon("map..jpg");
        return im;
    }




}

this is a runnable version of the code here

            lblMap.setIcon(null);
            map.genimage();
            ImageIcon im = new ImageIcon("map.jpg");
            lblMap.setIcon(im);

Looks like you are creating the map.jpg and binding it to label when you want to refresh. with this code, do you still see old image after refresh? Did you verified that the map.jpg is latest? If yes then could be issue with OS paging and OS still returning the old file when JVM asked for it. You can solve this by generating unique file name everytime on refresh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM