Reputation: 1
I already have a custom control base on JList like this
public class MyClass extends JList<String> {
private DefaultListModel<String> items = new DefaultListModel<String>();
public MyClass() {
setModel(items);
}
public String getAAAA() { //code here.. }
public int getBBB() { //code here... }
}
But I want the Jlist have scrollbar, so I change the super class of my custom control to JScrollPane and add the JList into it. code like this
public class MyClass extends JScrollPanel {
private JList<String> list = new JList<String>();
private DefaultListModel<String> items = new DefaultListModel<String>();
public MyClass() {
list.setModel(items);
this.setViewportView(list);
}
public String getAAAA() { //code here.. }
public int getBBB() { //code here... }
}
Well, if MyClass extends JList, in the JFrame I can addMouseListener to MyClass object and in mouseClicked I compare the MouseEvent getSource is a instance of MyClass and call these method of MyClass.
@Override
public void mouseClicked(MouseEvent arg0)
{
Object source = arg0.getSource();
if (source instanceof MyClass)
{
String a = ((MyClass) source).getAAAA();
int b = ((MyClass) source).getBBB();
}
}
But if MyClass extends JScrollPane, I add MouseListener to MyClass object and like above, in mouseClicked I compare the MouseEvent getSource instanceof MyClass and call these method (getAAA(), getBBB()...) but it's not working??
Upvotes: 0
Views: 172
Reputation: 879
You likely do not want to extend JScrollPane. You can add any Component to a JScrollPane to achieve having scroll bars. JList is a component, so you can add it directly. There shouldn't be much you need to do.
see: http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
Upvotes: 2