problem in opening and closing swing forms

Discussion in 'Java' started by Luke, Mar 14, 2007.

  1. Luke

    Luke Guest

    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
    Luke, Mar 14, 2007
    #1
    1. Advertising

  2. Luke wrote:
    > 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);
    > }
    > }


    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
    Michael Rauscher, Mar 14, 2007
    #2
    1. Advertising

  3. Luke

    Luke Guest

    On 14 Mar, 12:27, Michael Rauscher <> wrote:
    > Luke wrote:
    > > 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);
    > > }
    > > }

    >
    > 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



    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.
    Luke, Mar 14, 2007
    #3
  4. Luke wrote:
    > 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
    Michael Rauscher, Mar 14, 2007
    #4
  5. Luke

    Luke Guest

    On 14 Mar, 13:27, Michael Rauscher <> wrote:
    > Luke wrote:
    > > 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



    that's the same problem
    if you try to click on A and A il already selected, it doesn't open
    the second window
    Luke, Mar 15, 2007
    #5
  6. Luke wrote:
    > 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
    Michael Rauscher, Mar 15, 2007
    #6
  7. Luke

    Luke Guest

    On 15 Mar, 14:27, Michael Rauscher <> wrote:
    > Luke wrote:
    > > 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



    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
    Luke, Mar 15, 2007
    #7
  8. On Mar 16, 12:40 am, "Luke" <> wrote:
    > On 15 Mar, 14:27, Michael Rauscher <> wrote:
    >
    > > Luke wrote:
    > > > 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.
    Andrew Thompson, Mar 15, 2007
    #8
  9. Andrew Thompson wrote:
    >
    > How did you go with the MouseListener?


    Thanks ;)

    Bye
    Michael
    Michael Rauscher, Mar 15, 2007
    #9
  10. Luke

    Luke Guest

    On 15 Mar, 15:28, Michael Rauscher <> wrote:
    > Andrew Thompson wrote:
    >
    > > 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
    Luke, Mar 15, 2007
    #10
  11. Luke wrote:

    > 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?


    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
    Michael Rauscher, Mar 16, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nemesis
    Replies:
    0
    Views:
    529
    Nemesis
    Oct 6, 2005
  2. Nemesis
    Replies:
    2
    Views:
    1,379
  3. Allan Ebdrup
    Replies:
    0
    Views:
    2,701
    Allan Ebdrup
    Jan 26, 2006
  4. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    644
    Thomas Hansen
    Feb 15, 2007
  5. thomas
    Replies:
    0
    Views:
    218
    thomas
    Oct 23, 2003
Loading...

Share This Page