我有一个java swing程序,它在jtable中显示xml页面的内容。我需要在每1分钟后显示要刷新的帧。我需要处理旧框架并在刷新后显示新框架
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLTableExample {
JFrame frame = new JFrame("Frame");
public static void main(String[] args) {
new XMLTableExample();
}
public XMLTableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JTable table = new JTable();
table.setOpaque(false);
Color lav = new Color(220, 220, 250);
try {
table.setModel(new XMLTableModel(new File("Htmll.xml")));
Thread.sleep(6000);
//frame.dispose();
} catch (ParserConfigurationException | SAXException
| IOException | XPathExpressionException ex) {
ex.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel label=new JLabel("label",JLabel.CENTER);
label.setText("Service Now Notification");
label.setFont(new Font("Courier New", Font.ITALIC, 30));
label.setForeground(Color.blue);
frame.add(label,BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setUndecorated(true);
JButton aButton = new JButton("submit");
aButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JScrollPane scroll = new JScrollPane(table);
scroll.getViewport().setBackground(lav);
frame.add(scroll);
frame.add(aButton, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
int frameWidth = 800;
int frameHeight = 135;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
frame.setLocation((int) 220,590);
frame.setVisible(true);
}
});
}
public static class XMLTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
protected static final String[] COLUMN_NAMES = { "Number", "Priority",
"State", "Assigned", "Description", "Task" };
private List<MSEntity> rows;
public XMLTableModel(File file) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
rows = new ArrayList<>(25);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(file);
setDocument(dom);
}
protected String getMSValue(Node msNode, String name)
throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("S[@N='" + name + "']");
Node sNode = (Node) expression
.evaluate(msNode, XPathConstants.NODE);
return sNode != null ? sNode.getTextContent() : null;
}
@Override
public int getRowCount() {
return rows.size();
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
public MSEntity getEntityAtRow(int row) {
return rows.get(row);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
MSEntity entity = getEntityAtRow(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = entity.getNumber();
break;
case 1:
value = entity.getPriority();
break;
case 2:
value = entity.getState();
break;
case 3:
value = entity.getAssigned();
break;
case 4:
value = entity.getDesc();
break;
case 5:
value = entity.getTask();
break;
}
return value;
}
public void setDocument(Document dom) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("/Objs/Obj/MS");
NodeList nl = (NodeList) expression.evaluate(dom,
XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String number = getMSValue(node, "Number");
String priority = getMSValue(node, "Priority");
String state = getMSValue(node, "State");
String assigned = getMSValue(node, "Assigned");
String desc = getMSValue(node, "Short_desc");
String task = getMSValue(node, "Task");
MSEntity entity = new MSEntity(number, priority, state,
assigned, desc, task);
rows.add(entity);
}
fireTableDataChanged();
}
public class MSEntity {
private final String number;
private final String priority;
private final String state;
private final String assigned;
private final String desc;
private final String task;
public MSEntity(String number, String priority, String state,
String assigned, String desc, String task) {
this.number = number;
this.priority = priority;
this.state = state;
this.assigned = assigned;
this.desc = desc;
this.task = task;
}
public String getAssigned() {
return assigned;
}
public String getDesc() {
return desc;
}
public String getNumber() {
return number;
}
public String getPriority() {
return priority;
}
public String getState() {
return state;
}
public String getTask() {
return task;
}
}
}
}
我的xml文件是这个
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>Selected.System.Management.Automation.PSCustomObject</T>
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="Number">INC0811168</S>
<S N="Priority">2 - High</S>
<S N="State">Assigned</S>
<S N="Assigned">New</S>
<S N="Short_desc">Review Ad-Hoc Service Request for Lavon A Gudmundson</S>
<S N="Task">Catalog Task</S>
</MS>
</Obj>
</Objs>
我试过
frame.dispose()但帧没有刷新
答案 0 :(得分:1)
我用过
static JFrame frame = new JFrame(“Frame”);
问题解决了
答案 1 :(得分:0)
您也可以尝试使用Jframe的重绘(long tm)方法。