Add JInternalFrame to JDesktopPane using a button inside other JInternalFrame - java

This snippet code I got from https://stackoverflow.com/a/6868039/2240900
how to add the internal2 to desktoppane1 using a button placed somewhere in internal1.
In the ActionListener added to your button you can use code like the following to get a reference to the desktop pane:
Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource());
if (container != null)
{
JDesktopPane desktop = (JDesktopPane)container;
JInternalFrame frame = new JInternalFrame(...);
desktop.add( frame );
}
My question is how to add another JInternalFrame if the button reside in another JInternalFrame? ex: add internalX to desktoppane1 using a button placed somewhere in internal2/internal3/internalX, where each internal was created using a button inside internalX not using a menubar.
Any help will be appreciated. Thanks.

I accidentally find out that we can use a method of JInternalFrame that is getDesktopPane().
As mention in javadoc:
getDesktopPane
public JDesktopPane getDesktopPane()
Convenience method that searches the ancestor hierarchy for a JDesktop instance. If JInternalFrame finds none, the desktopIcon tree is searched.
Returns:
the JDesktopPane this internal frame belongs to, or null if none is found
So we can just use a command like:
JDesktopPane desktopPane = internalFrame.getDesktopPane();
desktopPane.add(internalX);
or if the class extends JInternalFrame simply use
JDesktopPane desktopPane = this.getDesktopPane();
desktoppane.add(internalX);
to get the JDesktopPane to add another JInternalFrame in a nested JInternalFrame.

Externalize the listener into it's own class, with proper parameters if needed. Then, you can instantiate this listener every time you create a new frame and apply it to its button.

Related

Should be really simple, how do I make a jbutton show a jframe i have already created via the window builder in eclipse?

Not sure if I'm just using a visual basic approach, but I was under the impression i could use the window builder in eclipse to create my Jframes, then simply invoke them when the button/actionlistener is selected...
Like:
JButton btn_register = new JButton("Register");
btn_register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frm_register.setvisible(true);
}
frm_register is the class name for the register form...
YourFrame frame=new YourFrame();
frame.setVisible(true);
frm_register is the class name for the register form...
I hope not. Class names should start with upper case character. For example: RegistrationForm.
Then you need code like:
RegistrationForm register = new RegistrationForm();
register.setVisible( true );
This of course assumes that the constructor for the RegistrationForm adds components to the form and does a pack on the form.
i could use the window builder in eclipse to create my Jframes,
Also, you should not be using a JFrame. An application generally has a single JFrame. A child window would typically be a JDialog. See: The Use of Multiple JFrames: Good or Bad Practice?

How to use another classes' functions in Java

First time using the stackoverflow. So, please don't be too harsh :)
I have a little Java project. We are learning how to built GUI with swing. I am stuck with methodology.
I have a JFrame for GUI and I am creating 4 JPanels for:
Menu (Menu Class)
Buttons (Buttons Class)
ActionArea (ActionArea Class)
Statusbar (StatusBar Class)
I have been asked to have same options in the Menu and Buttons. i.e. New File or Open File options will be available in the Menu and Buttons sections.
I don't want to duplicate the code and copy into Menu Class and Buttons Class. I believe there is a way to use only one function from both classes.
Could somebody help me to achieve this?
Here is main code:
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class GUI extends JFrame {
private Menu menu;
private Buttons buttons;
private ActionArea actionArea;
private StatusBar statusBar;
public GUI() {
super("New GUI");
this.setSize(800, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout (new BorderLayout());
this.menu = new Menu();
this.buttons = new Buttons();
this.actionArea = new ActionArea();
this.statusBar = new StatusBar();
this.setJMenuBar(this.menu);
add(this.toolbar, BorderLayout.NORTH);
add(this.actionArea, BorderLayout.CENTER);
add(this.statusBar, BorderLayout.SOUTH);
}
}
lets say the function name is newFile();
I don't want to write this function one for menu class and one for buttons class.
Thanks in advance to all
EDIT:
this.menu = new Menu(); creates menuitems with required actions in Menu class
this.buttons = new Buttons(); creates jbuttons with required actions in Buttons class
actions are exactly same. Code is doubled. This is the problem.
Use a Swing Action. See How to use Actions for details on using them in buttons and menu items.
you can use Action class which is an extension of ActionListener....there are several properties you can set in objects of these class...you can check out the line:
Tutorial for Using Action classes
and for API: Action classes API
it may help you..
You can make your Button class to take the object of Menu class as parameter in its constructor. Now, with this object, you can get access to the Action classes of your Menu class, and then, you can use them for your button.
If you still can't understand, please provide the code of your Button and Menu classes.

convert form.showDialog from vb to Java

I need a similar code of form.showDialog from vb to Java to show a Frame up to its parent Frame. I've tried something like this :
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
Frame2 form = new Frame2();
form.setVisible(true);
}
and i got 2 problems,
the first : the new frame wont stay on the top alway, that mean i can select the old form
and the second problem : when i close the new form the parent form will close too !
Take a look at Swing's JDialog which has a modal property allowing the dialog to remain as the topmost window

