Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

moveing two JList using one JScorllpane

  Asked By: Jason    Date: Jan 05    Category: Java    Views: 900
  

i had taken two Jlist both have the JscrollPane

i want to atoScroll to one Jlist when other Jlist is

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Emma Brown     Answered On: Jan 05

you could use this sample for your JList :


jt = new JTree(top);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jt, v, h);
panel.add(jsp, BorderLayout.CENTER);

 
Answer #2    Answered By: Willie Gomez     Answered On: Jan 05

Your explanation of your situation is kinda vague. Are your lists in the same
scroll pane or do
they have their own separate scroll pane?

Nevertheless, you might want to have a look at this:


package org.blahblah;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.util.Vector;

public class ClickOnTextField {

public static void main(String[] args) {

JFrame frame = new JFrame();

Vector<String> leftData = createData("left");
JList leftList = new JList(leftData);
final JScrollPane leftScroll = new JScrollPane(leftList);

Vector<String> rightData = createData("right");
JList rightList = new JList(rightData);
final JScrollPane rightScroll = new JScrollPane(rightList);

JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel,
BoxLayout.X_AXIS);
panel.setLayout(boxLayout);

panel.add(leftScroll);
panel.add(Box.createHorizontalStrut(5));
panel.add(rightScroll);

leftScroll.getViewport().
addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
rightScroll.getViewport().
setViewPosition(leftScroll.getViewport().
getViewPosition());
}
});

frame.add(panel);
frame.pack();
frame.setVisible(true);
}

public static Vector<String> createData(String side) {
Vector<String> data = new Vector<String>();

for (int i = 0; i < 50; i++) {
data.add(i + side);
}

return data;
}

}

 
Didn't find what you were looking for? Find more on moveing two JList using one JScorllpane Or get search suggestion and latest updates.




Tagged: