import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends JFrame {
public static void main(String[]args){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setSize(250,450);
window.getContentPane().setLayout(null);
JLabel lbl = new JLabel("");
lbl.setSize(145,25);
lbl.setLocation(1,115);
lbl.setBackground(Color.darkGray);
lbl.setBounds(-1,1,265,655);
lbl.setOpaque(true);
JLabel lbl1 = new JLabel("Погода");
lbl1.setSize(145,55);
lbl1.setLocation(1,115);
lbl1.setBackground(Color.pink);
lbl1.setBounds(95,1,45,25);
lbl1.setOpaque(true);
JLabel lbl2 = new JLabel("Погода");
lbl2.setSize(145,55);
lbl2.setLocation(1,115);
lbl2.setBackground(Color.pink);
lbl2.setBounds(95,115,45,25);
lbl2.setOpaque(true);
JButton btn = new JButton();
btn.setText("Проверить погоду");
btn.setSize(145,25);
btn.setLocation(43,235);
btn.setBackground(Color.white);
TextField text = new TextField();
text.setLocation(45,15);
text.setBounds(43,145,145,22);
text.setBackground(Color.white);
window.add(text);
window.add(lbl1);
window.add(btn);
window.add(lbl);
window.add(lbl2);
}
}
address.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) {
if (newPropertyValue) {
System.out.println("Textfield on focus");
TextInputDialog dialog = new TextInputDialog("walter");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter your name:");
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Your name: " + result.get());
}
// focus to different node on the scene
address.getParent().requestFocus();
// or mySubmitBtn.requestFocus();
} else {
System.out.println("Textfield out focus");
}
}
});
@Override
public void start( Stage stage )
{
TextArea address = new TextArea();
address.focusedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue )
{
if ( newPropertyValue )
{
System.out.println( "Textfield on focus" );
TextInputDialog dialog = new TextInputDialog( "walter" );
dialog.setTitle( "Text Input Dialog" );
dialog.setHeaderText( "Look, a Text Input Dialog" );
dialog.setContentText( "Please enter your name:" );
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if ( result.isPresent() )
{
System.out.println( "Your name: " + result.get() );
}
// focus to different node on the scene
address.getParent().requestFocus();
// or mySubmitBtn.requestFocus();
}
else
{
System.out.println( "Textfield out focus" );
}
}
} );
Scene scene = new Scene( new VBox( address ), 200, 200 );
stage.setScene( scene );
stage.show();
}