views:

307

answers:

4

I am trying to remove the drag bar across the top of the JFrame. I would like to keep the minimize maximize and close options that appear on this bar available. What I was thinking was to remove the bar (and icons). Then add the icons as embedded images, that implement the JFrame actionlistener. It would also be necessary for this to work with JInternalFrames. Any help would be greatly appreciated.

+1  A: 

To remove the titlebar, use

setUndecorated(true);

You could then re-add buttons for maximize/minimize. The source for maximize-button could look something like that (just to get an idea). Use JFrame.ICONIFIED for minimize button.

JButton btnMaximize = new JButton("+");
btnMaximize.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(MainFrame.this.getExtendedState() == JFrame.MAXIMIZED_BOTH) {
            MainFrame.this.setExtendedState(JFrame.NORMAL);
        }
        else {
            MainFrame.this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        }
    }
});
Peter Lang
Have you already tried to use it with an JInternalFrame?
Peter Lang
setUndecorated does not work for JInternalFrames (weird I thought they inherited JFrames)
Jeffrey
I will give that a try, but consider editing your question concerning JInternalFrames.
Peter Lang
JFrame is not a parent class of JInternalFrame... JFrame is a top-level class with a native peer. JInternalFrames are purely Swing components and must exist within a JDesktopPane.
Nate
A: 

For JInternalFrames...

javax.swing.plaf.InternalFrameUI ifu= this.getUI();
((javax.swing.plaf.basic.BasicInternalFrameUI)ifu).setNorthPane(null);
Jeffrey
Yeah, just found that in another forum :)
Peter Lang
why the downvote? I'm new to stackoverflow, is it poor form to post codes found in other forums?
Jeffrey
+4  A: 

You need to step back and understand how Swing works.

When you create a JFrame, Swing uses the OS widget for the frame. The title bar that you see is part of the OS component and you have no direct control over it with Swing. You can hide the titlebar (and border) of the frame by using setUndecorated(false) as suggested earlier. In this case you loose all the functionality associated with the title bar (dragging and access to all the buttons) and the Border (resizing). So if you need any of this functionality you need to recreate it all yourself.

On the other hand you can use:

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();

and Swing will build a title bar and Border for you and add back all the default functionality. So if you want to prevent dragging you would now need to inspect the JFrame for all its components to find the component that represent the title bar. When you find this component you can then remove the MouseMotionListeners from the component to prevent dragging. This way the title bar will still be there and the buttons will be active, but the dragging will be disabled. I would guess that is easier the adding in all the functionality to an undecorated frame.

As you have already realized a JInternalFrame is a component completely written in Swing so you have access to the child components, which is essentially the approach I'm suggesting for the JFrame as well.

camickr
Wow, that was one of the most comprehensive answers I have received so far! I really appreciate it, let me give it a shot and get back to you
Jeffrey
+1  A: 

Take a look at this article - i think it is pretty much what you need for the JFrame part.

http://www.stupidjavatricks.com/?p=4

It is based on JDialog, but it should be pretty much the same as JFrame. Maximize/minimize should be pretty much the same as the close button.

Fedearne