Window management: Difference between revisions

m (→‎{{header|C}}: Remove vanity tags)
(→‎{{header|Java}}: Avoid NPEs)
Line 413:
 
=={{header|Java}}==
{{incorrect|Java|Several flaws causing NPE's.}}
Java cannot (easily) manipulate windows created by other programs. This code manipulates windows that it has created, but any window created in the same JVM can be controlled similarly. This example uses Swing - for AWT or SWT go figure.
<lang Java>import java.awt.BorderLayout;
Line 433 ⟶ 432:
// Create UI on correct thread
public static void main( final String[] args ) {
EventQueue.invokeLater( () -> new WindowController() );
new Runnable() {
public void run() {
new WindowController();
}
}
);
}
 
private JComboBox<ControlledWindow> list;
 
// Button class to call the right method
Line 475 ⟶ 468:
getRootPane().setBorder( new EmptyBorder( 3, 3, 3, 3 ) );
add( new JLabel( "Add windows and control them." ), BorderLayout.NORTH );
main.add( list = new JComboBox<>() );
add( main, BorderLayout.CENTER );
controls.setLayout( new GridLayout( 0, 1, 3, 3 ) );
Line 520 ⟶ 513:
 
public void doHide() {
( (final JFrame )window list.getSelectedItem()= ).setVisiblegetWindow( false );
if ( null == window ) {
}return;
}
window.setVisible( false );
}
 
public void doShow() {
( (final JFrame )window list.getSelectedItem()= ).setVisiblegetWindow( true );
if ( null == window ) {
return;
);}
window.setBoundssetVisible( true );
}
 
public void doClose() {
( (final JFrame )window list.getSelectedItem()= ).disposegetWindow();
if ( null == window ) {
) return;
(}
window.dispose();
}
 
public void doMinimise() {
( (final JFrame )window list.getSelectedItem()= ).setStategetWindow( Frame.ICONIFIED );
if ( null == window ) {
return;
}
window.setState( Frame.ICONIFIED );
}
 
public void doMaximise() {
( (final JFrame )window list.getSelectedItem()= ).setExtendedStategetWindow( Frame.MAXIMIZED_BOTH );
if ( null == window ) {
return;
}
window.setExtendedState( Frame.MAXIMIZED_BOTH );
}
 
public void doMove() {
( (final JFrame )window list.getSelectedItem= getWindow() ).setLocation;
if ( null == window ) {
return;
Integer.parseInt( JOptionPane.showInputDialog( "Horizontal position?" ) ),
}
Integer.parseInt( JOptionPane.showInputDialog( "Vertical position?" ) )
final int hPos = getInt( "Horizontal position?" );
);
if ( -1 == hPos ) {
return;
}
final int vPos = getInt( "Vertical position?" );
if ( -1 == vPos ) {
return;
}
window.setLocation ( hPos, vPos );
}
 
public void doResize() {
final JFrame window = getWindow();
if ( null == window ) {
return;
}
final int width = getInt( "Width?" );
if ( -1 == width ) {
return;
}
final int height = getInt( "Height?" );
if ( -1 == height ) {
return;
}
window.setBounds ( window.getX(), window.getY(), width, height );
}
 
private JFrame getWindow() {
final JFrame window = ( JFrame ) list.getSelectedItem();
if ( null == window ) {
JOptionPane.showMessageDialog( this, "Add a window first" );
}
return window;
}
 
private int getInt(final String prompt) {
window.setBounds
final String s Integer.parseInt(= JOptionPane.showInputDialog( "Horizontal position?" )prompt ),;
(
if ( null == s window.getX(), {
window.getY(),return -1;
}
Integer.parseInt( JOptionPane.showInputDialog( "Width?" ) ),
try {
Integer.parseInt( JOptionPane.showInputDialog( "Height?" ) )
return Integer.parseInt( s );
} catch ( final NumberFormatException x ) {
JOptionPane.showMessageDialog( this, "Not a number" );
return -1;
}
}
}
Anonymous user