【发布时间】:2013-09-07 15:11:45
【问题描述】:
显示的图像是我使用Swing 制作的窗口。
当我单击Submit 按钮时如何创建一个警报窗口,并且在执行的操作完成之前应该显示警报(警报+代码应该在后台运行)?
这是我的代码 sn-p :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//alert window should start here when button performed
//and end based on the output i get
DefaultHttpClient httpclient = new DefaultHttpClient();
jLabel3.setText("");
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(getHost(), AuthScope.ANY_PORT),
new UsernamePasswordCredentials(getUser(), getPass()));
HttpResponse response = null;
HttpEntity entity = null;
try {
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==500) {
jLabel3.setText("Such Goods Movement does not exist / You do not have permssions for the entered.");
//end here or
} else if(response.getStatusLine().getStatusCode()==401) {
System.out.println(response);
jLabel3.setText("Auth Failed");
//end here or soon based on the event
}
entity = response.getEntity();
System.out.println(entity);
if (entity != null) {
InputStream instream = entity.getContent();
System.out.println(instream);
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream));
String inputLine;
String xmlText="";
while ((inputLine = reader.readLine()) != null)
xmlText = xmlText+inputLine+"\n";
System.out.println(xmlText);
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlText));
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("MaterialMgmtInternalMovementLine");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
getListGRNLine(element);
}
} catch (SAXException ex) {
Logger.getLogger(GoodsReceipt.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(GoodsReceipt.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch(UnknownHostException e){
jLabel3.setText("URL Not Found / Check Internet Connectivity");
} catch(NoRouteToHostException f){
jLabel3.setText("URL Not Found / Check Internet Connectivity");
} catch (IOException ex) {
Logger.getLogger(GoodsReceipt.class.getName()).log(Level.SEVERE, null, ex);
}
try {
EntityUtils.consume(entity);
} catch (IOException ex) {
Logger.getLogger(GoodsReceipt.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(GoodsReceipt.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
图像窗口:
【问题讨论】:
-
i.imgur.com/sqSs9E1.png 这是我的图片的链接(窗口)
-
请查看SwingWorker。在
actionPerformed()方法中的注释处,将JDialog置于带有警报消息的可见状态,将其余代码放入SwingWorker的doInBackground()方法和done()方法中,只需丢弃JDialog。您必须在Event Dispatcher Thread上拨打JLabel.setText()之类的电话。如果它是一些中间事物的结果,那么只需调用publish()并在process()方法中执行JLabel.setText()。就是这样:-) -
不过,如果您可以提供一个简单、简短、可编译的 (SSCCE)。这对我们提供一个工作示例会很有帮助:-)
-
好的,我会调查的。
-
一旦我将 jDialog 作为代码放入,背景应该停止的地方有很多。你能举个例子吗?
标签: java swing background alert execution