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: Eclipse - project not built - comp.lang.java.programmerI have tried Project > Clean,closing and opening Eclipse ... programmer... write file: filename.".Fix the problem ... not built - comp.lang.java.programmer Dynamic form ... Inserting new line of code between existing XML code - comp.soft ...... scheduled="false" updateModel="false"> <Problem> I want to insert a line of the form ... XML ... beginning of new lines inserted between the opening and closing ... Closing a database - comp.databases.mysqlDatabase Service Manager encountered a problem and ... I am opening a form name "Martin", then I open a form ... How to close a form Martin and Ann form while closing... Open new JPanel (JFrame, ...) by clicking JButton - comp.lang.java ...Hello, I tried it for opening the searchFrame ... what you suggested me: thanks!). Only closing a JFrame seems to be a problem? ... comp.lang.java.gui... import javax.swing ... window.open() with username and password - comp.lang.javascript ...I have the following problem: There's a shared folder on ... the way to foce window.open() to use logging form. ... Expect on Windows, SSH/Telnet processes not closing ... Preventing A New Window From Grabbing Focus - comp.lang.java ...I guess the problem is more the ... class NFW extends javax.swing.JFrame { /** Creates new form ... entire queue, instead of closing aftereach data download then opening ... Populate form-fillable pdf from command line - comp.text.pdf ...... some sample iText code to fill my test form. It's a simple enough problem ... PDF, if you have not saved it before closing ... That is to populate the fields prior to opening ... DSP Job opening in Sacramento, CA - comp.dspWe have quite a few DSP job opening at Northrop ... impression that our alarming illegal immigration problem ... Did they give him forms in Greek or provide him with ... regsub (and regular expressions in general) trouble. - comp.lang ...The problem I'm having is that I can't get it to ... file whose contents includes sequences of the form ... directive stands on a line of its own, and its closing ... creating a button on a figure to stop the program. - comp.soft-sys ...Stop Window Closing - comp.databases.filemaker ... to stop the program. - comp.soft-sys ... problem with opening a ... lang.pascal.misc Of course if you program the forms ... Garage Door Problems Often Require Only Minor Repairs by ...Swing-up doors have a plate where the spring is ... systems for setting the opening and closing limit on an opener. ... our hiring tips and download the useful forms ... opening and closing form - Excel Templates | Excel Add-ins and ...The problem is that the mascro sub is empty, I made te form by clicking on UserForm. ... Re: opening and closing form 7/19/2012 8:38:19 PM
|