Java version of Amazon ad link code creator

March 12, 2009JPComments OffRate This ArticlenewLinks to this article


    As an exercise, I created a Java version of my Amazon Ad Link Generator add-in. It's a stripped down version with a few less bells and whistles, and like the Excel add-in, no validation is performed (except on the 10 digit ISBN/ASIN to make sure it's 10 digits).

    It will check and make sure at least the Associates ID and ISBN are filled in. No title or product description are necessary. If you click "Submit" and nothing happens, that means either the Associates ID was left blank, or the ISBN was either left blank or is less than 10 alphanumeric digits.

    Otherwise it works exactly the same, generating the link by building a string out of what you type in the textboxes. Clicking "Reset" will clear all the text fields except the associates ID, so you can enter several products in a row without having to reenter that.

Download java source code

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

public class AmazonAdLinkGenerator extends JFrame
{
    // text fields
    private JTextField txtTitle, txtDescription, txtISBNNumber, txtAssociatesID;
    // labels
    private JLabel lblTitle, lblDescription, lblISBNNumber, lblAssociatesID,
           lblLink;
    // text area
    private JTextArea txtLink;
    private JScrollPane sbrText;

    // buttons
    private JButton btnReset, btnExit, btnSubmit;
    // events
    private ResetButtonHandler rbHandler;
    private ExitButtonHandler ebHandler;
    private SubmitButtonHandler sbHandler;

    private static final String Title = ("Amazon Ad Link Generator");
    private static final int WIDTH = 500;
    private static final int HEIGHT = 300;

    public AmazonAdLinkGenerator()
    {
        // create labels
        lblTitle = new JLabel("Title:", SwingConstants.RIGHT);
        lblDescription = new JLabel("Description:", SwingConstants.RIGHT);
        lblISBNNumber = new JLabel("ISBN/ASIN Number:", SwingConstants.RIGHT);
        lblAssociatesID = new JLabel("Associates ID:", SwingConstants.RIGHT);
        lblLink = new JLabel("HTML Link:", SwingConstants.RIGHT);

        // create text fields
        txtTitle = new JTextField(20);
        txtDescription = new JTextField(20);
        txtISBNNumber = new JTextField(20);
        txtAssociatesID = new JTextField(20);

             // scrolling text area
         // with help from http://www.abbeyworkshop.com/howto/java/ta_scroll/index.html
        txtLink = new JTextArea(5,20);
        txtLink.setLineWrap(true);
        sbrText = new JScrollPane(txtLink);
        sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        /* Buttons */
        // Reset button
        btnReset = new JButton("Reset");
        rbHandler = new ResetButtonHandler();
        btnReset.addActionListener(rbHandler);

        // exit button
        btnExit = new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        btnExit.addActionListener(ebHandler);

        // Submit button
        btnSubmit = new JButton("Submit");
        sbHandler = new SubmitButtonHandler();
        btnSubmit.addActionListener(sbHandler);

        // basic GUI info
        setTitle(Title);
        setResizable(false);
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // create content pane and add panels to it
        // with help from:
        // http://www.cis.upenn.edu/~matuszek/cit591-2004/Pages/layout-examples.html
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4,2));

        panel.add(lblTitle);
        panel.add(txtTitle);
        panel.add(lblDescription);
        panel.add(txtDescription);
        panel.add(lblISBNNumber);
        panel.add(txtISBNNumber);
        panel.add(lblAssociatesID);
        panel.add(txtAssociatesID);

        // second panel
        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(1,2));

        secondPanel.add(lblLink);
        secondPanel.add(sbrText);

        //third panel
        JPanel thirdPanel = new JPanel();
        thirdPanel.setLayout(new GridLayout(1,3));

        thirdPanel.add(btnSubmit);
        thirdPanel.add(btnReset);
        thirdPanel.add(btnExit);

        // add panels to pane
      pane.add(panel, BorderLayout.NORTH);
        pane.add(secondPanel, BorderLayout.CENTER);
        pane.add(thirdPanel, BorderLayout.SOUTH);

        pack();

        // show GUI
        setVisible(true);

    } // end class method


    // class to handle Reset button click
    private class ResetButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            txtTitle.setText("");
            txtDescription.setText("");
            txtISBNNumber.setText("");
            txtLink.setText("");
        }
    }

    // class to handle Exit button click
    private class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }

    // class to handle Submit button click
    private class SubmitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {

            // check for required fields
            String isbn = new String(txtISBNNumber.getText());
            String assocID = new String(txtAssociatesID.getText());

            if (isbn.length() == 10 && assocID.length() != 0) {
                String title = new String(txtTitle.getText());
                String desc = new String(txtDescription.getText());

            txtLink.setText("<a href="http://www.codeforexcelandoutlook.com/blog/2009/03/java-version-of-amazon-ad-link-code-creator/"\"http://www.amazon.com/gp/product/" +
                    isbn + "?ie=UTF8&tag=" + assocID +
                    "&linkCode=as2&camp=1789&creative=9325&creativeASIN=" +
                    isbn + "\"" + "><img src="http://www.codeforexcelandoutlook.com/blog/2009/03/java-version-of-amazon-ad-link-code-creator/"\"http://images.amazon.com/images/P/" +
                    isbn + ".01.MZZZZZZZ.jpg\"" + " alt=" + "\"" + title + "\"" +
                    " title=" + "\"" + title + "\"" + " style="
                    + "\"border:0; margin:0px\"" + " /></a>" + "\n\n" + desc);
                }
        }
    } //end Submit button event

    public static void main(String[] args)
    {
        AmazonAdLinkGenerator ad = new AmazonAdLinkGenerator();
    } // end main

} // end class

Screenshot:

java version

Download java source code

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

Sorry, comments for this article are closed at this time.

Site last updated September 2, 2010 @ 7:03 pm