0

I am attempting to create a Java applet, and yes, I know, deprecated, blah blah blah, I have my reasons. I cannot seem to get the file to load for any reason via either placing it on my website or by using an HTML file to load it locally. Any help in this matter would be most appreciated, as this is frustrating me to no end.

<html>
<head>
<title>
Applet
</title>
</head>
<body>
<h2>Applet</h2>
<embed
archive="eloRating.jar" 
code="eloRatingSystem"
width=550 height=300>
 This means you done goofed
</applet>
</body>
</html>

.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

import java.awt.event.*;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class eloRatingSystem extends JApplet /*implements Runnable*/{
    /*****************************************************************************/
    private static final int BUTTON_HEIGHT = 50;
    private static final int BUTTON_WIDTH  = 150;
    /*****************************************************************************/
    private Button newPlayer, editPlayer, editTeams, commitMatch, downloadBook; 
    private JButton editPopUp;
    /*****************************************************************************/
    private JComponent[] inputs; 
    private JComponent[] tables;
    /*****************************************************************************/
    private int testRow;
    private int playerCounter;
    /*****************************************************************************/
    private ArrayList<Player> pList;
    /*****************************************************************************/
    private String[] titles;
    /*****************************************************************************/
    private ListenForAction lForAction;
    /*****************************************************************************/
    private modifyExcel book;
    /*****************************************************************************/
    private JPanel panel;
    /*****************************************************************************/
    private JTable playerTable;
    /*****************************************************************************/
    private TableRowSorter<?> sorter;
    /*****************************************************************************/
    Dimension d;
    /*****************************************************************************/

    /*****************************Initialization**********************************/
    public void init() {
        inputs         =   new JComponent[]{
                new JLabel("Enter The Player's Name"),
        };

        testRow        = 10000;

        playerCounter  = 0;

        lForAction     =   new ListenForAction();

        book           =   new modifyExcel();

        book.openExcel();

        titles         =   new String[6];

        panel          =   new JPanel();


        pList          =   new ArrayList<Player>();
        d              =   new Dimension(500,140);

        titles         =   book.setTitles();

        /*DEBUG
        JOptionPane.showMessageDialog(null, titles[0] + titles[1] + titles[2] + titles[3] + titles[4] + titles[5], "Work", JOptionPane.PLAIN_MESSAGE);
        */
        editPopUp      =   new JButton("Edit Player");

        newPlayer      =     new Button("Add Player");
        editPlayer     =     new Button("Edit Player");
        editTeams      =     new Button("Edit Teams");
        commitMatch    =     new Button("Commit Match");
        downloadBook   =   new Button("Download Excel File");


        setLayout(null);

        //editPopUp.setBounds(0,0,BUTTON_WIDTH,BUTTON_HEIGHT);
        newPlayer.setBounds(75, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
        editPlayer.setBounds(235, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
        editTeams.setBounds(395, 180, BUTTON_WIDTH, BUTTON_HEIGHT);
        commitMatch.setBounds(555, 180, BUTTON_WIDTH, BUTTON_HEIGHT);

        panel.add(editPopUp);

        newPlayer.addActionListener(lForAction);
        editPlayer.addActionListener(lForAction);
        editPopUp.addActionListener(lForAction);

        initPlayers(book.getRows());

        //System.out.println(pList[4].getName());



        tables = new JComponent[]{
                new JScrollPane(playerTable = new JTable(new MyTableModel(pList))),
                panel
        };


        playerTable.setAutoCreateRowSorter(true);
        createSorter();

        add(newPlayer);
        add(editPlayer);
        add(editTeams);
        add(commitMatch);

        this.setBackground(Color.BLACK);
        this.setVisible(true);
    }

    public void start() {

    }
    public void destroy() {

    }
    public void stop() {

    }
    public void paint(Graphics g){

    }
    public void run() {
        this.setVisible(true);

    }
    /*****************************Initialize Players*************************************
     *   This function calls for a read in of all players and data listed
     *   inside of an excel spreadsheet. The players are added to an Array
     *   List for easy access and dynamic storage capabilities.  * 
    /************************************************************************************/
    public void initPlayers(int i){

        for(int x = 0; x < (i-1); x++){

            //System.out.println("Made it Here");
            pList.add(book.setPlayer((x+1)));
            /*DEBUG
            JOptionPane.showMessageDialog(null, pList[x].getName());
            */
        }
        playerCounter = (book.getRows()-1);

    }
    /***************************************************************************************
     *  This function defines an Action Listener for defining button activity
     *  The buttons all refer to this listener to create their functionality 
     ***************************************************************************************/
    public class ListenForAction implements ActionListener{
        String S;
        int active = 0;
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == newPlayer){
                S = JOptionPane.showInputDialog(null, inputs, "Add New Player", JOptionPane.PLAIN_MESSAGE);
                if (S != null){
                    addPlayer(S);
                }
                /*DEBUG
                JOptionPane.showMessageDialog(null, pList[playerCounter - 1].getName());
                pList[0].setElo(2300);
                pList[0].calculateElo(6, "LOSE");
                JOptionPane.showMessageDialog(null, pList[0].getElo());
                */
            } else if (e.getSource() == editPlayer){            
                JOptionPane.showOptionDialog(null, tables, "Edit Player Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
            } else if (e.getSource() == editPopUp){
                if (active == 0) {
                    editPopUp.setText("Stop Editing");
                    testRow = playerTable.getSelectedRow();
                    active = 1;
                } else {
                    editPopUp.setText("Edit Player");
                    testRow = 1000000;
                    active = 0;
                }

            }

        }
    }
    /************************************************************************************
     *  Custom Table Model to allow for a list of editable size and data
     *  The players are stored read into this via the Array List and the
     *  Data is displayed in a list that can be sorted by column.
     *************************************************************************************/
    private class MyTableModel extends AbstractTableModel {

        ArrayList<Player> list = null;

        MyTableModel(ArrayList<Player> list) {
             this.list = list;
        }

        public int getColumnCount() {
             return titles.length;
        }

        public int getRowCount() {
             return list.size();
        }

        public String getColumnName(int col) {
             return titles[col];
        }

        public void setValueAt(Object value, int row, int col){

            switch(col) {
            case 0:
                pList.get(row).setName((String)value);
                break;
            case 1:
                pList.get(row).setElo((Integer)value);
                break;
            case 2:
                pList.get(row).setAttendance((Integer)value);
                break;
            case 3:
                pList.get(row).setMVP((Integer)value);
                break;
            case 4:
                pList.get(row).setWins((Integer)value);
                break;
            case 5:
                pList.get(row).setLose((Integer)value);
                break;
            }

            System.out.println(pList.get(row).getName() + pList.get(row).getAttendance());
        }

       public boolean isCellEditable(int row, int column)
        {
           //System.out.println(Flag);
            if(testRow == playerTable.getSelectedRow()){
                return true;
            } else {
                return false;
            }
        }

        public Object getValueAt(int row, int col) {

             Player object = list.get(row);

             switch (col) {
             case 0:
                  return object.getName();
             case 1:
                  return object.getElo();
             case 2:
                  return object.getAttendance();
             case 3:
                  return object.getMVP();
             case 4:
                  return object.getWin();
             case 5:
                return object.getLose();
             default:
                  return "unknown";
             }
        }

        public Class getColumnClass(int c) {
             return getValueAt(0, c).getClass();
        }
    }

    private void addPlayer(String S){
        pList.add(new Player(S));
        tables = new JComponent[]{
                new JScrollPane(playerTable = new JTable(new MyTableModel(pList))),
                panel
        };


        playerCounter++;
        createSorter();
    }



    public void createSorter(){

        playerTable.setPreferredScrollableViewportSize(d);

        playerTable.setAutoResizeMode(getHeight());
        playerTable.setAutoCreateRowSorter(true);
        sorter = (TableRowSorter<?>)playerTable.getRowSorter();
        sorter.setRowFilter(new RowFilter<TableModel, Integer>(){

            @Override
            public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry){
                boolean included = true;
                Object cellValue = entry.getModel().getValueAt(entry.getIdentifier(), 0);
                if(cellValue == null || cellValue.toString().trim().isEmpty()){
                    included = false;
                }
                return included;
            }
        });
    }
}

