Set cursor for jTabbedPane tab in java

I created a custom jTabbedPane class that extends BasicTabbedPaneUI and successfully created my desired jTabbedPane , but now the problem is that I can set the Hand pointer for each tab in my custom jTabbedPane

I tried to set the cursor using this

 tabbedPane.setUI(new CustomMainMenuTabs()); tabbedPane.setCursor(new Cursor((Cursor.HAND_CURSOR))); 

this sets the cursor for the whole jTabbedPane, but I want to set the cursor when the mouse is over only any tab.

How to set tabs cursor in jTabbedPane?

My code

 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import javax.swing.plaf.basic.BasicTabbedPaneUI; public class HAAMS { //My Custom class for jTabbedPane public static class CustomMainMenuTabs extends BasicTabbedPaneUI { protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) { Graphics2D g2 = (Graphics2D) g; Color color; if (isSelected) { color = new Color(74, 175, 211); } else if (getRolloverTab() == tabIndex) { color = new Color(45, 145, 180); } else {color = new Color(68, 67, 67);} g2.setPaint(color); g2.fill(new RoundRectangle2D.Double(x, y, w, h, 30, 30)); g2.fill(new Rectangle2D.Double(x + 100,y,w,h)); } } public static void main(String[] args) { JFrame MainScreen = new JFrame("Custom JTabbedPane"); MainScreen.setExtendedState(MainScreen.getExtendedState() | JFrame.MAXIMIZED_BOTH); //Setting UI for my jTabbedPane implementing my custom class CustomMainMenuTabs JTabbedPane jtpane = new JTabbedPane(2); jtpane.setUI(new CustomMainMenuTabs()); jtpane.add("1st Tabe", new JPanel()); jtpane.add("2nd Tabe", new JPanel()); jtpane.add("3rd Tabe", new JPanel()); MainScreen.getContentPane().add(jtpane); MainScreen.setVisible(true); } } 

How to set the cursor to the HAND_CURSOR cursor when the mouse hovers over any tab, but not jpanel or any other component. It would be great if this were done without a mouse listener.

+5
source share
7 answers

Here I see a lot of answers that are too complex (user interfaces, extra listeners, graphics stuff, etc.).

Basically, camickr outlined this for you. Here is a simple demo:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.*; public class JTabbedPaneCursorDemo implements Runnable { JTabbedPane tabbedPane; public static void main(String[] args) { SwingUtilities.invokeLater(new JTabbedPaneCursorDemo()); } public void run() { JPanel panelA = new JPanel(); JPanel panelB = new JPanel(); tabbedPane = new JTabbedPane(); tabbedPane.addTab("A", panelA); tabbedPane.addTab("B", panelB); tabbedPane.addMouseMotionListener(new MouseMotionListener() { public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) { adjustCursor(e); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 200); frame.getContentPane().add(tabbedPane, BorderLayout.CENTER); frame.setLocationRelativeTo(null); frame.setVisible(true); } private void adjustCursor(MouseEvent e) { TabbedPaneUI ui = tabbedPane.getUI(); int index = ui.tabForCoordinate(tabbedPane, e.getX(), e.getY()); if (index >= 0) { tabbedPane.setCursor(new Cursor((Cursor.HAND_CURSOR))); } else { tabbedPane.setCursor(null); } } } 
+3
source

I want to set the cursor when the mouse moves on any tab.

I would suggest that you need to add MouseMotionListener to the tabbed panel. Then, when the mouseMoved(...) event is mouseMoved(...) , you check to see if the mouse is above the tab.

You can use the tabForCoordinate(...) method for BasicTabbePaneUI to determine if the mouse is above the tab or not.

+2
source

Steps:

  • Create a MouseMotionListener and add it to JTabbedPane
  • Inside the listener -> mouseMoved method, chec kif the current mouse position is inside the borders of your tabs
    • If true, move the cursor to the mouse pointer
    • else will show the default cursor

1. Method to check if the mouse is within the tabs:

 private static int findTabPaneIndex(Point p, JTabbedPane tabbedPane) { for (int i = 0; i < tabbedPane.getTabCount(); i++) { if (tabbedPane.getBoundsAt(i).contains(px, py)) { return i; } } return -1; } 

2. Mouse listener:

  MouseMotionListener listener = new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); if (findTabPaneIndex(e.getPoint(), tabbedPane) > -1) { tabbedPane.setCursor(new Cursor((Cursor.HAND_CURSOR))); } else { tabbedPane.setCursor(new Cursor((Cursor.DEFAULT_CURSOR))); } } }; 

3. To add a listener to JTabbedPane:

  jtpane.addMouseMotionListener(listener); 

Additional documentation:

End Code:

