Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

problem in opening and closing swing forms

Thread view: 
Luke - 14 Mar 2007 11:59 GMT
Hi
I have a windows form with a jtable
I have a listenere that open a new form when a row of the jtable is
clicked

this is the event:
public void valueChanged(ListSelectionEvent event) {
       if(event.getSource() == jTable1.getSelectionModel() &&
event.getFirstIndex() >= 0 ) {
           TableModel model = (TableModel)jTable1.getModel();
           String string =
(String)model.getValueAt(jTable1.getSelectedRow(), 0);
           ModificaStudenteComunicazione nStud = new
ModificaStudenteComunicazione(string, id_com, false);
           nStud.setVisible(true);
       }
   }

so a new form opens. in this form I have a Clode button that simply
close the window. the code of the close button is;:
this.dispose();

this works right only for 2 times. I mean:
I open form B from form A clicking on a row of the jtable inside form
A. When B il loaded I click close and B closes.
I do the same another time and it works ok.
I do the same for the 3rd time and B doesn't open. It seems that the
listener doesn't understand that I clicked on the jtable.

is this the right code to use to open/close windows?

thanks
Michael Rauscher - 14 Mar 2007 12:27 GMT
> Hi
> I have a windows form with a jtable
[quoted text clipped - 13 lines]
>         }
>     }

Poss. Problem 1: the event may be one from a series of events.
Poss. Problem 2: getFirstIndex returns 'the index of the first row
                 whose selection may have changed'.

There might be more :)

Try the following instead:

public void valueChanged( ListSelectionEvent e ) {
    if ( e.getValueIsAdjusting() )
        return;

    int row = jTable1.getSelectedRow();
    if ( row != -1 ) {
        TableModel model = jTable1.getModel();
        String string = (String)model.getValueAt(row, 0);
        ModificaStudenteComunicazione nStud =
            new ModificaStudenteComunicazione(string, id_com, false);
        nStud.setVisible( true );
    }
}

Bye
Michael
Luke - 14 Mar 2007 12:41 GMT
> > Hi
> > I have a windows form with a jtable
[quoted text clipped - 39 lines]
> Bye
> Michael

thanks for your reply Michael
But with your code the second time I try to open B it doesn't open
I placed a brakpoint in the first line of the method and the second
time it seems not to pass there.
Michael Rauscher - 14 Mar 2007 13:27 GMT
> But with your code the second time I try to open B it doesn't open
> I placed a brakpoint in the first line of the method and the second
> time it seems not to pass there.

Here's an SSCCE:

import javax.swing.*;
import javax.swing.event.*;

public class Test {

    public static void main( String args[] ) {
        final JTable table = new JTable( new String[][]{
            {"A","B"},{"C","D"},{"E","F"}}, new String[] {"A","B"} );
        table.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {
            public void valueChanged( ListSelectionEvent e ) {
                if ( e.getValueIsAdjusting() )
                    return;
                int row = table.getSelectedRow();
                if ( row != -1 )
                    JOptionPane.showMessageDialog( null,
                        table.getModel().getValueAt(row,0) );
            }
        });

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.add( new JScrollPane(table) );
        frame.pack();
        frame.setVisible( true );
    }
}

Bye
Michael
Luke - 15 Mar 2007 14:13 GMT
> > But with your code the second time I try to open B it doesn't open
> > I placed a brakpoint in the first line of the method and the second
[quoted text clipped - 33 lines]
> Bye
> Michael

that's the same problem
if you try to click on A and A il already selected, it doesn't open
the second window
Michael Rauscher - 15 Mar 2007 14:27 GMT
> if you try to click on A and A il already selected, it doesn't open
> the second window

Ah, I understand the problem now. So, you'll need an other listener
(perhaps a MouseListener will do) since the selection doesn't change.

Bye
Michael
Luke - 15 Mar 2007 14:40 GMT
> > if you try to click on A and A il already selected, it doesn't open
> > the second window
[quoted text clipped - 4 lines]
> Bye
> Michael

yes
the problem is that the second time I click on the same row the
selection value is not changed, so the event doesn't fire
is there a solution?

thanks
Andrew Thompson - 15 Mar 2007 14:45 GMT
> > > if you try to click on A and A il already selected, it doesn't open
> > > the second window
...
> > ..you'll need an other listener
> > (perhaps a MouseListener will do)
..
> is there a solution?

How did you go with the MouseListener?

Andrew T.
Michael Rauscher - 15 Mar 2007 15:28 GMT
> How did you go with the MouseListener?

Thanks ;)

Bye
Michael
Luke - 15 Mar 2007 15:52 GMT
> > How did you go with the MouseListener?
>
> Thanks ;)
>
> Bye
> Michael

ok, now it works. this is the code:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

jTable1.addMouseListener(new MouseAdapter() {

           public void mouseClicked(MouseEvent e) {
               TableModel model = (TableModel)jTable1.getModel();
               BigDecimal id =
(BigDecimal)model.getValueAt(jTable1.getSelectedRow(), 0);
               String string = id.toString();
               ElencoStudentiComunicazione nStud = new
ElencoStudentiComunicazione(string);

nStud.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
               nStud.setVisible(true);
           }
       });

thanks for your help!
however I have 2 other problems.
1. I don't know how to "lock" the row in order not to let the user
edit it directly. If i doubleclick a cell, I can edit it, but I don't
want to let user do this. is there a property of the jtable?

2. When I open the second form (B) from A, and then I close B, I need
to "refresh" A, because maybe some values have changed. Is it
possible?

thanks
Michael Rauscher - 16 Mar 2007 06:11 GMT
> 1. I don't know how to "lock" the row in order not to let the user
> edit it directly. If i doubleclick a cell, I can edit it, but I don't
[quoted text clipped - 3 lines]
> to "refresh" A, because maybe some values have changed. Is it
> possible?

Do you know about MVC? Swing follows this approach.

JTable (together with it's UI delegate) provides a view to a TableModel.
To stay up to date JTable registers a TableModelListener with the
TableModel which informs it's listeners about any changes.

ad 1) Have a look at TableModel#isCellEditable.
ad 2) Have a look at AbstractTableModel#fireXXX methods.

Bye
Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.