.

public class Player {
    private String Name;
    private int Elo = 1600, Attendance = 0, MVP = 0, Win = 0, Lose = 0;


    public Player(String n, int e, int a, int m, int w, int l){
        Name = n;
        Elo = e;
        Attendance = a;
        MVP = m;
        Win = w;
        Lose = l;
    }
    public Player(String n){
        Name = n;
    }

    public Player() {

    }
    /***********************************************/
    public void setName(String n){
        Name = n;
    }

    public void setElo(int e){
        Elo = e;
    }

    public void setAttendance(int a){
        Attendance = a;
    }

    public void setMVP(int m){
        MVP = m;
    }

    public void setWins(int w){
        Win = w;
    }

    public void setLose(int l){
        Lose = l;
    }

    /************************************************/
    public void addAttendance(int e){
        Attendance += e;
    }
    public void addElo(int e){
        Elo += e;
    }
    public void addMVP(int e){
        MVP += e;
    }
    public void addWins(int e){
        Win += e;
    }
    public void addLose(int e){
        Lose += e;
    }
    /************************************************/
    public void calculateElo(int oE, String win){ 
        double tRatingP; //holds the players transformed Elo Rating for calculation
        double tRatingO; //holds the opponents transformed Elo Rating for calculation
        double expectedP; //Players expected score
        double pointP = 0;
        int eloValue = 32; //default elo value

        if (Elo > 2400){
            eloValue = 24;
        }

        switch(win){
            case "WIN":  pointP = 1;
                         break;
            case "DRAW": pointP = 0.5;
                         break;
            case "LOSE": pointP = 0;
                         break;
        }

        tRatingP = 10^(Elo/400);
        tRatingO = 10^(oE/400);

        expectedP = tRatingP/(tRatingP+tRatingO);

        this.setElo((int)Math.round(Elo + (eloValue*(pointP-expectedP))));


    }
    /************************************************/

