1

I'm making database application and I'm strugling with this problem for a week. I'm also not sure how to post title to my question, but I hope you will understand. I have two frame's: City and Country.

City frame:

enter image description here Country frame:

enter image description here

When I click on "Search" button in City frame it opens me a Country frame. And when I click on table in Country frame it passes value from selected row and column "Country" into a variable tableValCountry by this method:

table.addMouseListener(new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent e) {
            int selRow = table.getSelectedRow();

            String tableValCountry = (String)table.getValueAt(selRow, 1);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }});

Then I want to pass that tableValCountry variable to City frame and update one of textfield's by clicking add button. How can I achieve that? Thanks in advance.

P.S. I could do that with getters and setters but I would need to have some "fetch" button in my City frame to retrieve value (which I don't want to do). And also City frame must be visible all time.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • Note that there are better listeners for changes in table selection then a `MouseListener` (namely a `ListSelectionListener` added to the selection model of the `JTable`) – Robin Aug 26 '12 at 16:43
  • Go here and see why I choosed MouseListener instead of ListSelectionListener: http://stackoverflow.com/questions/11640905/java-listselectionlistener – Branislav Lazic Aug 26 '12 at 17:21

3 Answers3

2

The Swing way of doing things would be to have the search panel implement a getSelectedCountry() method and a list of listeners that are notified when the user makes a country selection.

When the user clicks search, your main component will create the search panel and register itself as a listener to search panel events. The event handler will get the search panel's selected country and populate the text field.

The search panel implementation just needs to notify all its listeners when the user selects the country.

I think this article covers it pretty well.

sjr
  • 9,769
  • 1
  • 25
  • 36
2

See The Use of Multiple JFrames, Good/Bad Practice?

For this use-case, modal dialogs would be perfect. The values can be queried the line after the dialog is set visible.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I saw that post, a milion times. And of course, I know that program with multiple frame's is very unfriendly for user. The deal is my "City" frame will be one frame (main window) composed from multiple panel's added to one tabbed pane (so "City" will be just a panel). But tell me more about modal dialog's. Any link? – Branislav Lazic Aug 26 '12 at 23:43
  • Ok, will do. Thanks for advice btw. – Branislav Lazic Aug 26 '12 at 23:56
0

I am facing the same problem but my alternate solution would be threading mechanism

Intact Abode
  • 382
  • 5
  • 20