首页 \ 问答 \ 我无法从JDialog更新Jcombobox(通过模型)(I can't update Jcombobox (via model) from a JDialog)

我无法从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
更新时间:2022-07-21 09:07

最满意答案

你可能想要这个:

@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手册?我猜你没有。) 此外,为什么您希望用户在运行时输入切入点? 您是否应该早点知道在哪里应用您的方面? 即使我会向您展示创建方面的解决方案,它们也 ...
  • 用户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 ...
  • 你可能想要这个: @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方法也不会返回任何内容。 最 ...
  • 我调查问题。 如果你使用类的扩展(它可能是抽象的),并且注释宣布为继承,那么我的切入点将起作用,但它不适用于已实现的接口。 下一个示例将起作用,但它不适用于已实现接口上的注释: @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的 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)