2

How do i remove the window box from any java program. Because i want to make it look border-less. I know any jar files running on jre automatically gets a window like this. So i want to know if there is workaround about this.

Thanks in advance

here is a photo what i want to do exactly

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Yeasin Ar Rahman
  • 666
  • 13
  • 21
  • i want to know something more as my application will have many features [like accessing database from web, user login,update information from the application to the server] i am not very uptight about look, any simple gui will be allright to me. But after reviewing javafx it seems to me that i can actually have both functionality and cool interface. How much it will affect complexity if want to use javfx instead of standard swing. – Yeasin Ar Rahman Jun 01 '13 at 09:41

1 Answers1

4

See Frame#setUndecorated

You could also use a JWindow which is undecorated by default.

Check this and this for example uses

Updated

If you remove the border, you become responsible for moving and resizing of the window...

This "basic" example demonstrates how to move a JWindow with the mouse. This makes a "drag zone" around window which is 10 pixels wide.

Resizing would be similar process, but you need to decide in which direction to resize (ie it might need you to move the window when it's resized ;))

import java.awt.Component;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestMoveWindow {

    public static void main(String[] args) {
        new TestMoveWindow();
    }

    public TestMoveWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JWindow window = new JWindow();
                window.setSize(200, 200);
                window.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }

                });

                MouseAdapter mouseHandler = new MouseAdapter() {

                    private Point offset;

                    protected boolean isWithinBorder(MouseEvent e) {
                        Point p = e.getPoint();
                        Component comp = e.getComponent();
                        return p.x < 10 || p.y < 10 || p.x > comp.getWidth() - 10 || p.y > comp.getHeight()  - 10;
                    }

                    @Override
                    public void mouseMoved(MouseEvent e) {
                        Component comp = e.getComponent();
                        if (isWithinBorder(e)) {
                            System.out.println("Move");
                            comp.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                        } else {
                            System.out.println("Default");
                            comp.setCursor(Cursor.getDefaultCursor());
                        }
                    }

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        if (offset != null) {
                            Point pos = e.getLocationOnScreen();

                            int x = pos.x - offset.x;
                            int y = pos.y - offset.y;

                            System.out.println(x + "x" + y);

                            SwingUtilities.getWindowAncestor(e.getComponent()).setLocation(x, y);
                        }
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        if (isWithinBorder(e)) {
                            Point pos = e.getComponent().getLocationOnScreen();
                            offset = new Point(e.getLocationOnScreen());
                            offset.x -= pos.x;
                            offset.y -= pos.y;
                        }
                    }

                };

                window.getContentPane().addMouseListener(mouseHandler);
                window.getContentPane().addMouseMotionListener(mouseHandler);

                window.setLocationRelativeTo(null);
                window.setVisible(true);
            }
        });
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • thanks for the suggestion jwindow is what i need i think i need to add some control.But when i impose a jinternalframe i see that jinternal frame is movable inside that jwindow but jwindow is rigid. I can not move it. what should i do. – Yeasin Ar Rahman Jun 01 '13 at 10:03
  • here is a sample code: package jwindow; import javax.swing.*; public class Jwindow { public static void main(String[] args) { javax.swing.JInternalFrame jInternalFrame1 = new javax.swing.JInternalFrame();JWindow w = new JWindow(); w.setSize(300, 300); w.setLocation(500, 100); w.add(jInternalFrame1); w.setVisible(true); jInternalFrame1.setVisible(true); } } – Yeasin Ar Rahman Jun 01 '13 at 10:06
  • Without the frame, you become responsible for things like resizing and moving – MadProgrammer Jun 01 '13 at 10:25
  • How about this http://stackoverflow.com/questions/9347076/how-to-remove-all-components-from-a-jframe-in-java?rq=1 – Yeasin Ar Rahman Jun 02 '13 at 06:24
  • What are you trying to do? What is it you're trying to accomplish? – MadProgrammer Jun 02 '13 at 06:27
  • Sorry for bothering you I want to accomplish this the easiest way http://i1147.photobucket.com/albums/o551/saviour2012/Untitled2-1.jpg http://i1147.photobucket.com/albums/o551/saviour2012/Untitled1-1.jpg also the application need to be moved easily. – Yeasin Ar Rahman Jun 02 '13 at 14:34