swing - Set cursor for jTabbedPane's tab in java -
i have created custom jtabbedpane
class extends basictabbedpaneui
, have created desired jtabbedpane
problem how can set hand cursor each tab in custom jtabbedpane?
i tried set cursor this
tabbedpane.setui(new custommainmenutabs()); tabbedpane.setcursor(new cursor((cursor.hand_cursor)));
this sets cursor whole of jtabbedpane want set cursor when mouse hovers on of tab in only.
how can set hand cursor tabs in jtabbedpane?
my code is
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 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 jtabbedpane implementing 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 set cursor hand_cursor cursor when mouse hovers on tab not jpanel or other component. great if done without mouse listener.
i see lot of answers here way complicated (custom uis, listeners, graphics stuff, etc.).
basically, camickr spelled out you. here's 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); } } }
Comments
Post a Comment