problem in opening and closing swing forms

L

Luke

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
 
M

Michael Rauscher

Luke said:
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
 
L

Luke

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

Michael Rauscher

Luke said:
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
 
L

Luke

Luke said:
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
 
M

Michael Rauscher

Luke said:
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
 
L

Luke

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
 
L

Luke

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
 
M

Michael Rauscher

Luke said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top