PeakGen
PeakGen

Reputation: 23025

ComponentListener is not working

Please have a look at the following code.

package normal;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainForm extends JFrame implements ComponentListener
{
    private JTabbedPane tab;

    private InsertForm insertForm;
    private UpdateDeleteForm updateDelete;
    private SearchForm searchForm;

    public MainForm()
    {
        tab = new JTabbedPane();
        insertForm = new InsertForm();
        updateDelete = new UpdateDeleteForm();
        searchForm = new SearchForm();

        //Creating the main window
        tab.add(insertForm,"Insert");
        tab.add(updateDelete,"Update/Delete");
        tab.add(searchForm,"Search");
        tab.addChangeListener(new TabChangeWork());

        getContentPane().add(tab);




       // this.setSize(500,500);
        this.setTitle("My Phone Book App");
        this.setResizable(false);
        this.pack();
        this.validate();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    @Override
    public void componentResized(ComponentEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void componentMoved(ComponentEvent e)
    {
        System.out.println("X Location: "+this.getX());
    }

    @Override
    public void componentShown(ComponentEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void componentHidden(ComponentEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private class TabChangeWork implements ChangeListener
    {

        @Override
        public void stateChanged(ChangeEvent e) 
        {
            JTabbedPane tabSource = (JTabbedPane)e.getSource();

            int index = tabSource.getSelectedIndex();
            System.out.println("Tab Changed to: "+tabSource.getTitleAt(index));


            if(tabSource.getTitleAt(index).equals("Update/Delete"))
            {
                updateDelete.addNames();
            }
        }

    }
}

In here, the ComponentListener is not working properly because the action inside the method "ComponentMoved" is not happening. I am trying to the get the new coordinates of the JFrame if this is moved. Why it is not working? Please help!!

Upvotes: 3

Views: 1662

Answers (3)

Andreas Dolk
Andreas Dolk

Reputation: 114787

Your MainForm is a ComponentListener. So it will listen to component events - but only, if you tell it, where to listen. Listeners don't listen to anything automatically. They're initially deaf.

Look at the component whose movement you want to monitor and register the main frame with that components addComponentListner method. Something like component.addComponentListener(this);

Upvotes: 3

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

You have not registered with the ComponentListener anywhere in the code...

Do it like this....

component_to_monitor.addComponentListener(this);

Upvotes: 6

Reimeus
Reimeus

Reputation: 159784

You have not called addComponentListener anywhere.

If you wish to register such a listener with your JFrame, then you can add:

addComponentListener(this);

or, for a sub component, e.g. searchForm:

searchForm.addComponentListener(this);

Upvotes: 3

Related Questions