Swing How to make dialogues

There are two types of dialog:

  • modal
  • non-modal: must use JDialog directly


Taken JFileChooser as Example:

1. Open a JFileChooser:

JFileChooserfileChooser = new JFileChooser();

2. Get value from it:

File file = fileChooser.getSelectedFile();

Code:



  1 import javax.swing.*;
  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.io.File;
  7 
  8 public class nameDlg extends JFrame {
  9 
 10 public nameDlg() {
 11 
 12 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 13 
 14 this.setTitle("GridBagLayout");
 15 
 16 this.setSize(600, 200);
 17 
 18 Container pane = this.getContentPane();
 19 
 20 GridBagLayout layout = new GridBagLayout();
 21 
 22 pane.setLayout(layout);
 23 
 24 GridBagConstraints c = new GridBagConstraints();
 25 
 26 // lable
 27 c.fill = GridBagConstraints.HORIZONTAL;
 28 c.gridx = 0;
 29 c.gridy = 0;
 30 c.insets = new Insets(3, 3, 3, 3);
 31 c.weightx = 0;
 32 c.weighty = 0.25;
 33 
 34 pane.add(new JLabel("Please enter the prefix:"), c);
 35 
 36 // textfield
 37 c.fill = GridBagConstraints.HORIZONTAL;
 38 c.gridx = 1;
 39 c.gridy = 0;
 40 c.gridwidth = 3;
 41 c.weightx = 0;
 42 
 43 pane.add(new JTextField(), c);
 44 
 45 // lable
 46 c.fill = GridBagConstraints.HORIZONTAL;
 47 c.gridx = 0;
 48 c.gridy = 1;
 49 c.gridwidth = 1;
 50 
 51 pane.add(new JLabel("Please enter the starting number:"), c);
 52 
 53 // textfield
 54 c.fill = GridBagConstraints.HORIZONTAL;
 55 c.gridx = 1;
 56 c.gridy = 1;
 57 c.gridwidth = 3;
 58 
 59 pane.add(new JTextField(), c);
 60 
 61 // lable
 62 c.fill = GridBagConstraints.HORIZONTAL;
 63 c.gridx = 0;
 64 c.gridy = 2;
 65 c.gridwidth = 1;
 66 
 67 pane.add(new JLabel("Please select a folder:"), c);
 68 
 69 // textfield shows file path
 70 c.fill = GridBagConstraints.HORIZONTAL;
 71 c.gridx = 1;
 72 c.gridy = 2;
 73 c.weightx = 0.75;
 74 
 75 final JTextField pathText = new JTextField();
 76 pane.add(pathText, c);
 77 
 78 // button
 79 c.fill = GridBagConstraints.HORIZONTAL;
 80 c.gridx = 2;
 81 c.gridy = 2;
 82 c.weightx = 0.25;
 83 
 84 JButton folderBtn = new JButton("Folder");
 85 
 86 folderBtn.addActionListener(new ActionListener() {
 87 JFileChooser fileChooser;
 88 
 89 @Override
 90 public void actionPerformed(ActionEvent event) {
 91 
 92 fileChooser = new JFileChooser();
 93 int returnVal = fileChooser.showOpenDialog(nameDlg.this);
 94 
 95 if (returnVal == JFileChooser.APPROVE_OPTION) {
 96 File file = fileChooser.getSelectedFile();
 97 
 98 pathText.setText(file.getPath());
 99 }
100 
101 }
102 });
103 
104 pane.add(folderBtn, c);
105 
106 // button enter
107 c.fill = GridBagConstraints.NONE;
108 c.gridx = 0;
109 c.gridy = 5;
110 c.insets.top = 10;
111 c.weightx = 0;
112 
113 pane.add(new JButton("Enter"), c);
114 
115 // button quit
116 c.fill = GridBagConstraints.NONE;
117 c.gridx = 1;
118 c.gridy = 5;
119 
120 pane.add(new JButton("Quit"), c);
121 
122 this.setVisible(true);
123 
124 }
125 }

 

 

你可能感兴趣的:(dialog)