You are viewing our Forum Archives. To view or take place in current topics click here.
[Solved] I want only the file name and not whole directory!
Posted:

[Solved] I want only the file name and not whole directory!Posted:

-Tacodevil-
  • TTG Senior
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
Instead of showing the whole directory (I.E C:\User\[Name]\file.exe), how can I get it to where it just says "file.exe"? The reason why is that I want to create a JButton that has the file name to where when you click the button it launches the program but since the button is huge for the filename it just shows C:\User\[Name]\Docu... for all of them so you could imagine the confusion if you wanted to launch a specific game but all the titles were the same... Here's some of the code:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
        if(!jTextField6.getText().equals("")){
            jLabel15.setText("File Added!");
            JButton Button = new JButton(jFileChooser2.getSelectedFile().toString());
            Button.setSize(200, 75);
            jPanel11.add(Button);
        }
    }


What the code does is that if the text field (Directory) isn't empty and you click the button, it will say File Added! and make a new button with the SelectedFile code, which unfortunately displays the whole directory. Any tips will help, thanks!


Last edited by -Tacodevil- ; edited 1 time in total
#2. Posted:
aBoxOfMints
  • TTG Addict
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
well first off you don't need this:

java.awt.event.ActionEvent

I'll send ya something here in a little, let me code something up for ya
#3. Posted:
aBoxOfMints
  • TTG Addict
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
I also understand you're working with a GUI, however don't try and lean too far away from non-GUI actions you can still use parameter arguments that can read files in and then register them to that button click without having to brute force it.... do you have more code by any chance so I could take a look I want to see what your main is

also have you tried doing a full C:// directory check, it's not that complicated so in other words you can access files all over you're HDD
#4. Posted:
-Tacodevil-
  • TTG Senior
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
xMasterJPx wrote I also understand you're working with a GUI, however don't try and lean too far away from non-GUI actions you can still use parameter arguments that can read files in and then register them to that button click without having to brute force it.... do you have more code by any chance so I could take a look I want to see what your main is

also have you tried doing a full C:// directory check, it's not that complicated so in other words you can access files all over you're HDD
Yes, I am working with a GUI and I'm trying my best to learn hands on without relying on drag-and-drop programming. Also, I do have more code I just didn't want to publicize it. I'll PM you right now!
#5. Posted:
aBoxOfMints
  • TTG Addict
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
Ok I got it I'll see what this issue is and try my best to pin point the issue
#6. Posted:
-Tacodevil-
  • TTG Senior
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
xMasterJPx wrote Ok I got it I'll see what this issue is and try my best to pin point the issue
It's not really an issue, I'm sure I could code a workaround but I just want to get ONLY the filename and not the whole directory.
#7. Posted:
aBoxOfMints
  • TTG Addict
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
dude here's an idea after looking at it for couple minutes, try and use something called Jfilechooser

I will pm you a demo use of Jfilechooser, you can add a function call for Jfilechooser and have a Jbutton function run from that call just give me a couple minutes and I'll type ya up something that relates to using jbutton and jfilechooser together!
#8. Posted:
-Tacodevil-
  • TTG Senior
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
xMasterJPx wrote dude here's an idea after looking at it for couple minutes, try and use something called Jfilechooser

I will pm you a demo use of Jfilechooser, you can add a function call for Jfilechooser and have a Jbutton function run from that call just give me a couple minutes and I'll type ya up something that relates to using jbutton and jfilechooser together!
I do have it? If you run a quick compile and click Browse, it pops up. I looked at the demo yesterday. Look under button number 8 actions performed I believe.
#9. Posted:
-Tacodevil-
  • TTG Senior
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
Status: Offline
Joined: Dec 08, 201013Year Member
Posts: 1,375
Reputation Power: 56
If you have Netbeans just copy and paste that code into a new Java Swing class and compile. I can send you the whole project if you think that would help.
#10. Posted:
aBoxOfMints
  • TTG Addict
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
Status: Offline
Joined: May 17, 201013Year Member
Posts: 2,604
Reputation Power: 130
I may need the whole project cause running this alone with just this class isn't working
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.