Advertisement
leomovskii

GoToDialog.java

Jan 16th, 2022
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package com;
  2.  
  3. import java.awt.event.KeyEvent;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JComponent;
  7. import javax.swing.JDialog;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JTextArea;
  11. import javax.swing.JTextField;
  12. import javax.swing.KeyStroke;
  13. import javax.swing.text.BadLocationException;
  14.  
  15. public class GoToDialog {
  16.  
  17.     private JDialog gotoDialog;
  18.     private JTextArea textArea;
  19.  
  20.     public GoToDialog(JFrame main, JTextArea textArea) {
  21.         gotoDialog = new JDialog(main, "Переход на строку", true);
  22.         gotoDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  23.         gotoDialog.setSize(300, 110);
  24.         gotoDialog.setResizable(false);
  25.         gotoDialog.setLocationRelativeTo(null);
  26.         gotoDialog.setLayout(null);
  27.  
  28.         gotoDialog.getRootPane().registerKeyboardAction(e -> gotoDialog.dispose(),
  29.                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
  30.  
  31.         this.textArea = textArea;
  32.  
  33.         initUI();
  34.  
  35.         gotoDialog.setVisible(true);
  36.     }
  37.  
  38.     private void initUI() {
  39.         JLabel lineNumber = new JLabel("Номер строки:");
  40.         lineNumber.setBounds(10, 10, 100, 20);
  41.         gotoDialog.add(lineNumber);
  42.  
  43.         JTextField inputField = new JTextField();
  44.         try {
  45.             int n = textArea.getLineOfOffset(textArea.getCaretPosition()) + 1;
  46.             inputField.setText(String.valueOf(n));
  47.             inputField.selectAll();
  48.         } catch (BadLocationException e) {
  49.             inputField.setText("");
  50.         }
  51.         inputField.setBounds(102, 10, 174, 20);
  52.         gotoDialog.add(inputField);
  53.  
  54.         int lines = textArea.getLineCount();
  55.         if (lines < 100000) {
  56.             JLabel maxNumber = new JLabel("Макс: " + String.valueOf(lines));
  57.             maxNumber.setBounds(10, 44, 80, 20);
  58.             gotoDialog.add(maxNumber);
  59.         }
  60.  
  61.         JButton buttonOK = new JButton("ОК");
  62.         buttonOK.setBounds(94, 44, 80, 20);
  63.         buttonOK.addActionListener(e -> {
  64.             try {
  65.                 int n = Integer.parseInt(inputField.getText());
  66.                 textArea.setCaretPosition(textArea.getLineStartOffset(n - 1));
  67.             } catch (Exception ex) {
  68.             }
  69.             gotoDialog.dispose();
  70.         });
  71.         gotoDialog.add(buttonOK);
  72.  
  73.         JButton buttonCancel = new JButton("Отмена");
  74.         buttonCancel.setBounds(196, 44, 80, 20);
  75.         buttonCancel.addActionListener(e -> gotoDialog.dispose());
  76.         gotoDialog.add(buttonCancel);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement