Srikanth Sridhar
Srikanth Sridhar

Reputation: 2467

how to resize jws applet window?

I am using JWS to start my application. I need to reset my JFrame size and it should not be resized. I tried with frame.setSize(new Dimension(500, 500)); and frame.setResizable(false); which is not working. I went through the forum and tried other options available. My complete code is

/**
 * 
 */
package com.bus.oprs;

import java.util.Properties;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicBorders;
import javax.swing.plaf.basic.BasicBorders.ButtonBorder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.MalformedURLException;

// classes of the web-start API, used in this example.
import javax.imageio.ImageIO;
import javax.jnlp.BasicService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;

/**
 * @author sreekanth
 * @date Aug 29, 2012 11:33:16 AM
 * @version 1.0
 */
public class ApsrtcOprsJNLP extends JFrame {

    private static final long serialVersionUID = 8221867230431906706L;
    private Color titleColor = new Color(241,248,255);
    final private JPanel panel = new JPanel();
    final Properties properties = new Properties();
    /**
     * @throws IOException, URISyntaxException 
     */
    public ApsrtcOprsJNLP() throws IOException, URISyntaxException {
    super();
    launchApplication();
    }


    private void loadProperties() {
    try {
        InputStream inputStreamUrl = this.getClass().getResourceAsStream("/AppResources.properties");
        properties.load(inputStreamUrl);
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

    private void setButtonProperties(JButton button){
    try {
        Color buttonColor =  new Color(102,153,255);
        button.setBackground(buttonColor);
        button.setForeground(Color.WHITE);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

    private void launchApplication() throws IOException, URISyntaxException {
    JLabel clickMessageLabel = null;
    JLabel label = null;
    Dimension dimension = null;
    try {
        final BasicService basicService = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
        dimension = new Dimension(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.loadProperties();
        setTitle(properties.getProperty("app.title"));

        UIManager.put("InternalFrame.activeTitleBackground", new javax.swing.plaf.ColorUIResource(titleColor));  
        JFrame.setDefaultLookAndFeelDecorated(true); 
        panel.setBackground(titleColor);

        label = new JLabel(properties.getProperty("app.welcome.msg"));
        Font newLabelFont = new Font(label.getFont().getName(), Font.BOLD, label.getFont().getSize());

        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label);
        label.setFont(newLabelFont);

        clickMessageLabel = new JLabel(properties.getProperty("app.click.link.txt"));
        clickMessageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(clickMessageLabel);
        clickMessageLabel.setFont(newLabelFont);

        String[] jButton = properties.getProperty("urls").split(",");
        String[] jLabel = properties.getProperty("urlLabels").split(",");

        for (int i = 0; i < jLabel.length; i++) {
            JLabel labels = new JLabel(jLabel[i]);
            panel.add(labels);
            JButton button = new JButton(jButton[i]);
            this.setButtonProperties(button);
            button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
            try {
                URL url = new URL(ae.getActionCommand());
                basicService.showDocument(url);
                setVisible(false);
                dispose();
            } catch (MalformedURLException murle) {
                JOptionPane.showMessageDialog(panel,
                    murle.getMessage());
            }
            }
        });
            panel.add(button);
        }

        panel.setBorder(new EmptyBorder(8, 8, 8, 8));
        setSize(dimension);
        setResizable(false);
        getContentPane().add(panel);        
        pack();

        setLocationRelativeTo(null);
    } catch (UnavailableServiceException use) {
        use.printStackTrace();
        System.exit(-1);
    } finally {
        label = null;
        clickMessageLabel = null; dimension = null;
    }
    }
    /** 
     * Construct the GUI and display it.
     *  
     * @throws IOException 
     * @throws URISyntaxException
     */
    public static void main(String[] args) throws IOException, URISyntaxException {
    BufferedImage image = null;
    ApsrtcOprsJNLP app = new ApsrtcOprsJNLP();
    try {
        image = ImageIO.read(app.getClass().getResource("/logo.png"));
        app.setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    app.setIconImage(image);
    app.setVisible(true);
    }
}

JNLP :

<?xml version="1.0" encoding="UTF-8" ?>
<jnlp spec="1.0+"
      href="http://localhost:9999/apsrtc-oprs-jnlp/launch-app.jnlp">
  <information>
    <title>APSRTC-OPRS</title>
    <vendor>APSRTC</vendor>
    <description kind="one-line">
      APSRTC-OPRS WEB START
    </description>
    <icon kind="shortcut" href="logo.png" width="128" height="128"/>
    <shortcut online="false">
      <desktop/>
    </shortcut>
  </information>
  <resources>
    <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se" />
    <jar href="http://localhost:9999/apsrtc-oprs-jnlp/oprs-jnlp.jar" main="true" />
    <applet-desc
       name='apsrtc-oprs-jnlp'
       main-class='com.abhibus.oprs.ApsrtcOprsJNLP'
       width="800"
       height="800">
   </applet-desc>
  </resources>
 <application-desc main-class="com.abhibus.oprs.ApsrtcOprsJNLP"/>
</jnlp>

Thanks

Upvotes: 1

Views: 2004

Answers (1)

Thorn
Thorn

Reputation: 4057

Calling pack() will change the size to minimum required to fit the UI content placed in that window. If we remove this line, the dimensions should stay where you set them.

private void launchApplication() {
    /* other code omitted */
    panel.setBorder(new EmptyBorder(8, 8, 8, 8));
    setSize(dimension);
    setResizable(false);
    getContentPane().add(panel);        
    //pack();
}

Upvotes: 1

Related Questions