Using Singleton Design Pattern In Swing - java

I have been trying to develop swing application which contains jxtreetable in which i do have Jbutton(which is used for navigation to another frame for editing that particular record)
previously i was just showing the window by creating new instance but later i realized that every time it opens new window (so if user clicked it 10 times then there will be 10 different window) so to avoid it i had implemented singleton design pattern which doesn't allow multiple window to opened
but at the same time if click on one edit button and then on another edit button it does refreshes the content within the frame on the first go but not on the second time
Edit Button is Jbutton which has been added to the jxtreetable column.
is that thing happening due to reference is existed already ?how to workaround this?

I do have JButton(which is used for navigation to another frame for
editing that particular record) previously i was just showing the
window by creating new instance but later i realized that every time
it opens new window (so if user clicked it 10 times then there will be
10 different window)
Note: remarks are mine.
There's no need to implement Singleton Pattern here, it is intended to other purposes. You just need to read about Modality: How to Use Modality in Dialogs.
If the JButton is pressed don't show a new JFrame to edit the selected record but use a modal JDialog instead. This way the user won't be able to click the JButton again until this JDialog is closed. Consequently a single instance of the edit window will be shown, what is actually your goal.

Related

Prevent JOption from blocking child JFrame

Is there a possibility to prevent a JOptionPane dialog from blocking the interaction with the rest of the program, especially child JFrames? In my GUI, I launch a JFrame and want a message dialog to pop up after the child is closed to remind the user of something, but they launch parallel and the reminder blocks the child frame from being used.
Like here:
popupObjMan newPopup1 = new popupObjMan(gatewayAbstract, gatewayAbstractID);
JOptionPane.showInternalMessageDialog(this, "REMINDER: DO REFRESH");
I've tried to set the popup always on top, but this doesn't quite do the job.
I have no problem with them launching parallel (I'd even prefer it), but I could not work my head around it yet.
I just started Java programming ,so sorry in case that'd be something obvious.
A JOptionPane normally need to be modal. It shows something important and waits till the user answers with whatever option you give him (e.g. ok-button, yes/no-buttons, ...)
But there are several ways to reach your target.
(a)
Normally a JOptionPane creates a modal window.
You need a modeless window which does not block other windows.
https://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
(b)
You can start different threads to work for your different windows. They can have windows which are shown whenever the responsible thread commands them to. This is a bit difficult and can lead to memory-troubles.
(c)
You can write your own message-panels (e.g. notificaton) which are shown when and how long you like.
Bigger projects use different of these ways to achieve their goals.
A JOptionPane is a component, just like a JPanel. As a component it can be added to any other panel.
The JOptionPane API provides static methods to create a show the JOptionPane on a modal JDialog by default. You can't change this behaviour.
However, you can manually add the JOptionPane to a non-modal JDialog that you create. This is extra work as you now need to handle the closing of the dialog and processing the clicked button.
If you really want to do this then read the JOptionPane API. There is a section on Direct Use which demonstrates the basic code needed to add the JOptionPane to a JDialog.

How to use JLabel and Swing Timer to briefly notify the user?

I'm working on a small app for a class. One of the functions I want is when a button is clicked, it adds an object to an ArrayList based on the fields that they filled. When they click they button, I want them to have visual confirmation that the action has been performed. However, just updating a JLabel would cause it to sit there forever after the button was clicked.
I would like the label to start off displaying "".
When the button is clicked, I want it to say "Character Added!"
..for a few seconds. and then switch back to "".
I looked up some questions about label updating using a timer, but most of them have been using it to display a counting down clock, and they usually use a timer start method. I'm confused about if you need a timer stop method to reset the label.
Any pointers?
What I ended up doing is, instead of using a JLabel that updates, I simply made it so when the button is pressed and a character is added to the ArrayList, I called
JOptionPane.showMessageDialogue("Character Added!");
That way instead of a small little JLabel confirming it, they get a very apparent notification that requires them to acknowledge it. Tons more simple than doing the whole Timer thing.

How to prevent from user to open more than one JPanel in MDI Window?

I am making a vehicle_management system in java swings. I have a MDI(muti-document interface) Window in which there are more then one panels can be open.But when one panel is open after another the previous one is hides under new one, so all the JPanel are stacks. what i want is that if a panel is open and user trying to open another panel the previous opened panel get closed.how to do that.
Your application should maintain a List<JInternalFame> of open frames. In your open Action, see if the target frame is already open. If so, invoke setSelected(true) to bring the frame to the front; if not, open the frame as usual. A related example is cited here.
Addendunm:
I don't know how to make a list of open frame and check target frame is open or not.
This example illustrates how to compose and iterate a List<JInternalFrame>. Use the list's indexOf() method to search the list for an existing instance. A return value of -1 means that the list does not contain the element
First off, unless yours is an extremely unusual application, you don't "open" a JPanel. Since we're talking about an MDI application I'm guessing that what you really mean is that you're opening a new JInternalFrame.
Second, you said that you want to "close" the other panels (again, I'm assuming here that you really meant internal frames) but as someone else already pointed out, if you close them then it's not really an MDI application. I'm guessing that what you really meant or want is to MINIMIZE (not close) the other internal frames. If that's the case, then you should create a collection (e.g., a List) of JInternalFrame instances in the class where you create and add new internal frames. When you're about to add a new frame then just loop through the items in that collection and call setIcon(true) for each internal frame instance. Once that loop has completed, add the new internal frame to the collection and then also add to the desktop pane. At that point it will be the only one that's not minimized / iconified and any frame after that will likewise be (at least initially) the only one that's not minimized / iconified.

How to create button go to next questions in Quiz Software

I'm creating a Quiz Software with 40+ questions. Problem is when I click the "Next" Button, I would like to make the current question and answer go and a new set come there in the same frame while also keeping the marks user got for previous question. I can make 40+ Jframes Forms but that would too complicated.
UPDATE: Simply when I click "Next" Button, the question in question box and answers of the radio buttons will change. But this has to be done over and over again, by same button. There's also this previous button that will remove the current question and go back to the last one.
To make things more complicated, each time the there will be different IF selections as the the radio button congaing correct answer also changes.
As I said I can design JFrame forms for each but I'm having a hard taking the marks of each question for one frame (called "Final Result") And there's also this timer that countdown from one hour. I don't think I can do either of these by creating 40+JFrame forms.
You can create just one Frame and on the 'Next' Button Handler, Change Label Text, Check Box Text (Assuming Multiple choice questions)

OfficeBean won't display after moving containing Swing Panel from one Container to another

I'm developing a Java 6 applet which allows users to view OO (v.3.2) documents (read only), and if they choose, click a button which launches a new JDialog window, with the document displayed in it which allows the user to and mark and redact it as they wish. Once they are done, they can close the JDialog, which saves the document to a server and redisplay the updated document (read-only again) in the original applet window
I guessed that I could do this with a single instance of an OfficeBean, embedded in a Swing Panel. However, I cannot seem to successfully move my Panel (containing the OfficeBean) from the applet to the JDialog when the "Redact" button is clicked. All I get is a blank area in the JDialog where the document should be. I get no errors.
I have currently managed to get round this by creating new instances of the OfficeBean every time I need to display the document (once when the applet is loaded, again when the user chooses to redact and it is opened in a JDialog, and finally when they click "Save" in the dialog and the redacted result is displayed in the applet again.) However this means three trips to and from the server where the documents originate. That seems mad to me.
I'm in no way a Swing expert and may well be making a silly mistake. However, I've done a lot of fiddling around, debugging and googling and can't seem to get this to work. Can anyone help me in this? Am I trying to do something which is fundamentally impossible? I hope not.
One rule in Swing is that a component can only be displayed / attached to one part of the gui "tree" at a time. When you "move" your component to the dialog, are you first removing it from the applet?

Categories