problem in opening and closing swing forms

  • Follow


HiI have a windows form with a jtableI have a listenere that open a new form when a row of the jtable isclickedthis 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 = newModificaStudenteComunicazione(string, id_com, false);            nStud.setVisible(true);        }    }so a new form opens. in this form I have a Clode button that simplyclose 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 formA. 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 thelistener doesn't understand that I clicked on the jtable.is this the right code to use to open/close windows?thanks
0
Reply lmarantelli (2) 3/14/2007 10:59:39 AM

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 );     }}ByeMichael
0
Reply Michael 3/14/2007 11:27:26 AM


On 14 Mar, 12:27, Michael Rauscher <michlm...@gmx.de> 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> Michaelthanks for your reply MichaelBut with your code the second time I try to open B it doesn't openI placed a brakpoint in the first line of the method and the secondtime it seems not to pass there.
0
Reply Luke 3/14/2007 11:41:48 AM

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 );     }}ByeMichael
0
Reply Michael 3/14/2007 12:27:14 PM

On 14 Mar, 13:27, Michael Rauscher <michlm...@gmx.de> 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> Michaelthat's the same problemif you try to click on A and A il already selected, it doesn't openthe second window
0
Reply Luke 3/15/2007 1:13:26 PM

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.ByeMichael
0
Reply Michael 3/15/2007 1:27:12 PM

On 15 Mar, 14:27, Michael Rauscher <michlm...@gmx.de> 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> Michaelyesthe problem is that the second time I click on the same row theselection value is not changed, so the event doesn't fireis there a solution?thanks
0
Reply Luke 3/15/2007 1:40:16 PM

On Mar 16, 12:40 am, "Luke" <lmarante...@yahoo.it> wrote:> On 15 Mar, 14:27, Michael Rauscher <michlm...@gmx.de> 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.
0
Reply Andrew 3/15/2007 1:45:32 PM

Andrew Thompson wrote:> > How did you go with the MouseListener?Thanks ;)ByeMichael
0
Reply Michael 3/15/2007 2:28:18 PM

On 15 Mar, 15:28, Michael Rauscher <michlm...@gmx.de> wrote:> Andrew Thompson wrote:>> > How did you go with the MouseListener?>> Thanks ;)>> Bye> Michaelok, 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 = newElencoStudentiComunicazione(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 useredit it directly. If i doubleclick a cell, I can edit it, but I don'twant 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 needto "refresh" A, because maybe some values have changed. Is itpossible?thanks
0
Reply Luke 3/15/2007 2:52:46 PM

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.ByeMichael
0
Reply Michael 3/16/2007 5:11:00 AM

10 Replies
104 Views

(page loaded in 0.103 seconds)

Similiar Articles:













7/19/2012 8:38:19 PM


Reply: