Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.

View Poll Results: What should i work on next?

Voters
1. You may not vote on this poll
  • 3d Game Engine

    1 100.00%
  • Another application

    0 0%
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    40
    Thanks
    5
    Thanked 0 Times in 0 Posts

    JAVA- How to make buttons download certain files

    Hey, im working on a program, and it uses buttons. I want to know how to make it, when you click a button, its downloads a certain file. So, Essentials will download the file essentials.zip. How can i do this, here is my java script

    Code:
    package layout;
    
    /*
     * TabDemo.java
     */
    
    import java.awt.*;
    import javax.swing.*;
    
    public class TabDemo {
        final static String HOMEBUTTONPANEL = "Home";
        final static String MAINBUTTONPANEL = "Main Plugins";
        final static String ECONOMYBUTTONPANEL = "Economy Plugins";
        final static String FUNBUTTONPANEL = "Fun Plugins";
        final static String PERMBUTTONPANEL = "Permissions Plugins";
        final static String WORLDMODBUTTONPANEL = "World Modifiers";
        final static int extraWindowWidth = 600;
        final static int extraWindowHeight = 600;
    
        public void addComponentToPane(Container pane) {
            JTabbedPane tabbedPane = new JTabbedPane();
    
            //Create the "cards".
            JPanel card1 = new JPanel() {
                //Make the panel wider than it really needs, so
                //the window's wide enough for the tabs to stay
                //in one row.
                public Dimension getPreferredSize() {
                    Dimension size = super.getPreferredSize();
                    size.width += extraWindowWidth;
                    size.height += extraWindowHeight;
                    return size;
                }
            };
    
    
    
            JPanel card2 = new JPanel();
            
            card2.add(new JButton("Chest Shop"));
            card2.add(new JButton("Essentials"));
            card2.add(new JButton("Factions"));
            card2.add(new JButton("GroupManager"));
            card2.add(new JButton("Iconomy")); 
            card2.add(new JButton("Kingdoms"));
            card2.add(new JButton("PermissionsEx"));
            //Breakline here
            card2.add(new JButton("Towny"));
            card2.add(new JButton("World Border"));
            card2.add(new JButton("World Edit"));
            card2.add(new JButton("World Guard"));
            
    
            tabbedPane.addTab(HOMEBUTTONPANEL, card1);
            tabbedPane.addTab(MAINBUTTONPANEL, card2);
    
            pane.add(tabbedPane, BorderLayout.CENTER);
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Minecraft Server Plugin Downloader");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            TabDemo demo = new TabDemo();
            demo.addComponentToPane(frame.getContentPane());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You need to add an event to the buttons; typically the ActionListener. Then use the JFileChooser's saveFile method to prompt for a download.

  • Users who have thanked Fou-Lu for this post:

    Hydrian (10-26-2012)

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    40
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Cheers, can you give me an example of doing this please. That would be great if you can. If so ill add you into the credits list for extremely helpful

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, I won't make one that matches what you have here, just something far more bare and generic so its easier to follow. Given what you have above, I think you can adapt it to what you need.
    PHP Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;


    public class 
    ALTest
    {
        public static 
    void main(String[] argv)
        {
            
    SwingUtilities.invokeLater(new Runnable()
            {
                @
    Override
                
    public void run()
                {
                    
    JFrame jf = new JFrame();
                    final 
    JButton jb1jb2jb3;
                    
                    
    jb1 = new JButton("Button 1");
                    
    jb2 = new JButton("Button 2");
                    
    jb3 = new JButton("Button 3");
                    
                    
    ActionListener al = new ActionListener()
                    {
                        @
    Override
                        
    public void actionPerformed(ActionEvent e)
                        {
                            if (
    e.getSource() == jb1)
                            {
                                
    JOptionPane.showMessageDialog(null"Button one was pressed");
                            }
                            else if (
    e.getSource() == jb2)
                            {
                                
    JOptionPane.showMessageDialog(null, ((JButton)e.getSource()).getText() + " was pressed");
                            }
                            else
                            {
                                
    JOptionPane.showMessageDialog(null"Neither button 1 or button 2 pressed");
                            }
                        }
                        
                    };
                    
                    
    jb1.addActionListener(al);
                    
    jb2.addActionListener(al);
                    
    jb3.addActionListener(al);
                    
                    
    jf.getContentPane().add(jb1);
                    
    jf.getContentPane().add(jb2);
                    
    jf.getContentPane().add(jb3);
                    
    jf.setLayout(new GridLayout(31));
                    
    jf.pack();
                    
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
    jf.setVisible(true);
                    
                }
                
            });
        }


  • Users who have thanked Fou-Lu for this post:

    Hydrian (10-27-2012)

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    40
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
                    jb1.addActionListener(al);
                    jb2.addActionListener(al);
                    jb3.addActionListener(al);
    So i make al the url to the file?

    so somthing like this?

    Code:
                    jb1.addActionListener(http://www.example.com/example1.zip/);
                    jb2.addActionListener(http://www.example.com/example2.zip/);
                    jb3.addActionListener(http://www.example.com/example3.zip/);
    Last edited by Hydrian; 10-27-2012 at 04:27 AM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Hydrian View Post
    Code:
                    jb1.addActionListener(al);
                    jb2.addActionListener(al);
                    jb3.addActionListener(al);
    So i make al the url to the file?

    so somthing like this?

    Code:
                    jb1.addActionListener(http://www.example.com/example1.zip/);
                    jb2.addActionListener(http://www.example.com/example2.zip/);
                    jb3.addActionListener(http://www.example.com/example3.zip/);
    Nope, you need to use an action listener event, not a url. I used an instantiation of the ActionListener and assigned it to the variable al.
    To download, you would then make use of the JFileChooser as its simple.

    Code:
    class DownloadAction implements ActionListener
    {
    	private String sPath;
    
    	public DownloadAction(String sPath)
    	{
    		this.sPath = sPath;
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e)
    	{
    		try 
    		{
    			URL url = new URL(this.sPath);
    			JFileChooser jfc = new JFileChooser();
    			int iResult = jfc.showSaveDialog(null);
    			if (iResult == JFileChooser.APPROVE_OPTION)
    			{
    				ReadableByteChannel read = Channels.newChannel(url.openStream());
    				FileOutputStream write = new FileOutputStream(jfc.getSelectedFile());
    				System.out.println(jfc.getSelectedFile().getAbsolutePath());
    				write.getChannel().transferFrom(read, 0, 1 << 24);
    				write.close();
    				read.close();
    			}
    		}
    		catch (MalformedURLException ex)
    		{
    			ex.printStackTrace();
    		}
    		catch (IOException ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    }
    Then I simply invoke the action listener with new DownloadAction("http://site.com/file");.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •