I've been making an installer for a few of my friends for adding mods to Minecraft. I removed all of my static variables and methods from my project(except the main method of course) and just added objects to the main method so I could call methods from other classes(without the methods in the other classes being static[Thanks to MadProgrammer for suggesting this to me :D]) And now when I try to call a boolean in my seperate class(FileHandling) it returns false no matter what, but if I print out the boolean in the main class it changes between true and false(like it should) Why is this and how can I fix it?
Below is my previous error, which is still occurring, but is not the 'main idea' of this post
I created a method(inside the class FileHandling) called fileChooserCheck. What it does is it gets the directory for installation defined by the user by a JFileChooser and sets that to modDir, or, if the user doesn't specify, it sets the installation directory to the default directory, which is the File 'defaultDir'. Whenever I run the method fileChooserCheck, it doesn't change the Path modDir, when I try to call it(modDir) it returns null.
How can I make it so that fileChooserCheck actually changes the Path modDir?
If you see anything in my code that you is sloppy and/or could/needs to be improved please tell me, I've only been programming with Swing for a few days, and Java for almost a week[I only know one other programming language[which is pretty basic)]
EDIT: Updated and slimmed down the code
Main Class
public class MainInstaller extends JFrame {
//File moddir;
Path modDir;
URI bioForums = URI.create("http://bio-mc.com/forum");
String mcDir = "C://Users//" + username + "//AppData//Roaming//.minecraft";
File defaultDir = new File("C://Users//" + username + "//AppData//Roaming//.minecraft");
Font consoleFont = new Font("", Font.PLAIN, 13 + 1/2);
Font checkFont = new Font("", Font.PLAIN, 14 + 1/2);
String[] minimapsArray = { "REI's Minimap", "Zan's Minimap" }; //An array of optifine versions, get manually later
Object minimap;
int yInterval = 22;
boolean enabled = false;
boolean optifine, invtweaks, armorhud, statuseffecthud, tabbychat, spc, chatbubbles, map = true;
boolean fcLaunched = false;
JPanel installPanel, modsPanel, consolePanel;
Box box, box1;
JLabel title;
JLabel modsel;
JLabel author;
JTabbedPane tabbedpane = new JTabbedPane();
JComboBox<?> minimaps; //The drop down menus to select the versions of optifine and invtweaks
JButton installButton;
JButton fileButton, forumButton, threadButton;
JTextArea console;
JTextField mcdir;
JFileChooser fc;
public MainInstaller() { //MainInstaller Constructor
//The First Tab(Installer)
installPanel = new JPanel();
installPanel.setLayout(null);
Insets insets = installPanel.getInsets();
installButton = new JButton("Install!");
installButton.setFont(new Font("", Font.PLAIN, 20));
Dimension size = installButton.getPreferredSize();
mcdir = new JTextField();
mcdir.setText("C:/Users/" + username + "/AppData/Roaming/.minecraft");
mcdir.setFont(new Font("", Font.PLAIN, 13));
size = mcdir.getPreferredSize();
mcdir.setBounds(139 + insets.left, 80 + insets.top, size.width + 5, size.height);
mcdir.setEditable(false);
fileButton = new JButton("...");
size = fileButton.getPreferredSize();
fileButton.setBounds(mcdir.getX() + 280, mcdir.getY() - 1 + insets.top, size.width, size.height + 3);
fileButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
fcLaunched = true;
//Create the FileChooser and define it a bit
if (fc == null) {
fc = new JFileChooser("C://Users//" + username + "//AppData//Roaming//.minecraft");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
//Show the file Chooser
int returnVal = fc.showOpenDialog(fileButton);
fc.setSelectedFile(defaultDir);
modDir = moddir.toPath(); //Changed what was previously 'moddir.toPath();' into what it is now b/c 'moddir' is a String type, not a File type
//Process the results
if(returnVal == JFileChooser.APPROVE_OPTION) {
mcdir.setText(fc.getSelectedFile().getPath());
}
}
});
installButton.setBounds(139 + insets.left, mcdir.getY() + 50 + insets.top, mcdir.getWidth() + fileButton.getWidth(), size.height + 40);
installPanel.add(author);
installPanel.add(forumButton);
installPanel.add(threadButton);
installPanel.add(installButton);
installPanel.add(mcdir);
installPanel.add(fileButton);
//The Second Tab(Mod Selection)
modsPanel = new JPanel();
modsPanel.setLayout(null);
insets = modsPanel.getInsets();
JLabel modsel = new JLabel("Select the mods you installed");
modsel.setFont(new Font("", Font.BOLD, 15));
size = modsel.getPreferredSize();
modsel.setBounds(5 + insets.left, 2 + insets.top, size.width + 15, size.height);
//Adds all the mod checkboxes
JCheckBox invBox = new JCheckBox("InvTweaks");
invBox.setFont(checkFont);
size = invBox.getPreferredSize();
invBox.setBounds(2 + 1/2 + insets.left, 2 + (yInterval*2) + 1/2 + insets.top, size.width, size.height); //Y = 44
invBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
invtweaks = !invtweaks;
}
});
modsPanel.add(modsel);
modsPanel.add(invBox);
modsPanel.add(mapBox);
tabbedpane.addTab("Installer", installPanel);
tabbedpane.addTab("Mod Selection", modsPanel);
//Add everything to the frame
add(box, BorderLayout.NORTH);
add(tabbedpane);
}
public static void main(String[] args) throws IOException { //Main Method
final MainInstaller frame = new MainInstaller();
Insets insets = frame.getInsets();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
final FileHandling file = new FileHandling();
file.createBaseFolder();
file.copyMods();
frame.installButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(frame.invtweaks == false) {
JOptionPane.showMessageDialog(frame, "No mods are enabled");
} else {
// (?) Install mods here?
try {
file.copyMods();
System.out.println("Installing Mods");
System.out.println("Source: " + file.invSource);
System.out.println("Target:" + file.invTarget);
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("Error!" + e);
}
}
}});
}
}
FileHandling Class
public class FileHandling {
MainInstaller mi = new MainInstaller();
Path modDir = mi.modDirPath;
Path source = Paths.get("C://Users//Gannon//Desktop//Java//workspace//ModPack Installer//test.txt");
Path target = Paths.get("C://Users");
Path invSource = Paths.get("mods//invtweaks.jar");
Path invTarget = Paths.get(mi.modDirPath + "//InventoryTweaks-MC1.6.2-1.55-b56.jar");
public void fileChooserCheck() {
if(mi.fcLaunched == false) {
mi.modDirPath = mi.defaultDir.toPath();
System.out.println("Unchanged: " + mi.modDirPath);
} else if(mi.fcLaunched == true) {
mi.modDirPath = mi.fc.getSelectedFile().toPath();
System.out.println("Changed: " + mi.modDirPath);
}
}
public void copyMods() throws IOException {
fileChooserCheck();
try {
if(mi.invtweaks == true) {
Files.copy(invSource, invTarget);
System.out.println("Trying to copy!");
} else if(mi.invtweaks == false ) {
System.out.println("InvTweaks isn't selected!");
}
} catch (IOException e) {
System.out.println("The Copying Failed!" + e);
}
}
}