NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
ruthbenj
Joined: 01 Jun 2010 Posts: 8
|
Posted: Tue Jun 01, 2010 6:10 pm Post subject: Listening to scrolling event in JTable |
|
|
Hello,
I have 2 JTables that are positioned side by side in a JPanel with a (1 by 2) grid-layout. I want to synchronize the 2 JTables, like when I scroll the left JTable, the other JTable on the right will scroll in synchrony, ie, they must show the same row number as I scroll up & down. The same thing applies when I want to scroll the JTable on the right, where the JTable on the left must scroll in synchrony.
I looked at the events in the Netbeans IDE "properties pane" for JTable, but I couldn't find a "scrolling event". I've tried Google, but the info I get from there, confuses me rather than solving my problem.
Anyone on this list who can give me a hint of how to do this, would be much appreciated.
Cheers. |
|
| Back to top |
|
 |
Greg Baker Posted via mailing list.
|
Posted: Tue Jun 01, 2010 8:57 pm Post subject: Listening to scrolling event in JTable |
|
|
It's not the scroll event of the JTable, it is the scroll event of JScrollPane that contains the JTable. Look in the Inspector panel of NB and you'll see that your jtable is in the jscrollpane.
Search for "JScrollPane scroll event" and you should find some example code such as: http://www.exampledepot.com/egs/javax.swing/scroll_SpEvt.html
-----Original Message-----
From: ruthbenj [mailto:address-removed]
Sent: Tuesday, June 01, 2010 1:11 PM
To: address-removed
Subject: [nbusers] Listening to scrolling event in JTable
Hello,
I have 2 JTables that are positioned side by side in a JPanel with a (1 by 2) grid-layout. I want to synchronize the 2 JTables, like when I scroll the left JTable, the other JTable on the right will scroll in synchrony, ie, they must show the same row number as I scroll up & down. The same thing applies when I want to scroll the JTable on the right, where the JTable on the left must scroll in synchrony.
I looked at the events in the Netbeans IDE "properties pane" for JTable, but I couldn't find a "scrolling event". I've tried Google, but the info I get from there, confuses me rather than solving my problem.
Anyone on this list who can give me a hint of how to do this, would be much appreciated.
Cheers. |
|
| Back to top |
|
 |
ruthbenj
Joined: 01 Jun 2010 Posts: 8
|
Posted: Wed Jun 02, 2010 3:15 am Post subject: Scrolling event |
|
|
Greg,
Thanks for the message.
I am using the code from the link that you posted above (repasted below):
http://www.exampledepot.com/egs/javax.swing/scroll_SpEvt.html
I have one object of type "AdjustmentListener" , that both the JScrollPane objects listen to, as shown:
------------------------------------------
AdjustmentListener listener = new MyAdjustmentListener();
paneLeft.getVerticalScrollBar().addAdjustmentListener(listener);
paneRight.getVerticalScrollBar().addAdjustmentListener(listener);
------------------------------------------
I still can't figure out how can I make it to synchronize the 2 JTables when scrolling.
I tried the following code (based on the link in your post above):
-------------------------------------------------
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable sourceAdjust = evt.getAdjustable();
if (evt.getValueIsAdjusting()) {
Object obj = evt.getSource();
if (obj instanceof JScrollPane) {
System.out.println("JScrollPane Object");
} else if (obj instanceof JTable) {
System.out.println("JTable Object");
} else if (obj instanceof JViewport) {
System.out.println("JViewport Object");
} else {
System.out.println("Unknown Object");
}
}
}
-------------------------------------------------
And my output is always "Unknown Object", regardless whether I am scrolling the left JTable or scrolling the right JTable. So, I don't know how to identify which table that I am scrolling.
Would you have any other idea of how to synchronize the scrolling of the 2 JTables?
Cheers. |
|
| Back to top |
|
 |
