jframe interview questions
Top jframe frequently asked interview questions
How can I get the JFrame in which a JPanel is living?
My current solution is to ask the panel for it's parent (and so on) until I find a Window:
Container parent = this; // this is a JPanel
do {
parent = parent.getParent();
} while (!(parent instanceof Window) && parent != null);
if (parent != null) {
// found a parent Window
}
Is there a more elegant way, a method in the Standard Library may be?
Source: (StackOverflow)
Is there a way to somehow 'do something' when I click the red close button on the title bar of a JFrame? What I want to do is to call a method called confirmExit()
when said button is clicked. So far, the only option I have is to have it do nothing, but I don't want that. How do I accomplish this?
Thanks in advance.
Source: (StackOverflow)
I don't get how can I employ this code:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
to close the program with the x button.
Source: (StackOverflow)
I have a button on a JFrame that when clicked I want a dialog box to popup with multiple text areas for user input. I have been looking all around to try to figure out how to do this but I keep on getting more confused. Can anyone help?
Source: (StackOverflow)
If I minimize a JFrame
which was Aero-snapped to the left of the screen by clicking on the minimize-button of the Windows WindowDecoration and unminimize it by Alt-Tabbing or clicking it in the Windows TaskBar, the frame gets restored correctly snapped to the left. Good!
But if I minimize the frame by
setExtendedState( getExtendedState() | Frame.ICONIFIED );
and look at the preview by hovering over the Windows TaskBar, it shows the frame a wrong position.
After unminimizing it by Alt-Tabbing or clicking it in the Windows TaskBar, the frame gets restored at this wrong position and size. The frame-bounds are the "unsnapped" values, which Windows normally remembers to restore if you drag the frame away from the ScreenBorder.
A screen recording of the Bug:

My conclusion is, that Java does not know about AeroSnap and delivers the wrong bounds to Windows. (For example Toolkit.getDefaultToolkit().isFrameStateSupported( Frame.MAXIMIZED_VERT ) );
returns false.)
This is my fix for the bug:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
* Fix for the "Frame does not know the AeroSnap feature of Windows"-Bug.
*
* @author bobndrew 20160106
*/
public class SwingFrameStateWindowsAeroSnapBug extends JFrame
{
Point location = null;
Dimension size = null;
public SwingFrameStateWindowsAeroSnapBug( final String title )
{
super( title );
initUI();
}
private void initUI()
{
setDefaultCloseOperation( EXIT_ON_CLOSE );
setLayout( new FlowLayout() );
final JButton minimize = new JButton( "Minimize" );
final JButton maximize = new JButton( "Maximize" );
final JButton normal = new JButton( "Normal" );
add( normal );
add( minimize );
add( maximize );
pack();
setSize( 200, 200 );
final ActionListener listener = actionEvent ->
{
if ( actionEvent.getSource() == normal )
{
setExtendedState( Frame.NORMAL );
}
else if ( actionEvent.getSource() == minimize )
{
//Size and Location have to be saved here, before the minimizing of an AeroSnapped WindowsWindow leads to wrong values:
location = getLocation();
size = getSize();
System.out.println( "saving location (before iconify) " + size + " and " + location );
setExtendedState( getExtendedState() | Frame.ICONIFIED );//used "getExtendedState() |" to preserve the MAXIMIZED_BOTH state
//does not fix the bug; needs a Window-Drag after DeMinimzing before the size is applied:
// setSize( size );
// setLocation( location );
}
else if ( actionEvent.getSource() == maximize )
{
setExtendedState( getExtendedState() | Frame.MAXIMIZED_BOTH );
}
};
minimize.addActionListener( listener );
maximize.addActionListener( listener );
normal.addActionListener( listener );
addWindowStateListener( windowEvent ->
{
System.out.println( "oldState=" + windowEvent.getOldState() + " newState=" + windowEvent.getNewState() );
if ( size != null && location != null )
{
if ( windowEvent.getOldState() == Frame.ICONIFIED )
{
System.out.println( "Fixing (possibly) wrong size and location on de-iconifying to " + size + " and " + location + "\n" );
setSize( size );
setLocation( location );
//Size and Location should only be applied once. Set NULL to avoid a wrong DeMinimizing of a following Windows-Decoration-Button-Minimize!
size = null;
location = null;
}
else if ( windowEvent.getOldState() == (Frame.ICONIFIED | Frame.MAXIMIZED_BOTH) )
{
System.out.println( "Set size and location to NULL (old values: " + size + " and " + location + ")" );
//Size and Location does not have to be applied, Java can handle the MAXIMIZED_BOTH state. Set NULL to avoid a wrong DeMinimizing of a following Windows-Decoration-Button-Minimize!
size = null;
location = null;
}
}
} );
}
public static void main( final String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
new SwingFrameStateWindowsAeroSnapBug( "AeroSnap and the Frame State" ).setVisible( true );
}
} );
}
}
This seems to work for all situations under Windows7, but it feels like too much messing around with the window-management. And I avoided to test this under Linux or MacOS for some reason ;-)
Is there a better way to let AeroSnap and Java Frames work together?
Edit:
I've filed a bug at Oracle: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8147840
Source: (StackOverflow)
I have a dual monitor config and I want to run my GUI in a specific monitor if it is found. I tried to create my JFrame
window passing a GraphicConfiguration
object of my screen device, but it doesn't work, frame still display on the main screen.
How can I set the screen where my frame must be displayed?
Source: (StackOverflow)
I want to display two (or more) JFrames at the same time.
When I close one of them (use the default close button), the other frames should still be visible.
How can I do that?
Source: (StackOverflow)
What is the difference between a JFrame
and a JDialog
?
Why can't we use setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
for a JDialog?
Source: (StackOverflow)
I am learning Java gui. The way I learned to create a windows is to inherit or Extend JFrame
class and I good to as JFrame
contains all the properties of a Window. Now If I want to add something to this window, I need to use add()
method. But Today I came across JPanel
which also creates a windows and we can add stuff by jpanelObjec.add()
.
What is the difference between the two methods? Are they somehow related?
Source: (StackOverflow)
I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide, whether to use a separate JFrame to add Images to the Database from the GUI. I'm just wondering whether it is good practice to use multiple JFrames?
Source: (StackOverflow)
What's the correct way to get a JFrame to close, the same as if the user had hit the [x] button, or pressed Alt+F4 (on windows)?
I have my default close operation set the way I want, via
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and it does exactly what I want, with the aforementioned controls. This question isn't about that.
What I really want to do is cause the gui to behave in the same way as a press of [x] would cause it to behave.
I.e., supposing I were to extend WindowAdaptor, and then add an instance of my adaptor as a listener via addWindowListener()
; I would like to see the same sequence of calls through windowDeactivated()
windowClosing()
windowClosed()
as would occur with the [x]. Not so much tearing up the window as telling it to tear itself up, so to speak.
Source: (StackOverflow)
While working with Java, I find it hard to position my main window in the center of the screen when I start the application.
Is there any way I can do that?
It doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome.
Source: (StackOverflow)
im making a physics simulator for fun and i was looking up graphics tutorials when i tried to figure out the difference between all these J's. could somebody elaborate on them or perhaps provide a link to a helpful source?
Source: (StackOverflow)