How to create internal frames (child frames) in an applet

I am trying to create a child frame to exist inside my applet and it should be bound to a JPanel. I found this and that on the internet but nothing that worked. I think something went wrong during the process and the darn thing is hidden or something. Can someone please give me some help on this issue.
My source code follows...
public class EnableFrame {
public void init() {
EnableFrame theframe = new EnableFrame();
theframe.setSize(550, 300);
theframe.setVisible(true);
}
public EnableFrame() {
JPanel containall = new JPanel();
JInternalFrame iframe = new JInternalFrame("New Frame",true,true);
iframe.setBounds(10,10,150,150);
iframe.getContentPane().add(containall);
iframe.show(true);
}
}
Thanks in advance
-Roland
A JInternal is normally associated with a JDesktopPane.
I order for the internal frame to appear on the screen, you must have added the frame to an appropriate container, such as a JDesktopPane
You may find How to Use Internal Frames of some use.
my view only the comment
even is possible there could be caused with some side_effect for mouse and focus event betweens heavyweight (J)Applet and lightweight JInternalFrames that complicated this idea, and heavyweight (J)Applet can jumping toFront()
you'd don't do that and to use JDesktopPane from JFrame rather than for (J)Applet

Creating a custom modal dialog for a Swing applet / application

I'm writing a Swing application that needs to function either as an applet in a browser or as a standalone application, i.e. it may be contained in either a JFrame or a JApplet.
In this context, I'd like to display a custom modal dialog box to the user (i.e. a complex dialog with a custom layout and logic, not just one of the simple JOptionPane prompts). It is fine if the dialog is a lightweight component fully contained within the application window.
At the same time, there will be background processing happening in the application (network threads, animations etc.). This needs to continue while the dialog is displayed.
What would be the best approach to implement this?
Take a look at JDialog. If you set it modal it will run its own event processing to keep the GUI up to date, while capturing mouse and keyboard events for its own use.
I've looked at the code it uses and it's really not something you want to try to reinvent.
If you run it non modal, you 'll probably need to add a listener to be called when it finally closes. That is done with addWindowListener and a WindowAdapter that overrides windowClosing.
As for the owner parameter for the constructor, I use
Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class, comp);
where comp is some visible component.
It works because there is always a top level Window, whether running as an applet or application.
Here interesting method for showing frames as modal to specified owner is described:
Show the given frame as modal to the specified owner
However, start() method of class EventPump should be modified in such way:
protected void start() throws Exception
{
Class<?> cl = Class.forName("java.awt.Conditional");
Object conditional = Proxy.newProxyInstance(cl.getClassLoader(), new Class[] { cl }, this);
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
String name = Thread.currentThread().getName();
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
Constructor constructor = Class.forName("java.awt.EventDispatchThread").
getDeclaredConstructor(ThreadGroup.class, name.getClass(), eventQueue.getClass());
constructor.setAccessible(true);
Object eventDispatchThread = constructor.newInstance(threadGroup, name, eventQueue);
Method pumpMethod = eventDispatchThread.getClass().getDeclaredMethod("pumpEvents", cl);
pumpMethod.setAccessible(true);
pumpMethod.invoke(eventDispatchThread, conditional);
}

Categories