    public String getName(){
        return Name;
    }

    public int getElo(){
        return Elo;
    }

    public int getAttendance(){
        return Attendance;  
    }

    public int getMVP(){
        return MVP;
    }

    public int getWin(){
        return Win;
    }

    public int getLose(){
        return Lose;

    }


}

.

import java.io.File;
import java.util.Date;
import jxl.*;
import jxl.write.*;

public class modifyExcel {
    private Player pHolder;
    private String[] tHolder =  new String[6];
    private Workbook eloBook;
    Sheet sheet;

    public void openExcel(){
        try {
            eloBook = Workbook.getWorkbook(new File("./eloSpreadsheet.xls"));
        } catch(Exception e){
            throw new Error(e);
        }
        sheet = eloBook.getSheet(0);

    }

    public void appendExcel(Player p){

    }

    public String[] setTitles(){
        for(int x = 0; x<=5; x++){
            tHolder[x] = sheet.getCell(x, 0).getContents();
        }
        return(tHolder);
    }

    public Player setPlayer(int i){
        //System.out.println("Made it Here " + i);
        pHolder = new Player();
        pHolder.setName(sheet.getCell(0, i).getContents());
        pHolder.setElo(Integer.parseInt(sheet.getCell(1, i).getContents()));
        pHolder.setAttendance(Integer.parseInt(sheet.getCell(2, i).getContents()));
        pHolder.setMVP(Integer.parseInt(sheet.getCell(3, i).getContents()));
        pHolder.setWins(Integer.parseInt(sheet.getCell(4, i).getContents()));
        pHolder.setLose(Integer.parseInt(sheet.getCell(5, i).getContents()));


        return(pHolder);
    }

    public int getRows(){
        return(sheet.getRows());
    }
}

For further information, I cannot even get the java console to appear which I assume means the applet is not loading at all.

Ranma344
  • 169
  • 1
  • 8
  • Did you allow or disallow applets to run from that domain? – Fallenreaper Mar 04 '16 at 19:41
  • Applets are allowed, but beyond that it won't run in an HTML file, and I have tested demo applets to make sure that there isn't a browser issue. – Ranma344 Mar 04 '16 at 19:44
  • Be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at the default level, raise the level and try it again. – Andrew Thompson Mar 05 '16 at 01:10
  • Not sure what you mean by raising the level, but I do have it configured to show. – Ranma344 Mar 05 '16 at 04:46
  • Ok, so I have the console showing, and it would appear the Applet is starting to load, but now it says my security settings have blocked the application. – Ranma344 Mar 05 '16 at 04:59
  • `eloBook = Workbook.getWorkbook(new File("./eloSpreadsheet.xls"));` Is that resource supplied by you or the end user? Because if an applet tries to deal with file paths, it will do so on the computer of the end user, not the server. Resources on the server must be accessed by URL. These days all applet code will need to be digitally signed in order to be launched. .. – Andrew Thompson Mar 06 '16 at 11:26
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) `This means you done goofed` You should not be blaming the user if the applet fails to load. It's not their fault. – Andrew Thompson Mar 06 '16 at 11:27

1 Answers1

0

You have incorrect ending of "embed" tag. It should be:

</embed>

instead:

</applet>
foxu94
  • 1
  • I caught that shortly after posting this, however I still get nothing when I attempt to run it. AppletViewer however is working just fine. – Ranma344 Mar 04 '16 at 21:33
  • I'm not an expert in applets, but you have empty methods like "paint" which overwrite super methods without calling them. – foxu94 Mar 04 '16 at 21:47
  • *:..you have empty methods like "paint" which overwrite super methods without calling them."* That is astute. The applet could not possibly work with an overridden and 'do nothing' paint method! In fact, if you made that the answer, it would be worth an up vote. (While you are correct about the tags needing to be balanced & consistent, I don't think that is what is causing the applet to fail.) – Andrew Thompson Mar 06 '16 at 11:30