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:



 import javax.swing.*;

 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File; public class nameDlg extends JFrame { public nameDlg() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("GridBagLayout"); this.setSize(600, 200); Container pane = this.getContentPane(); GridBagLayout layout = new GridBagLayout(); pane.setLayout(layout); GridBagConstraints c = new GridBagConstraints(); // lable
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(3, 3, 3, 3);
c.weightx = 0;
c.weighty = 0.25; pane.add(new JLabel("Please enter the prefix:"), c); // textfield
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 3;
c.weightx = 0; pane.add(new JTextField(), c); // lable
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1; pane.add(new JLabel("Please enter the starting number:"), c); // textfield
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 3; pane.add(new JTextField(), c); // lable
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1; pane.add(new JLabel("Please select a folder:"), c); // textfield shows file path
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.weightx = 0.75; final JTextField pathText = new JTextField();
pane.add(pathText, c); // button
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
c.weightx = 0.25; JButton folderBtn = new JButton("Folder"); folderBtn.addActionListener(new ActionListener() {
JFileChooser fileChooser; @Override
public void actionPerformed(ActionEvent event) { fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(nameDlg.this); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile(); pathText.setText(file.getPath());
} }
}); pane.add(folderBtn, c); // button enter
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 5;
c.insets.top = 10;
c.weightx = 0; pane.add(new JButton("Enter"), c); // button quit
c.fill = GridBagConstraints.NONE;
c.gridx = 1;
c.gridy = 5; pane.add(new JButton("Quit"), c); this.setVisible(true); }
}

Swing How to make dialogues的更多相关文章

  1. 如何使用swing创建一个BeatBox

    首先,我们需要回顾一些内容(2017-01-04 14:32:14): 1.Swing组件 Swing的组件(component,或者称之为元件),是较widget更为正确的术语,它们就是会放在GUI ...

  2. Java Swing interview

    http://www.careerride.com/Swing-AWT-Interview-Questions.aspx   Swing interview questions and answers ...

  3. Swing布局管理器介绍

    创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zhangjunhd.blog.51cto.com/113473/128174 当选 ...

  4. swing with transformjs

    Antecedent Facebook made a HTML5 game long time ago. The opening animation is a piece of software th ...

  5. java swing 双人五子棋源代码

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...

  6. 用swing也可以做出好看的界面

    用Swing做出的例子:JavaFX做出的界面:后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下,用户 ...

  7. Java界面设计 Swing(1)

    Java界面设计的用途 开发者可以通过Java SE开发丰富并且强大的具有图形界面的桌面应用程序.也可以设计一些提高效率的工具软件,帮助自己处理机械性工作. Java 的图形界面工具包,可以用于工具类 ...

  8. java基础 swing编程实战

    1. 实现金山词霸,点击左右收缩 效果图: exmaple code : /* * 词霸 * */ package demo7; import java.awt.*; import java.awt. ...

  9. Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解

    Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解   在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...

随机推荐

  1. 将特定TD颜色改变的两种方法

    方法一:取table值 方法二:使用tbody  

  2. JS 将字符串转换成日期类型

    将字符串形式的日期转换成日期对象 var strTime="2011-04-16"; //字符串日期格式           var date= new Date(Date.par ...

  3. iOS开发常用的第三方类库

    在iOS开发中不可避免的会用到一些第三方类库,它们提供了很多实用的功能,使我们的开发变得更有效率:同时,也可以从它们的源代码中学习到很多有用的东西. Reachability 检测网络连接 用来检查网 ...

  4. 个性二维码开源专题<前背景>

    //设置图片资源 private Image imgAgo; public override void SetParam() { base.SetParam(); // 读取前背景 string _i ...

  5. 导入HDFS的数据到Hive

    1. 通过Hive view CREATE EXTERNAL TABLE if not exists finance.json_serde_optd_table ( retCode string, r ...

  6. C语言 线性表 顺序表结构 实现

    一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...

  7. [安卓] 7、页面跳转和Intent简单用法

    这里有一个layout资源,2个activity.首先在MainActivity.java中实例化按钮和添加按钮监听绑定都是我们知道的,这里要注意的是第22行Intent intent = new I ...

  8. [51单片机] SPI nRF24L01无线 [可以放在2个单片机里实现通信]

    main.c #include<reg51.h> #include"2401.h" #define uint unsigned int #define uchar un ...

  9. 移动开发下Xamarin VS PhoneGap

    跨平台开发 移动应用开发对很多开发人员来说是一种令人恐惧的事情.许多企业希望能够通过开发移动应用程序,来提升企业业务水平,开发原生App时往往又缺少专业的Objective C 或 Java 移动开发 ...

  10. paip.获取文件名从路径uapi java python php总结...

    paip.获取文件名从路径uapi java python php总结... =====uapi basename_noext($fname); =============java  自己写.. St ...