解析java中的dom错误(Parsing dom error in java)
我试图解析XML,然后将其插入Excel文件。 如果我运行我的代码它甚至可以正常工作但我不能对它进行任何修改,因为我仍然有错误。 这是我的代码:
public class Parsing { private void parseXmlFile(){ //get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //using Factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation dom = db.parse("Employee.xml"); } } catch ) } }
这有什么问题? 有人能帮我吗? 我一直在搜索谷歌,它正在吃我的神经。
I am trying to parse an XML and then insert it an Excel File. If I run my code it works even with errors but I cannot make any modification to it because I still got errors. Here is my code:
public class Parsing { private void parseXmlFile(){ //get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //using Factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation dom = db.parse("Employee.xml"); } } catch ) } }
What is wrong with this? Can someone help me? I've been searching all over google and it's eating my nerves.
原文:https://stackoverflow.com/questions/31471711
最满意答案
在您的
SecondUI
您正在创建OneUI
新实例final OneUI obj = new OneUI(); obj.setVisible(true);// you don't call even this
但是你不称之为真正可见的。所以有一个隐藏的jframe和combobox组合框被更新,但不是第一帧的组合框,并且你看不到更新的组合框,因为框架不可见
解决这个传递给secondui的oneui引用然后调用那个引用的方法
例如
OneUI.java
public class OneUI extends JFrame { private JPanel contentPane; private JComboBox comboBox; private DefaultComboBoxModel modeltest; private Integer count = 0; private JButton btnOpenSecondUi; private SecondUI secondui; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { OneUI frame = new OneUI(); frame.setVisible(true); } }); } public OneUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnAddOne = new JButton("Add 1 element"); btnAddOne.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { updateModelCmb(); } }); btnAddOne.setBounds(187, 46, 129, 23); contentPane.add(btnAddOne); modeltest = new DefaultComboBoxModel(); comboBox = new JComboBox(); comboBox.setBounds(48, 47, 129, 20); comboBox.setModel(modeltest); contentPane.add(comboBox); btnOpenSecondUi = new JButton("Open second UI"); btnOpenSecondUi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { secondui= new SecondUI(OneUI.this); // pass reference of this oneui to secondui .so secondui can catch reference of this class[this frame] and update this combobox by calling updateModelCmb on this reference } }); btnOpenSecondUi.setBounds(155, 163, 161, 23); contentPane.add(btnOpenSecondUi); } public void updateModelCmb() { count++; modeltest.addElement(count); comboBox.setModel(modeltest); } }
SecondUI.java
public class SecondUI extends JDialog { private final JPanel contentPanel = new JPanel(); public SecondUI(OneUI oneui) { //setVisible(true); // don't call setvisible here call setvisible after you add all the component . setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 327, 142); getContentPane().setLayout(new BorderLayout()); contentPanel.setLayout(new FlowLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton btnAddOneElement = new JButton("Add 1 element"); btnAddOneElement.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { oneui.updateModelCmb(); } }); contentPanel.add(btnAddOneElement); } setVisible(true); // call setvisible at last } }
in your
SecondUI
you are creating new instance ofOneUI
final OneUI obj = new OneUI(); obj.setVisible(true);// you don't call even this
but you don't call it's visible to true.so there is a hidden jframe and combobox that combobox get updated but not first frame's combobox and you can't see updated combobox because the frame is not visible
to fix this pass oneui reference to secondui then call method of that reference
example
OneUI.java
public class OneUI extends JFrame { private JPanel contentPane; private JComboBox comboBox; private DefaultComboBoxModel modeltest; private Integer count = 0; private JButton btnOpenSecondUi; private SecondUI secondui; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { OneUI frame = new OneUI(); frame.setVisible(true); } }); } public OneUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton btnAddOne = new JButton("Add 1 element"); btnAddOne.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { updateModelCmb(); } }); btnAddOne.setBounds(187, 46, 129, 23); contentPane.add(btnAddOne); modeltest = new DefaultComboBoxModel(); comboBox = new JComboBox(); comboBox.setBounds(48, 47, 129, 20); comboBox.setModel(modeltest); contentPane.add(comboBox); btnOpenSecondUi = new JButton("Open second UI"); btnOpenSecondUi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { secondui= new SecondUI(OneUI.this); // pass reference of this oneui to secondui .so secondui can catch reference of this class[this frame] and update this combobox by calling updateModelCmb on this reference } }); btnOpenSecondUi.setBounds(155, 163, 161, 23); contentPane.add(btnOpenSecondUi); } public void updateModelCmb() { count++; modeltest.addElement(count); comboBox.setModel(modeltest); } }
SecondUI.java
public class SecondUI extends JDialog { private final JPanel contentPanel = new JPanel(); public SecondUI(OneUI oneui) { //setVisible(true); // don't call setvisible here call setvisible after you add all the component . setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 327, 142); getContentPane().setLayout(new BorderLayout()); contentPanel.setLayout(new FlowLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton btnAddOneElement = new JButton("Add 1 element"); btnAddOneElement.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { oneui.updateModelCmb(); } }); contentPanel.add(btnAddOneElement); } setVisible(true); // call setvisible at last } }
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
JComboBox + JPA(JComboBox + JPA)[2022-12-18]
组合框接受任何类型的对象。 为了显示它们的值,它使用对象的toString()方法。 所以,你可能有这样的代码: @Entity public class Genre { // fields and methods @Override public String toString() { return this.name; } } // in your GUI Listgenres = findAllGenresSortedByName( ... -
你可以用组合框制作一个构造函数...... private JComboBox comboBox; //constructor public ClassAddItem(JComboBox box) { this.comboBox = box; .... } 并在actionListener中使用此comboBox public void actionPerformed(ActionEvent e) { this.comboBox.addItem("this does work"); ...
-
首先一些关于MCVE的评论(因为你将在未来包括每个问题)。 我们希望代码可以放在单个源文件中,这样我们就可以轻松地复制/粘贴编译和测试。 我们不希望在我们的机器上放置3个文件,我们需要在测试后进行清理。 只应包括与问题直接相关的相关代码。 为什么你有TestTableModel类。 “列名”是否与问题相关? 要点总是在可能的情况下使用标准的JDK类来测试您的MCVE。 关于EventComboListener类。 再次,这可以通过使用和annoymouse内部类或lambda添加到组合框。 这将代码保存在一 ...
-
在您的SecondUI您正在创建OneUI新实例 final OneUI obj = new OneUI(); obj.setVisible(true);// you don't call even this 但是你不称之为真正可见的。所以有一个隐藏的jframe和combobox组合框被更新,但不是第一帧的组合框,并且你看不到更新的组合框,因为框架不可见 解决这个传递给secondui的oneui引用然后调用那个引用的方法 例如OneUI.java public class OneUI extends ...
-
隐形JComboBox(Invisible JComboBox)[2022-06-26]
您不应该调用setBounds() ,您需要使用布局来管理组件位置 You're not supposed to call setBounds(), you need to use a layout to manage the components positions -
JDialog有另一个按钮,我想在点击它时打开另一个JFrmae。 不要那样做。 一个典型的Swing应用程序有一个主JFrame和几个JDialog 。 请参阅此主题使用多个JFrame,好/坏练习? 结果:另一个Jframe打开但它不会到达顶部。它显示在对话框下。我想在该对话框的顶部打开第二个JFrame。 当然这是因为对话框是模态的。 可以使用secondFrame.setAlwaysOnTop(true); 但我没有控制权来关闭或移动它。 它不会解决任何问题,因为问题与对话框中的模态有关。 请参阅此 ...
-
JDialog及其所有者(JDialog and its owner)[2022-08-10]
唯一允许的所有者参数是顶级框架(我认为我不能从JPanel访问) 您可以使用以下方法访问面板的父框架: Window window = SwingUtilities.windowForComponent( yourPanelHere ); 然后只需使用该窗口作为对话框的所有者。 the only owner parameters that are allowed are top level Frames (which I don't think I can access from the JPanel Y ... -
代码是错误的,因为每次调用actionPerformed() ,您都在创建一个新组件: table = new JTable(new TestTableModel(data, header)); frame.add(new JScrollPane( table )); // <-- BTW: here you need to put the table otherwise you are adding an empty JScrollPane frame.validate(); (注意:还有一个bug, ...
-
JTable中的JComboBox(JComboBox in JTable)[2022-06-17]
JComboBox包含在CellEditor 。 您必须检索包装的组件,例如在使用DefaultCellEditor : DefaultCellEditor editor = (DefaultCellEditor)table.getColumnModel().getColumn(3).getCellEditor(); JComboBox combo = (JComboBox)editor.getComponent(); The JComboBox is wrapped in a CellEditor. Y ...