I am trying to make an MP3 player, something similar to WinAmp. I'm currently having a minor issue with the GUI:
I have a main frame that will have the equalizer/volume/time elapsed etc. on it, and a sub-frame that will be the playlist. The sub-frame is a class that extends JFrame and is a field inside the main frame's class. Currently I have a button on the mainframe that changes the visibility of the sub-frame. My problem is: when the sub-frame appears, it creates a new task on the task-bar (when it's invisible the task disappears as well). I know it's not a major issue, but I find it somewhat annoying. Is there a way around this?
The subframe is a class that extends JFrame
take JDialog instead of JFrame. This will not create a new task item on task bar.
Related
I am trying to make a Quiz game and my idea is that i have a main frame class with buttons and a label like this
So when I click the Start button i want to stay in the same frame just delete all these buttons and label and replace them with the question and answers to it. How can I accomplish this? Read something about repaint() method and removeAll() but what is the best practice. I come up with this(dunno is it the right thing to do in the situation) but call removeAll() in the actionListener of the start button and create the new frame in this listener? Or can I make another class e.g called StartFrame and then in the actionListener of the start button call removeAll() and then MainFrame.this = new StartFrame() assuming that my class holding the initial state of the game like in the pic is called MainFrame
I have a main class in java which call a JFrame Two.
But before call this JFrame Two, my main check a condition and if it's true, it call a JFrame One.
So, my main don't extends JFrame.
I want that my JFrame One stop my main until it been closed, then my main could call my JFrame Two.
I tried to make my JFrame One as a JDialog modal, but my main still running (probably because it isn't a JFrame ?)
Here a simplified part of my code :
File file = new File(PTA);
if (!file.exists()) {
FrameOne fo = new FrameOne(); //FrameOne Extends JFrame
}
FrameTwo ft = new FrameTwo();
So like you can see, my main will always call FrameTwo.
But i want that until FrameOne was closed, the main class stop running, so it don't call FrameTwo until FrameOne was closed.
I don't really know how a JDialog works, i tried to convert my JFrameOne in a JDialog with "setModal(true)" but my main class still running too.
Please can you help me ? My project is blocked by this problem...
Thanks for your attention and your help.
Regards,
Maxime OZENNE.
i tried to convert my JFrameOne in a JDialog with "setModal(true)" but my main class still running too.
It doesn't work that way, you and you can't make a JFrame modal this way (the JFrame doesn't even have this method). Instead you're going to have to use a JDialog and not a JFrame, and create the dialog so that it's modal. Myself I avoid creating classes that extend JFrame or the like and instead gear my code towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
So on that note, I'd do something on these lines:
MyPanel myPanel = new MyPanel();
// assuming that the parent window JFrame is referenced by "myFrame"
JDialog myDialog = new JDialog(myJFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
myDialog.add(myPanel);
myDialog.pack();
myDialog.setVisible(true);
If this doesn't help, then please create and post your Minimal, Complete, and Verifiable example program, and let us help modify it.
I realize it's pointless to include a button that would do the exact same thing as the 'x' in the window, but I've already worked out the placement of my buttons on my GUI and found having the exit button made things much easier if nothing but a placeholder. And I like the practice.
Ok anyways moving on.
I have a parent JFrame (main class actually) that I'd like to keep running open the entire time the program is being run. This isn't my issue. My problem is when opening a child JFrame. I instantiate it in the main class (it's adding a panel component I created) and I just can't figure out how close said JFrame from within the Panel. Is there an easy way of doing so? I already have the WindowConstant set to Dispose on Close.
What I've done so far is created a method getExit() which returns a boolean value of true. I then have in the main class where the JFrame was instantiated an if/else if statement telling it to set the JFrame visible if exit is False, and dispose of it if true. Doesn't do anything. I'm guessing that's because either it's not bothering to check at all or I coded it poorly.
Any advice?
EDIT: Clarifying what my code is so far without posting it (600 lines of crap to get through). I have my main class Driver(). It's the fairly straight forward main JFrame 'form'.
Said class has several buttons to open up a new JFrame that performs a simple function. We'll name one of those classes (only have 3 total for the Secondary JFrames) Panel(int type) It extends JPanel.
I have a constructor set up depending that takes the int type and hides certain components in my Panel (tried to maximize the panel by combining similar functions). I have a button on the panel that is an exit button. But because the class itself is not a JFrame and does not instantiate itself, I can't dispose of it there. I have to find a way to do so in the main class.
That is my issue.
See Closing an Application. You can use the ExitAction for your JButton and it will be just like clicking the close button of the frame.
I have Main class, StartFrame extends JFrame, and UserPanel extends JPanel which I add to StartFrame. I have button in the UserPanel, how can I close StartFrame when I press the button(I am familiar with event handling it's not a problem the issue is how to sent info to the StartFrame) . Or it is better to just change the panel of the frame(size if need) and reuse it?
If you want to close a window that is enclosing a component, you need a reference to that Window, and SwingUtilities has a method that can help you get this: getWindowAncestor(Component c). Then you could call dispose() on the window returned.
i.e.,
Window win = SwingUtilities.getWindowAncestor(myJPanel);
win.dispose();
Note that this is fine if you're using this to end your GUI, but if your goal is to swap views, then a better suggestion is not to swap Windows, but rather to leave the main JFrame visible but to instead swap components it shows with a CardLayout.
The button is already on the frame (on the upper right, here). Just call JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) when constructing it and it will work as the user expects.
Other tip
Don't extend frame or panel, but instead just create and use them.
Just need some quick guidance -
I have a main frame, and I want some smaller frames inside of it that can be dragged outside of the main frame (potentially onto multiple monitors). When dragged back in, they should not be hidden behind the main frame when the main frame is clicked on.
I'm unclear about what I should be using... JFrames? Frames? Windows???
I used two JFrames, but the lesser JFrame gets pushed behind the main JFrame when I click on it. Adding the lesser JFrame to the main JFrame's content pane gave horrible, nightmarish bugs. =)
Use JDialogs as the child windows and make sure you specify the JFrame as the owner of the dialog.