Greg Baker Posted via mailing list.
|
Posted: Wed Jun 02, 2010 4:44 pm Post subject: Listening to scrolling event in JTable |
|
|
Here's a quick test sample. Create a dialog with 2 jtables. Name the scrollpanes pane1 and pane2 and add the code below. It sync's pane2 with pane1... You'll need to add code to determine which pane sent the event to the handler and adjust the code accordingly.
In the constructor I set both tables to the same table model and fill the same test data in them.
In the setRows() I create the scrollbar listeners which require adjustmentValueChanged().
Hope this helps....
Greg
import java.awt.event.AdjustmentEvent; // scrollbar event
import java.awt.event.AdjustmentListener; // scrollbar listener
import javax.swing.table.DefaultTableModel; // table data model
public class SyncDlg extends javax.swing.JDialog implements AdjustmentListener {
DefaultTableModel model = new DefaultTableModel(); // demo table model
/** Creates new form SyncDlg */
public SyncDlg(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
jTable1.setModel(model); // create demo table model
jTable2.setModel(model); // for both tables
setRows(); // build demo tables
}
private void setRows() {
model.addColumn("Col1"); // create table columns / both tables
model.addColumn("Col2");
// fill both tables with test data
for (int i = 0; i < 100; i++) {
model.addRow(new Object[]{String.valueOf(i), "Demo Data"});
}
// create scrollbar listeners for both tables
pane1.getHorizontalScrollBar().addAdjustmentListener(this);
pane1.getVerticalScrollBar().addAdjustmentListener(this);
pane2.getHorizontalScrollBar().addAdjustmentListener(this);
pane2.getVerticalScrollBar().addAdjustmentListener(this);
}
// jscrollpane listener method override - required
public void adjustmentValueChanged(AdjustmentEvent evt) {
Object obj = evt.getSource();
lblMsg.setText(obj.toString());
if(evt.getValueIsAdjusting()){
return; // adjusting scroll bar - do nothing yet
}
// need to determine which scrollpane sent message.... then sync
//.....this is your job
//if pane1 then - syncs pane2 with pane1
pane2.getVerticalScrollBar().setValue(pane1.getVerticalScrollBar().getValue());
// if pane2 then
//pane2.getVerticalScrollBar().setValue(pane1.getVerticalScrollBar().getValue());
}
//.....Other code, main() etc.
}
-----Original Message-----
From: ruthbenj [mailto:address-removed]
Sent: Tuesday, June 01, 2010 10:16 PM
To: address-removed
Subject: [nbusers] Listening to scrolling event in JTable
Greg,
Thanks for the message.
I am using the code from the link that you posted above (repasted below):
http://www.exampledepot.com/egs/javax.swing/scroll_SpEvt.html
I have one object of type "AdjustmentListener" , that both the JScrollPane objects listen to, as shown:
------------------------------------------
AdjustmentListener listener = new MyAdjustmentListener();
paneLeft.getVerticalScrollBar().addAdjustmentListener(listener);
paneRight.getVerticalScrollBar().addAdjustmentListener(listener);
------------------------------------------
I still can't figure out how can I make it to synchronize the 2 JTables when scrolling.
I tried the following code (based on the link in your post above):
-------------------------------------------------
public void adjustmentValueChanged(AdjustmentEvent evt) {
Adjustable sourceAdjust = evt.getAdjustable();
if (evt.getValueIsAdjusting()) {
Object obj = evt.getSource();
if (obj instanceof JScrollPane) {
System.out.println("JScrollPane Object");
} else if (obj instanceof JTable) {
System.out.println("JTable Object");
} else if (obj instanceof JViewport) {
System.out.println("JViewport Object");
} else {
System.out.println("Unknown Object");
}
}
}
-------------------------------------------------
And my output is always "Unknown Object", regardless whether I am scrolling the left JTable or scrolling the right JTable. So, I don't know how to identify which table that I am scrolling.
Would you have any other idea of how to synchronize the scrolling of the 2 JTables?
Cheers. |
|
| Back to top |
|
 |
ruthbenj
Joined: 01 Jun 2010 Posts: 8
|
Posted: Fri Jun 04, 2010 6:50 pm Post subject: |
|
|
Greg, thanks for your useful tips. It helped.
Cheers. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|