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();
}
});
}
}
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.
public class ALTest { public static void main(String[] argv) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame jf = new JFrame(); final JButton jb1, jb2, jb3;
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"); } }
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");.