0

I am trying to create a JTabbedPane with tabs arranged vertically LEFT with SCROLL_TAB_LAYOUT. The code snippet for this is as below:


private Component createTabbedPane() 
{
 JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT,
  JTabbedPane.SCROLL_TAB_LAYOUT);
 for (int i = 0; i < 20; i++) {
  JPanel pane = new JPanel();
  pane.add(new JLabel("This is Panel " + i));
  tabbedPane.addTab("Tab " + i, pane);
 }
 return tabbedPane;
}

However, I want have the same scrolling feature with mouse dragged so that it could be used for touch screen. Is there any way to use mouse listeners on the tabs?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
george b
  • 41
  • 1
  • 1
  • 4
  • 1
    This would be helpful to you. Have a look at http://stackoverflow.com/questions/60269/how-to-implement-draggable-tab-using-java-swing and http://java-swing-tips.blogspot.com/2008/04/drag-and-drop-tabs-in-jtabbedpane.html. Fist link is simpler. – Ravindra Gullapalli Jan 25 '12 at 05:11

1 Answers1

1

You can add mouseListners to the tabs themselves or to the tab components.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • I tried by overriding the processMouseEvent and processMouseMotionEvent method for mouse drag. It works fine for a jtable, but not working for jtabbedpane. Which method should I need to modify to get this dragging behavior? – george b Dec 09 '10 at 05:17
  • Hi I tried the following:
    protected void processMouseEvent(MouseEvent e) { switch (e.getID()) { case MouseEvent.MOUSE_PRESSED: startPoint = e.getPoint(); break; } super.processMouseEvent(e); }
    – george b Dec 10 '10 at 09:27
  • protected void processMouseMotionEvent(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_DRAGGED) { Rectangle r = getVisibleRect(); Point p = e.getPoint(); int dx = (startPoint.x - p.x); int dy = (startPoint.y - p.y); Rectangle aRect = new Rectangle(r.x + dx, r.y + dy, r.width + dx, r.height + dy); scrollRectToVisible(aRect); } } – george b Dec 10 '10 at 09:28
  • It is not allowing me to put the code in order, am I doing it correctly? – george b Dec 10 '10 at 09:29
  • the code is not formatted, this text box is not allowing to format the code the way i did in the first post; but these are the two methods I override for dragging. with these two methods the dragging is not working [it works for a jtable]. can you please tell me where i am going wrong? – george b Dec 13 '10 at 04:47