Connecting all the points together, you get the following:

 import java.awt.Color; import java.awt.Cursor; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.plaf.basic.BasicTabbedPaneUI; public class HAAMS { // My Custom class for jTabbedPane public static class CustomMainMenuTabs extends BasicTabbedPaneUI { protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) { Graphics2D g2 = (Graphics2D) g; Color color; if (isSelected) { color = new Color(74, 175, 211); } else if (getRolloverTab() == tabIndex) { color = new Color(45, 145, 180); } else { color = new Color(68, 67, 67); } g2.setPaint(color); g2.fill(new RoundRectangle2D.Double(x, y, w, h, 30, 30)); g2.fill(new Rectangle2D.Double(x + 100, y, w, h)); } } public static void main(String[] args) { JFrame MainScreen = new JFrame("Custom JTabbedPane"); MainScreen.setExtendedState(MainScreen.getExtendedState() | JFrame.MAXIMIZED_BOTH); JTabbedPane jtpane = new JTabbedPane(2); jtpane.setUI(new CustomMainMenuTabs()); MouseMotionListener listener = new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); if (findTabPaneIndex(e.getPoint(), tabbedPane) > -1) { tabbedPane.setCursor(new Cursor((Cursor.HAND_CURSOR))); } else { tabbedPane.setCursor(new Cursor((Cursor.DEFAULT_CURSOR))); } } }; jtpane.add("1st Tabe", new JPanel()); jtpane.add("2nd Tabe", new JPanel()); jtpane.add("3rd Tabe", new JPanel()); jtpane.addMouseMotionListener(listener); MainScreen.getContentPane().add(jtpane); MainScreen.setVisible(true); } private static int findTabPaneIndex(Point p, JTabbedPane tabbedPane) { for (int i = 0; i < tabbedPane.getTabCount(); i++) { if (tabbedPane.getBoundsAt(i).contains(px, py)) { return i; } } return -1; } } 
0
source

You can use:

 public void setTabComponentAt(int index, Component component) 

And then you do

 component.addMouseListener(yourListener) 
0
source

I changed the main mentod according to your need, so that the Hand cursor is visible only in the tab header. check if your problem resolves

Working code

 public static void main(String[] args) { JFrame MainScreen = new JFrame("Custom JTabbedPane"); MainScreen.setExtendedState(MainScreen.getExtendedState() | JFrame.MAXIMIZED_BOTH); MouseListener listener = new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { JTabbedPane jp=(JTabbedPane)(e.getComponent().getParent().getParent()); jp.setSelectedIndex(jp.indexAtLocation(e.getComponent().getX(),e.getComponent().getY())); } @Override public void mouseEntered(MouseEvent e) { e.getComponent().setCursor(new Cursor((Cursor.HAND_CURSOR))); } }; JLabel jlabel1=new JLabel("1st Tabe"); jlabel1.addMouseListener(listener); JLabel jlabel2=new JLabel("2nd Tabe"); jlabel2.addMouseListener(listener); JLabel jlabel3=new JLabel("3rd Tabe"); jlabel3.addMouseListener(listener); //Setting UI for my jTabbedPane implementing my custom class CustomMainMenuTabs JTabbedPane jtpane = new JTabbedPane(2); jtpane.setUI(new CustomMainMenuTabs()); jtpane.add("1st Tabe", new JPanel()); jtpane.setTabComponentAt( 0, jlabel1); jtpane.add("2nd Tabe", new JPanel()); jtpane.setTabComponentAt(1, jlabel2); jtpane.add("3rd Tabe", new JPanel()); jtpane.setTabComponentAt( 2, jlabel3); MainScreen.getContentPane().add(jtpane); MainScreen.setVisible(true); } 
0
source

Short

Just add this code to your CustomMainMenuTabs :

  public static class CustomMainMenuTabs extends BasicTabbedPaneUI { protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) { // ... } private static final Cursor DEFAULT_CURSOR = Cursor.getDefaultCursor(); private static final Cursor HAND_CURSOR = new Cursor((Cursor.HAND_CURSOR)); protected void setRolloverTab(int index) { tabPane.setCursor((index != -1) ? HAND_CURSOR : DEFAULT_CURSOR); super.setRolloverTab(index); } } 

Explanation

Since you are already extending BasicTabbedPaneUI , you can simply extend the mechanics for drawing the rollover tab, which is already implemented there, without the need to use more listeners or calculate coordinates yourself.

Rollback is the mechanic that was present in the component since Java 5, and this is the correct extension, you just need to override and extend the method. This method is called whenever the mouse moves in the component of the tab (it affects the tab area, but does not affect children), and it is constantly updated.

I tried your code snippet with this add-on and worked great.

0
source

This is actually a lot simpler than installing a UI delegate.

You can set your own labels as tab components (components inside the tab handles) that will have their own cursors. Below is a simple example with 3 tabs and another cursor for the body of the tab and each tab:

 import java.awt.*; import javax.swing.*; public class TestTabCursor extends JFrame { private JTabbedPane contentPane; public TestTabCursor() { super("Test tab cursor"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(640, 480); setLocation(100, 100); createContentPane(); setCursors(); } private void createContentPane() { contentPane = new JTabbedPane(); addTab(contentPane); addTab(contentPane); addTab(contentPane); setContentPane(contentPane); } private void addTab(JTabbedPane tabbedPane) { int index = tabbedPane.getTabCount() + 1; JLabel label = new JLabel("Panel #" + index); label.setHorizontalAlignment(SwingConstants.CENTER); label.setFont(label.getFont().deriveFont(72f)); JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(Color.white); panel.add(label, BorderLayout.CENTER); JLabel title = new JLabel("Tab " + index); tabbedPane.add(panel); tabbedPane.setTabComponentAt(index - 1, title); } private void setCursors() { contentPane.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); contentPane.getTabComponentAt(0).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); contentPane.getTabComponentAt(1).setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); contentPane.getTabComponentAt(2).setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new TestTabCursor(); frame.setVisible(true); } }); } } 
0
source

Source: https://habr.com/ru/post/1203147/


All Articles