我无法从JDialog更新Jcombobox(通过模型)(I can't update Jcombobox (via model) from a JDialog)
我有一个简单的用户界面,其中我的按钮调用我的方法updateModelCmb(),此方法只是增加计数器的值并更新模型。 该按钮似乎正确地为模型添加了适当的值。 但是当我在我的第二个UI类中做同样的事时,模型没有得到更新...我做错了什么? 这里是我的代码:
package testing; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.DefaultComboBoxModel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JComboBox; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class OneUI extends JFrame { private JPanel contentPane; private JComboBox comboBox ; private DefaultComboBoxModel modeltest; private Integer count=0; private JButton btnOpenSecondUi; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { OneUI frame = new OneUI(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ 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) { new SecondUI(); } }); btnOpenSecondUi.setBounds(155, 163, 161, 23); contentPane.add(btnOpenSecondUi); } public void updateModelCmb(){ count++; modeltest.addElement(count); comboBox.setModel(modeltest); } }
这是第二类,似乎没有工作。
package testing; import java.awt.BorderLayout; public class SecondUI extends JDialog { private final JPanel contentPanel = new JPanel(); /** * Create the dialog. */ public SecondUI() { setVisible(true); 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)); final OneUI obj = new OneUI(); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton btnAddOneElement = new JButton("Add 1 element"); btnAddOneElement.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { obj.updateModelCmb(); } }); contentPanel.add(btnAddOneElement); } } }
请帮忙 :(
I have a simple UI in which my button that calls my method updateModelCmb(), this method just increases the value of a counter and updates the model. The button seems to add the proper values to the model just fine. But when I do the same in my secondUI class, the model does not gets updated... am I doing something wrong? here is my code:
package testing; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.DefaultComboBoxModel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JComboBox; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class OneUI extends JFrame { private JPanel contentPane; private JComboBox comboBox ; private DefaultComboBoxModel modeltest; private Integer count=0; private JButton btnOpenSecondUi; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { OneUI frame = new OneUI(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ 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) { new SecondUI(); } }); btnOpenSecondUi.setBounds(155, 163, 161, 23); contentPane.add(btnOpenSecondUi); } public void updateModelCmb(){ count++; modeltest.addElement(count); comboBox.setModel(modeltest); } }
This is the second class which does not seem to work.
package testing; import java.awt.BorderLayout; public class SecondUI extends JDialog { private final JPanel contentPanel = new JPanel(); /** * Create the dialog. */ public SecondUI() { setVisible(true); 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)); final OneUI obj = new OneUI(); getContentPane().add(contentPanel, BorderLayout.CENTER); { JButton btnAddOneElement = new JButton("Add 1 element"); btnAddOneElement.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { obj.updateModelCmb(); } }); contentPanel.add(btnAddOneElement); } } }
Please help :(
原文:https://stackoverflow.com/questions/31708263
最满意答案
你可能想要这个:
@Pointcut("within(com.vanilla.blService.BlService+)") public void businessLogicMethods(){}
BlService+
表示BlService和所有子类/实现类。You probably want this:
@Pointcut("within(com.vanilla.blService.BlService+)") public void businessLogicMethods(){}
BlService+
means BlService and all subclasses / implementing classes.
相关问答
更多-
我有同样的问题,在Repository的建议工作,但Controller的建议不是。 最后我找到了解决方案。 简而言之,您需要确保在Servlet上下文中加载AOP定义,而不是在不同的上下文中加载。 在我的例子中,我的Spring AOP定义在tools-config.xml定义。 从这里移动后
contextConfigLocation classpath:spring/tools- ... -
首先,你所谓的方面缺少与方面相关的注释,例如@Aspect和@Before ,没有它,它只是一个POJO类。 它永远不会被执行或甚至被认为是一个方面。 其次,Spring AOP是一个基于代理的框架,它只能与Spring组件一起使用,而不能与POJO类一起使用。 您需要AspectJ才能解决您的问题。 (顺便说一句,你有没有读过教程或Spring或AspectJ手册?我猜你没有。) 此外,为什么您希望用户在运行时输入切入点? 您是否应该早点知道在哪里应用您的方面? 即使我会向您展示创建方面的解决方案,它们也 ...
-
切入点内的SPRING AOP未针对bean的set方法进行处理(SPRING AOP within pointcut not processed for set method of a bean)[2022-11-22]
用户pap在评论部分回答了我的问题 User pap has answered my question in the comment section -
我只是在检查你的代码,但我有一个预感,问题是你没有从Spring获取你的TixServiceImpl实例,而是你自己在你的TixClient手动实例化它。 我在考虑你的TixService需要是一个从Spring ApplicationContext获得的Spring bean,以便Spring有机会在返回的实例上设置方面。 I'm just inspecting your code, but I have a hunch the problem is that you're not getting you ...
-
用于排除IgnoreClass所有方法的IgnoreClass将是: @Pointcut("execution(* com.company.app..*(..)) && !execution(* com.company.app.IgnoreClass.*(..)) ") 这里有关于切入点表达式的文档,还有关于Spring AOP声明切入点的更多相关信息。 The Pointcut to exclude all methods in IgnoreClass would be: @Pointcut("exec ...
-
切入点内的Spring AOP(Spring AOP within pointcut)[2023-01-09]
你可能想要这个: @Pointcut("within(com.vanilla.blService.BlService+)") public void businessLogicMethods(){} BlService+表示BlService和所有子类/实现类。 You probably want this: @Pointcut("within(com.vanilla.blService.BlService+)") public void businessLogicMethods(){} BlServi ... -
@Pointcut("within(org.com.revanth.model.Circle)") public void allCircleMethods(){} 应该 @Pointcut("within(org.com.revanth.model.Circle.*)") public void allCircleMethods(){} @Pointcut("within(org.com.revanth.model.Circle)") public void allCircleMethods(){} ...
-
你做了一个典型的Spring AOP初学者的错误:你认为它适用于私有方法,但正如文档明确指出的那样,它没有。 Spring AOP基于动态代理,当使用CGLIB代理实现接口时,它们仅适用于通过JDK代理实现接口的公共方法,还适用于受保护和包范围的方法。 如果要从某个方面拦截它,则应该将worker()方法设为public。 PS:完全成熟的AspectJ也适用于私有方法,但转换到另一个AOP框架在这里会有点过分。 更新:您的代码中还有其他问题: 即使您将其公开,第一个worker方法也不会返回任何内容。 最 ...
-
Spring AOP:创建检查父类注释的切入点(Spring AOP: create pointcut which check parent class annotation)[2021-12-11]
我调查问题。 如果你使用类的扩展(它可能是抽象的),并且注释宣布为继承,那么我的切入点将起作用,但它不适用于已实现的接口。 下一个示例将起作用,但它不适用于已实现接口上的注释: @Step public abstract class TestAopComp { public abstract void test(); } @Component public class TestAopCompImpl extends TestAopComp{ public void test(){ ... -
问题是:Spring AOP将无法匹配这些切入点。 Spring AOP仅匹配公共方法的切入点。 您需要使用AspectJ编译或加载时间编织才能使其正常工作。 注意 由于Spring的AOP框架基于代理的特性,根据定义,受保护的方法既不会被拦截,也不会被拦截(对于这不适用),也不能用于CGLIB代理(这在技术上是可行的,但不建议用于AOP)。 因此,任何给定的切入点都只能与公共方法匹配! 如果您的拦截需要包括受保护/私有方法甚至构造函数,请考虑使用Spring驱动的本机AspectJ编织而不是Spring的 ...