Inject Singleton via Spring

135 Views Asked by At

I am having two tabs and want to set a text in one tab and display it on another tab. Therefore, this text will be displayed in another class.

I have the following architecture:

My applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />

    <context:component-scan base-package="test.DELETEME"
        annotation-config="true" />

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <bean id="mainGUI" class="test.DELETEME.MainWdw" />
</beans>

Here I inject the applicationContext:

public class Singleton {

    /**
     * Application Context
     */
    private static ApplicationContext ctx;

    public static void main(String[] args) {
        ctx = new ClassPathXmlApplicationContext("applicationContext_Test.xml");
        MainWdw gui = ctx.getBean("mainGUI", MainWdw.class);
        gui.start();
    }   
}

Here I am creating my Tabs and setting my two TabPanels, ResultsPan and MainTabPan:

@Component
public class MainWdw extends JFrame {

    /**
     * UUID
     */
    private static final long serialVersionUID = -4931876787108249107L;

    public static JTabbedPane tabbedPane;

    public static ResultsPan resultsTabPanel; 

    @Autowired
    private MainTabPan mainTabPan; 

    private void makeLayout() throws Exception {

        setLayout(new BorderLayout());

        createTabBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }

    /**
     * create Tab Menu
     * @throws Exception 
     */
    public void createTabBar() throws Exception {

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        /*
         * Main View 
         */
        tabbedPane.addTab("Main", mainTabPan.createLayout());
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        /*
         * Result table
         */
        tabbedPane.addTab("Results", resultsTabPanel.createLayout());
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);


        //Add the tabbed pane to this panel.
        add(tabbedPane);

        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

    }

    /**
     * starts the GUI
     */
    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    makeLayout();
                } catch (Exception e) {
                    e.printStackTrace();
                }   
            }
        });
    }

    public static JTabbedPane getInstance() {
        if(tabbedPane == null) {
            tabbedPane = new JTabbedPane();
        }
        return tabbedPane;
    }

    @Autowired
    public void setResultsTabPanel (ResultsPan resultsTabPane){
        MainWdw.resultsTabPanel = resultsTabPane;
    }
}

My first panel is the ResultsPan, which should display the text:

@Component
public class ResultsPan extends JPanel{

    /**
     * UUID.
     */
    private static final long serialVersionUID = 5940818789959562707L;

    public String text = "";

    public JScrollPane createLayout() {
        JPanel panel = new JPanel(new MigLayout(""));
        JScrollPane sp;


        JLabel lab = new JLabel(text);
        lab.setFont(new Font("Tahoma", Font.BOLD, 15));

        panel.add(lab, "wrap");

        sp = new JScrollPane(panel);
        sp.repaint();
        sp.validate();

        return sp;
    }

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * @param text the text to set
     */
    public void setText(String text) {
        this.text = text;
    }
}

My second panel is the MainTabPan, which should pass the text to the ResultsTab:

@Component
public class MainTabPan extends JPanel {

    /**
     * UUID.
     */
    private static final long serialVersionUID = 5940818789959562707L;

    @Autowired
    public ResultsPan resPan;

    private JTextField txt;

    public JScrollPane createLayout() {
        JPanel panel = new JPanel(new MigLayout(""));
        JScrollPane sp;


        JLabel lab = new JLabel("Set text in ResultsTabPanel: ");
        txt = new JTextField();
        JButton btn = new JButton("Set text");

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                String text = txt.getText();
                resPan.createLayout();
                resPan.setText(text);
            }
        });

        panel.add(lab, "wrap");
        panel.add(txt, "growx, wrap");
        panel.add(btn, "wrap");

        sp = new JScrollPane(panel);
        sp.repaint();
        sp.validate();

        return sp;
    }

}

My problem is, that nothing gets passed to the second tab and nothing gets displayed.

Any recommendation what I am doing wrong?

I really appreciate your replies!

0

There are 0 best solutions below