JFrame with hidden or no maximize button but should be able to re-size using mouse(clicking and dragging on jFrame border). setResizable(false) is only disabling the minimize button but not able to re-size using mouse.
I personally can't think of a reason to allow resize and not allow maximize but here is an example of how to prevent maximizing a JFrame while still allowing resize and minimize. Tested in windows, untested on all other platforms. Full screen flash is minimized using setMaximizedBounds().
final JFrame jFrameNoMax = new JFrame() {
{
setMaximizedBounds(new Rectangle(0, 0));
addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(final WindowEvent e) {
if (e.getNewState() == MAXIMIZED_BOTH) {
setExtendedState(NORMAL);
}
}
});
}
};
// Tester
jFrameNoMax.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrameNoMax.setSize(300, 300);
jFrameNoMax.setLocation(300, 300);
jFrameNoMax.setVisible(true);
You can take the following steps:
-Right click on JFrame
-Select properties
-Uncheck the resizable checkbox
-Close properties
-Run the program
See the attached illustration:
One option could be to use a JDialog instead of a JFrame. This allows the window to be manually resizeable but not maximizeable. The only problem with doing this is that you lose both the minimize and maximize buttons. This may or may not be a problem for your application.
Related
My Goal:
I am trying to make a program which will log me into my zoom classes at the correct time. I have figured out everything except trying to open zoom, enter the ID and passcode correctly, and logging in to the class.
The Problem:
There is no API which I can use to achieve my goal. There is a certain Zoom API, but it is paid, and I am a minor. The only option I think is left is to make java click and enter text anywhere outside a JFrame.
I tried using java.awt.Robot for this, but it can only click on a button inside the JFrame. I tried overlapping the two buttons and keeping the zoom window focused, but Java changes the focus to the JFrame.
If there is any other alternative in Java, or if there is any other external Library or API that may come in use, please suggest me the same.
Thank you in advance.
I tried using java.awt.Robot for this, but it can only click on a button inside the JFrame.
The Robot can be used to generate an OS level event.
If you want the Robot to click out side the frame then you need to move the mouse outside the bounds of the frame.
Simple example:
public static void main(String[] args) throws Exception
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocation(25, 25);
frame.setSize(100, 100);
frame.setVisible(true);
Robot robot = new Robot();
robot.delay(2000);
robot.mouseMove(15, 15 );
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
The Robot will click the system menu of the window found at the top/left corner of your desktop. So make sure you have a window maximized.
You could use WindowListener,#windowDeactivated or WindowFocusListener#windowLostFocus, but you have to be careful with owned JDialog's (as you have to be with FocusListener anyway)
private void jBtnAboutActionPerformed(java.awt.event.ActionEvent evt) {
jPanel_About.setSize(300, 300);
jPanel_About.setVisible(true);
}
How can I make User unable to select the main jFrame where jBtn_About is located while the About Panel pop-up?
Also, the code does show a new panel but it's created at top left corner of display. How can i allign it to be created in middle of the main jFrame?
Thank you very much!
You can use JDialog (as modal window) instead of jPanel_About.
For JDialog you can use setLocation to place it in desired position.
I'm trying to create a transparent JFrame, i.e. a window where the interior is completely transparent while the border, which is the top bar with the close button, minimize etc visible. I tried creating a new Jpanel and then using panel.setOpaque(false);, but it did not help.
I'm extremely new to swing and Java GUI and would love to get some help.
What you want to do is possible, but not in the way you're imagining it...
If you want your standard operating system's window decorations (i.e. window border, close, maximize, minimize buttons), it's impossible. Transparency cannot be applied to 'decorated' frames.
However, this can be done by using Java's default window decorations. Try out the code below:
public static void main(String[] args) {
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
g.setColor(Color.RED);
g.drawRect(32,32,32,32);
}
};
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame();
f.add(panel);
f.setSize(256,256);
// The following two statements cause the window-transparency:
f.setUndecorated(true);
f.setBackground(new Color(0,255,0,0));
f.setVisible(true);
}
I personally don't find the Java window decorations very nice looking... Apart from that, OS-specific features (such as Window's Snap feature by 'half-maximizing' a window by moving it to the side of the screen) don't work... To me this is a deal-breaker, but depending on your needs, it could be an acceptable solution. :)
To be honest, I didn't quite know how to express my question in the title. So if someone has a clearer idea of what i'm trying to ask, then please be so kind as to edit it, for the greater good of mankind.
I have the following code:
public class Main {
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
JFrame frame = new JFrame();
Window window = new Window(frame);
JButton btn = new JButton("Quit");
window.add(btn);
if(gd.isFullScreenSupported())
gd.setFullScreenWindow(window);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(null);
}
});
}
}
What I want to do is make a library system, illustrated in full screen mode. Inside the full screen mode, there should be simple frame windows with text fields in them and so on... No heavy 2D/3D graphics required here...
My problem is, the quit button is resized to fit the entire screen. I've tried to use setBounds, setSize, etc, on the frame, window and even button... But it doesn't seem to let me be able to center the button in the middle.
So my question: Is it possible to make JFrame application windows inside a JFrame application window which is set to full screen exclusive mode? Or is it only possible to use full screen exclusive mode together with 2D/3D methods like drawing?
Note: The only thing I intend to use full screen mode for is to set it to a black background, and have a Jframe window ontop of this black background in the center of the application. It is to give the illusion that the application is actually a system.
Thank you.
P.S. please note that the quit button was merely for my own testing purposes. It won't be part of the actual system, so my question does not revolve around how to resize this particular quit button.
Fullscreen Exclusive Mode is not meant to have Swing components inside. It is meant as a high performance way to draw graphics in Java (and can benefit as much as possible of underlaying hardware if done right).
What you want to do could be done by using an undecorated JDesktopPane and maximize it over the dimensions of the screen. Then proceed with using a JInternalFrame to have a JFrame like window inside that JDesktopPane.
I've included links to the respective API sections, they should get you started.
I think that what you are after is an MDI Application. If that is what you are after you could take a look here.
Your problem is that you do not use layout manager correctly.
Learn about layout managers - the modules that put and size/resize the visual components on screen. Then decide how do you want your button to look like and then decide which layout manager to use. It will do its job.
You know what i also had the exact same problem and the only thing i know from my experience is that you can use jinternal frame and set its property undecorated to true then add your custom title bar according to your requirement.
i'm developing a java application with a main Panel in which i add a JDesktopPane.
users clicking on JButton, will show a JInternalFrame. My problem is that if i use Mac OSx look and feel, the JDesktopPane shows with correct size the JInternalFrame, if i use other look and feel, it will display JInternalFrame in the dimension of screen. This is what i do:
In main JPanel:
jDesktopPane1 = new JDesktopPane();
setContentPane(jDesktopPane1);
when users click JButton(s):
public void addInDesktop(JInternalFrame frame){
frame.setVisible(true);
try {
jDesktopPane1.setSize(frame.getSize());
jDesktopPane1.setPreferredSize((frame.getSize()));
System.out.println(frame.getSize().toString());
System.out.println(jDesktopPane1.getSize().toString());
jDesktopPane1.add(frame);
frame.moveToFront();
frame.setSelected(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
The System.out.println says me that the dimension of JDesktopPane and JInternalFrame is the same, but why it show me the JInternalFrame in full screen size?
If you were to print out the sizes after jDesktopPane1.add(frame) you would then probably see the change in size. I would suggest that setting the desktop pane to the size of the internal frame is a bad practice - I would have thought it should remain the size of the client area of the parent JFrame the desktop pane sits in (the frame where you call setContentPane(jDesktopPane1) ), and just let the internal frame sit somewhere within it naturally.
What might be happening is that setting the pane and internal to the same size might make the look and feel assume you mean 'maximized', and so when it resolves the size of the desktop pane correctly it does something weird with the internal frame.
Try not calling setSize() / setPreferredSize() on the desktop pane and see if it then behaves consistently across L&Fs. Perhaps then you can figure out what best to do with the internal frame if its default size isn't what you want.
If you want to set the size as wide and high as your jframe where you are going to put in. You can use
variable_desktopPane.setBounds(this.getBounds());
By this I mean the Jframe you are calling its bounds.
I hope this helps, greetings.