java发送ctrl s_Ctrl+S保存配置文件

java swing程序,Ctrl+S 保存配置文件如何实现呢?

首先增加全局快捷键:/***

* 增加全局快捷键.

* Ctrl+S,保存参数到配置文件

*/

private void setGlobalShortCuts() {

// Add global shortcuts

Toolkit toolkit = Toolkit.getDefaultToolkit();

// 注册应用程序全局键盘事件, 所有的键盘事件都会被此事件监听器处理.

toolkit.addAWTEventListener(new java.awt.event.AWTEventListener() {

public void eventDispatched(AWTEvent event) {

if (event.getClass() == KeyEvent.class) {

KeyEvent kE = ((KeyEvent) event);

// 处理按键事件 Ctrl+S

if (kE.getKeyCode() == KeyEvent.VK_S

&& kE.isControlDown()

&& kE.getID() == KeyEvent.KEY_PRESSED) {

//System.out.println("save");

saveConfig();

}

}

}

}, java.awt.AWTEvent.KEY_EVENT_MASK);

}

/***

* 保存到配置文件中

*/

private void saveConfig(){

File configFile=new File(configFilePath);

if(!configFile.exists()){

try {

SystemHWUtil.createEmptyFile(configFile);

} catch (IOException e) {

e.printStackTrace();

GUIUtil23.errorDialog(e);

}

}

String content=String.format(SAVE_CONFIG_TEMP, ipTextField.getText(),portTextField.getText(),topicTextField.getText());

CMDUtil.show(configFilePath);//因为隐藏文件是只读的

FileUtils.writeToFile(configFilePath, content);

//CMDUtil.executeCmd("attrib "+configFilePath+" +H");

CMDUtil.hide(configFilePath);

}

程序启动时读取配置文件private void readConfig() throws IOException{

//GenericReadPropsUtil propUtil=new GenericReadPropsUtil();

Properties prop= GenericReadPropsUtil.getProperties(!isInjar, CONF_PATH);

/***

* 从系统盘目录下读取配置文件

*/

Properties prop2= GenericReadPropsUtil.getProperties(false, configFilePath);

String serverIp22=null;

String port22=null;

String topic22=null;

if(!ValueWidget.isNullOrEmpty(prop2)){

serverIp22=prop2.getProperty("ip");

port22=prop2.getProperty("port");

topic22=prop2.getProperty("topic");

}

//if(ValueWidget.isNullOrEmpty(prop)){

//prop= GenericReadPropsUtil.getProperties(false, CONF_PATH);

//}

if(ValueWidget.isNullOrEmpty(prop)&&ValueWidget.isNullOrEmpty(prop2)){

return;

}

String key2 = "selected_index";

if(ValueWidget.isNullOrEmpty(prop)){

selectedIndex=0;

}else{

selectedIndex=Integer.parseInt(prop.getProperty(key2));

}

String propValue=null;

if(ValueWidget.isNullOrEmpty(serverIp22)){

key2 = KEY_PROP_ACTIVEMQ_IP;

propValue = prop.getProperty(key2);

if (propValue != null ) {

this.ipTextField.setText(propValue);

}}else{

this.ipTextField.setText(serverIp22);

}

if(ValueWidget.isNullOrEmpty(port22)){

key2 = KEY_PROP_ACTIVEMQ_MQTT_PORT;

propValue = prop.getProperty(key2);

if (propValue != null ) {

this.portTextField.setText(propValue);

}

}else{

this.portTextField.setText(port22);

}

if(ValueWidget.isNullOrEmpty(topic22)){

key2 = KEY_PROP_ACTIVEMQ_TOPIC;

propValue = prop.getProperty(key2);

if (propValue != null ) {

this.topicTextField.setText(propValue);

}

}else{

this.topicTextField.setText(topic22);

}

key2 = KEY_PROP_ACTIVEMQ_CLIENT_ID;

if(ValueWidget.isNullOrEmpty(prop)){

}else{

propValue = prop.getProperty(key2);

}

if (propValue != null ) {

this.clientIdTextField.setText(propValue);

}

}

public static final String configFilePath="C:\\.mqtt_client.properties";

注意:隐藏文件是只读的,所以写入文件之前先执行CMDUtil.show(configFilePath)

程序运行状态:

298a5b5b1f421f58ffbfa55ed5ce5bbc.png

项目使用maven 构建

源码见附件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值