Adding a JLabel on top of a JPanel - java

I attempting to place a .jpg icon on top of a JPanel in order to represent a board piece on a board. I have a GUI folder with the .java files and another folder containing the .jpg files.
--Major Edit--
Example Code
When a square is clicked a white icon is meant to be placed then black etc etc. This is a very basic example of what im trying to achieve
import java.awt.Dimension;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class gui extends JFrame implements MouseListener {
/**
*
*/
private static final long serialVersionUID = -973341728129968945L;
JLayeredPane layeredPane;
JPanel board;
JLabel piece;
int numSquares;
private boolean currentPlayer;
public gui(){
Dimension boardSize = new Dimension(600, 600);
numSquares = 6;
currentPlayer = true;
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
board = new JPanel();
layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);
board.setLayout( new GridLayout(numSquares, numSquares) );
board.setPreferredSize( boardSize );
board.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < (numSquares * numSquares); i++) {
JPanel square = new JPanel( new BorderLayout() );
square.setBorder(BorderFactory.createLineBorder(Color.black));
square.setBackground(Color.green);
board.add( square );
}
}
public static void main(String[] args) {
JFrame frame = new gui();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
#Override
public void mouseClicked(MouseEvent e) {
JPanel temp = (JPanel)board.findComponentAt(e.getX(), e.getY());
System.out.println(e.getX() + " " + e.getY());
if( currentPlayer ){
ImageIcon white = new ImageIcon("l/Images/white.jpg");
piece = new JLabel(white);
temp.add(piece);
}
else{
ImageIcon black = new ImageIcon( "/Images/black.jpg");
piece = new JLabel(black);
temp.add(piece);
}
currentPlayer = !currentPlayer;
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
}
}

Don't forget to revalidate and repaint if adding or removing components from a container. I've modified your SSCCE, and have gotten rid of the need to use images to make it runnable by folks who don't have access to your image files (like me!). Changes are noted by the // !! comments:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class Gui2 extends JFrame implements MouseListener {
private static final long serialVersionUID = -973341728129968945L;
JLayeredPane layeredPane;
JPanel board;
JLabel piece;
int numSquares;
private boolean currentPlayer;
// !!
private ImageIcon whiteIcon;
private ImageIcon blackIcon;
public Gui2() {
// !!
whiteIcon = createIcon(Color.white);
blackIcon = createIcon(Color.black);
Dimension boardSize = new Dimension(600, 600);
numSquares = 6;
currentPlayer = true;
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
board = new JPanel();
layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);
board.setLayout(new GridLayout(numSquares, numSquares));
board.setPreferredSize(boardSize);
board.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < (numSquares * numSquares); i++) {
// !! JPanel square = new JPanel(new BorderLayout());
JPanel square = new JPanel(new GridBagLayout()); // !!
square.setBorder(BorderFactory.createLineBorder(Color.black));
square.setBackground(Color.green);
square.setName(String.format("[%d, %d]", i % numSquares, i
/ numSquares)); // !!
board.add(square);
}
}
// !!
private ImageIcon createIcon(Color color) {
int width = 40;
int height = width;
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(color);
g2.fillOval(0, 0, width, height);
g2.dispose();
ImageIcon icon = new ImageIcon(img);
return icon;
}
public static void main(String[] args) {
JFrame frame = new Gui2();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
#Override
// !!
public void mousePressed(MouseEvent e) {
JPanel temp = (JPanel) board.findComponentAt(e.getX(), e.getY());
System.out.println(e.getX() + " " + e.getY());
System.out.println(temp.getName()); // !!
if (currentPlayer) {
// !! ImageIcon white = new ImageIcon("l/Images/white.jpg");
// !! piece = new JLabel(white);
piece = new JLabel(whiteIcon); // !!
temp.add(piece);
} else {
// !! ImageIcon black = new ImageIcon("/Images/black.jpg");
// !! piece = new JLabel(black);
piece = new JLabel(blackIcon); // !!
temp.add(piece);
}
temp.revalidate(); // !!
temp.repaint(); // !!
currentPlayer = !currentPlayer;
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
}
Also class names should be capitalized, and also you should again make your ImageIcons once. Again, one ImageIcon can be shared by many JLabels. You'll also want to respond to mousePressed not mouseClicked as mouseClicked can be fussy, especially if you move the mouse between press down and mouse release.
Hopefully you've also seen the value of an SSCCE. :)

Related

Dragging an image into the JButton in swing

I am working on Battleship project right now. I created two panels which i put in one frame. First panel is grid of buttons, and second panel is consist of draggable ship images. All I want, is when I drag the image into the certain button, it would appear on that button. In other words, i want just a simple addition of image into the JButton by dragging image into it.
Here is the full code:
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class BattleShip extends JFrame{
public BattleShip() throws IOException {
this.setResizable(false);
this.pack();
JPanel panelLeft = new JPanel();
JPanel panelRight = new JPanel();
panelLeft.setSize(500, 650);
panelRight.setSize(200,650);
panelRight.setBorder(BorderFactory.createLineBorder(Color.RED, 7));
panelRight.setBackground(Color.WHITE);
panelLeft.setLayout(new GridLayout(11,11));
panelRight.setLayout(null);
BufferedImage myPicture = ImageIO.read(new File("/home/hikmet/Desktop/595a7960d639a15d096a226d.png"));
BufferedImage[] resized = new BufferedImage[14];
for (int i = 0; i < 14; ++i) {
resized[i] = resize(myPicture, 20, 30);
}
JLabel[] img = new JLabel[14];
for (int i = 0; i < 14; ++i) {
img[i] = new JLabel((new ImageIcon(resized[i])));
}
Dimension size = img[0].getPreferredSize();
for (int i = 0; i < 14; ++i) {
panelRight.add(img[i]);
img[i].setBounds(7 + i * 50, 7, size.width, size.height);
}
JButton button[][] = new JButton[11][11];
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
button[i][j] = new JButton();
panelLeft.add(button[i][j]);
}
}
button[1][0].setText("A"); button[0][1].setText("1");
button[2][0].setText("B"); button[0][2].setText("2");
button[3][0].setText("C"); button[0][3].setText("3");
button[4][0].setText("D"); button[0][4].setText("4");
button[5][0].setText("E"); button[0][5].setText("5");
button[6][0].setText("F"); button[0][6].setText("6");
button[7][0].setText("G"); button[0][7].setText("7");
button[8][0].setText("H"); button[0][8].setText("8");
button[9][0].setText("I"); button[0][9].setText("9");
button[10][0].setText("J"); button[0][10].setText("10");
for (int i = 0; i < 14; i++) { //applying mouseEvent to each image
MouseHandler movement = new MouseHandler(img[i]);
}
this.setTitle("BattleShip");
this.setSize(700,700);
this.setLayout(new GridLayout(2,4));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().add(panelLeft);
this.getContentPane().add(panelRight);
}
//method for just resizing the size of image
private static BufferedImage resize(BufferedImage myPicture, int height, int width) {
Image tmp = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp,0,0,null);
g2d.dispose();
return resized;
}
//Class for handling mouseEvents
public class MouseHandler implements MouseMotionListener {
private int x, y;
public MouseHandler(JLabel img){
img.addMouseMotionListener(this);
}
#Override
public void mouseDragged(MouseEvent mouseEvent) {
mouseEvent.getComponent().setLocation((mouseEvent.getX()+mouseEvent.getComponent().getX())-x, (mouseEvent.getY()+mouseEvent.getComponent().getY())-y);
}
#Override
public void mouseMoved(MouseEvent mouseEvent) {
x = mouseEvent.getX();
y = mouseEvent.getY();
}
}
//Main class
public class Main {
public static void main(String[] args) throws IOException {
new BattleShip();
}
}
Hope someone will help me :)
You can specify the property to be dragged when you use the TransferHandler class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DragIcon extends JPanel
{
public DragIcon()
{
setLayout( new BorderLayout(40, 20) );
TransferHandler iconHandler = new TransferHandler( "icon" );
// TransferHandler iconHandler = new TransferHandler( "text" );
MouseListener dragListener = new DragMouseAdapter();
JLabel dragLabel = new JLabel("Drag");
dragLabel.setTransferHandler( iconHandler );
dragLabel.addMouseListener(dragListener);
dragLabel.setIcon( new ImageIcon("copy16.gif") );
dragLabel.setHorizontalAlignment(JLabel.CENTER);
add(dragLabel, BorderLayout.PAGE_START);
JLabel label = new JLabel("Label");
label.setTransferHandler( iconHandler );
add(label, BorderLayout.LINE_START);
JButton button = new JButton("Button");
button.setTransferHandler( iconHandler );
add(button, BorderLayout.LINE_END);
}
private class DragMouseAdapter extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
JComponent c = (JComponent)e.getSource();
TransferHandler handler = c.getTransferHandler();
handler.exportAsDrag(c, e, TransferHandler.COPY);
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Drag Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DragIcon());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

Simple paint program using JPanel shows random images of JButtons

I'm trying to make a simple paint program in Java. It has 3 colors and a JField to enter the thickness. It works, except every time I enter a button, an image of that button shows up in the JPanel that contains the drawing portion.
I know that I can set the paintPane JPanel to opaque, but it relies on drawing over it self for the painting to work - otherwise it just drags a point around the screen. Thanks!!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SimplePainting
{
//The main method simply creates a frame, and terminates the program
//once that frame is closed.
public static void main (String [] args)
{
PaintFrame frame = new PaintFrame();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
class PaintFrame extends JFrame implements ActionListener
{
JPanel pane;
PaintPane drawPane;
Color paintColor = Color.black;
private int radius = 5;
//holds the thickness of the line
JTextField thick;
public PaintFrame ()
{
//We use the JFrame consturctor to add a title to the frame
super("Windows Paint");
//set the main content pane
pane = (JPanel)getContentPane();
pane.setLayout(new BorderLayout());
//make a pane to hold the drawing
drawPane = new PaintPane();
drawPane.addMouseMotionListener(drawPane);
//Make a JPanle to hold all of the buttons
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(1,6));
//add the buttons
JButton black = new JButton("Black");
buttonPane.add(black);
JButton red = new JButton("Red");
buttonPane.add(red);
JButton green = new JButton("Green");
buttonPane.add(green);
//Make a field to re-enter the thickness
thick = new JTextField(3);
thick.setText("5");
JButton thickness = new JButton("Reset Thickness");
thickness.addActionListener(this);
buttonPane.add(thickness);
buttonPane.add(thick);
JButton reset = new JButton("New Drawing");
reset.addActionListener(this);
buttonPane.add(reset);
black.addActionListener(this);
red.addActionListener(this);
green.addActionListener(this);
pane.add(drawPane, BorderLayout.CENTER);
pane.add(buttonPane, BorderLayout.SOUTH);
setSize(500,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Black"))
paintColor = Color.black;
else if (e.getActionCommand().equals("Red"))
paintColor = Color.red;
else if (e.getActionCommand().equals("Green"))
paintColor = Color.green;
else if (e.getActionCommand().equals("Reset Thickness"))
{
if (thick.getText() != "")
{
int lineThickness = Integer.parseInt(thick.getText());
radius = lineThickness;
}
}
else if (e.getActionCommand().equals("New Drawing"))
{
drawPane.startPaint = false;
drawPane.repaint();
}
}
class PaintPane extends JPanel implements MouseMotionListener
{
private int x;
private int y;
// don't paint a point until mouse is dragged
boolean startPaint = false;
public PaintPane()
{
setBackground(Color.white);
}
//paints a circle centered at x,y
public void paint(Graphics g)
{
//recall that the frist (x,y) coordiantes represent the top left
//corner of a box holding the circle
g.setColor(paintColor);
if (startPaint)
g.fillOval(x-radius,y-radius, 2*radius, 2*radius);
else
super.paintComponent(g);
}
public void mouseDragged(MouseEvent e)
{
startPaint = true;
x = e.getX();
y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
}

Updating background image of panel

I am having trouble updating my "panel" when I want to change it's background.
In my program, whenever you reach a certain number of clicks in that panel, the panel will change it's background but I have trouble doing so.
here are my codes.
this is the constructor/gui class
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;;
public class demo {
JFrame frame = new JFrame ("Idle Game Test!");
JPanel backGroundPanel = new JPanel ();
static JPanel statusPanel = new JPanel();
JPanel buttonPanel = new JPanel ();
JPanel bigPanel = new JPanel ();
static JTextArea message = new JTextArea (34,43);
//MessageDisplay msg = new MessageDisplay ();
//Constructor
demo () {
//gets the dimension of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
backGroundClass bgc = new backGroundClass ();
frame.setSize (850,700);
frame.setResizable (false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
//get size in pixels
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width - w)/2;
int y = (dim.height - h)/2;
//set the location
frame.setLocation (x,y);
backGroundPanel.setLayout (null);
backGroundPanel.setBackground (Color.DARK_GRAY);
frame.add (backGroundPanel);
statusPanel.setSize(250, 600);
statusPanel.setLocation (15,55);
statusPanel.add(bgc.panel);
statusPanel.addMouseListener(new mouseEvent ());
backGroundPanel.add (statusPanel);
buttonPanel.setSize (550,100);
buttonPanel.setLocation (280,555);
backGroundPanel.add (buttonPanel);
JScrollPane scroll = new JScrollPane (message, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
DefaultCaret caret = (DefaultCaret)message.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
message.setWrapStyleWord(true);
message.setLineWrap(true);
message.setBackground (Color.gray);
message.setEditable (true);
message.setForeground(Color.white);
message.setFont(new Font ("Georgia", Font.PLAIN, 12));
message.setEditable(false);
//message.setSize (500,500);
bigPanel.add(scroll);
bigPanel.setSize (550, 490);
bigPanel.setLocation (280,55);
backGroundPanel.add (bigPanel);
}
}
The class that makes a panel w/background
import java.awt.*;
import javax.swing.*;
#SuppressWarnings("serial")
class backGroundClass extends JPanel {
//ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
int controlNumber = 0 ;
String []imagePath= {"Image1Ulquiora.jpg","Image2Diva.jpg","Image2DivaSized.jpg","Image3GirlInSword.jpg"};
ImageIcon icon = new ImageIcon (getClass().getResource(imagePath[controlNumber]));
ImagePanel panel = new ImagePanel (icon.getImage());
}
#SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
and the action event that suppose to update the panel (im not sure if this is right or what XD)
import java.awt.event.*;
public class mouseEvent implements MouseListener {
backGroundClass bgc = new backGroundClass ();
#Override
public void mouseClicked(MouseEvent e) {
if (!MainProg.flag){
demo.message.append(Integer.toString(e.getClickCount()));
}
if (e.getClickCount() > 5) {
bgc.controlNumber = 3;
bgc.panel.repaint(); //this does not work :<
demo.statusPanel.revalidate(); //this also
demo.statusPanel.repaint(); //same
}
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
I tried the repaint both on the panel that created w/background image AND the panel holds the panel that creates the image (redundant XD) but unfortunately none of them works.
also is there other way to access those static variables? the way i implemented them works but i do think there are other ways that are more suitable or better but i cant figure out.
I hope someone could help me thanks in advance and more power!

Set cursor for jTabbedPane's tab in java

I have created a custom jTabbedPane class which extends BasicTabbedPaneUI and have successfully created my desired jTabbedPane but now the problem is that how can I set Hand cursor for each tab in my custom jTabbedPane?
I tried to set cursor with this
tabbedPane.setUI(new CustomMainMenuTabs());
tabbedPane.setCursor(new Cursor((Cursor.HAND_CURSOR)));
this sets the cursor for whole of jTabbedPane but I want to set the cursor when mouse hovers over any of tab in it only.
How can I set Hand cursor for tabs in my 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 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 cursor to HAND_CURSOR cursor when mouse hovers over any tab only not jpanel or any other component. It would be great if done without a mouse listener.
I see a lot of answers here that are WAY too complicated (custom UIs, extra listeners, Graphics stuff, etc.).
Basically, camickr spelled it out for you. Here's 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);
}
}
}
I want to set the cursor when mouse moves over any of tab in it.
I would guess you need to add a MouseMotionListener to the tabbed pane. Then when the mouseMoved(...) event is generated you check if the mouse is over a tab.
You should be able to use the tabForCoordinate(...) method of the BasicTabbePaneUI to determine if the mouse is over a tab or not.
Steps:
Create a MouseMotionListener and add it to your JTabbedPane
Inside the listener -> mouseMoved method, chec kif the current position of the mouse is inside the bounds of your tabs
If true, then change the cursor to a hand cursor
else show the default cursor
1.Method to check if the mouse is within the bounds of the tabs:
private static int findTabPaneIndex(Point p, JTabbedPane tabbedPane) {
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
if (tabbedPane.getBoundsAt(i).contains(p.x, p.y)) {
return i;
}
}
return -1;
}
2.The 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 the listener to the JTabbedPane:
jtpane.addMouseMotionListener(listener);
Related Documentation:
MouseMotionListener
How to Write a Mouse-Motion Listener
The final code:
Putting all the peices 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(p.x, p.y)) {
return i;
}
}
return -1;
}
}
You can use:
public void setTabComponentAt(int index,
Component component)
And then you do
component.addMouseListener(yourListener)
I have changed main menthod according to your need that Hand cursor will be visible only on tab header . check if it solve your problem
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);
}
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're already extending BasicTabbedPaneUI you can simply extend the mechanics for painting the rollover tab, which is already implemented there without the need of using more listeners or calculating coordinates yourself.
The rolling over is a mechanic that has been present in the component since Java 5 and this is a proper extension, just need to override and extend the method. This method is called whenever the mouse moves in the tab component (it affects the tab area but does not affect the children) and and it's kept updated.
I've tried your code snippet with this addition and worked fine.
It's actually a lot easier than installing a custom UI delegate.
You can install your own labels as the tab components (the components inside the tab handles), which will have their own cursors. Following is a simple example with 3 tabs, and a different cursor for the body of the tabbed pane and each of the tabs:
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);
}
});
}
}

How to draw shape by right clicking a panel in Swing?

I am trying to create a swing program. In my program I want to achieve something like this: right click on a panel and select the menu "Draw rectangle" and program should draw a very simple rectangle on the panel. Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class MainWindow extends JFrame {
JFrame frame = null;
AboutDialog aboutDialog = null;
JLabel statusLabel = null; //label on statusPanel
public MainWindow() {
frame = new JFrame("Project");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//MENUS
JMenuBar menuBar = new JMenuBar(); //menubar
JMenu menuDosya = new JMenu("Dosya"); //menus on menubar
JMenu menuYardim = new JMenu("Yardım"); //menus in menus
menuBar.add(menuDosya);
menuBar.add(menuYardim);
JMenuItem menuItemCikis = new JMenuItem("Çıkış", KeyEvent.VK_Q); //dosya menus
menuItemCikis.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
menuDosya.add(menuItemCikis);
JMenuItem menuItemYardim = new JMenuItem("Hakkında", KeyEvent.VK_H); //hakkinda menus
menuItemYardim.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JDialog f = new AboutDialog(new JFrame());
f.show();
}
});
menuYardim.add(menuItemYardim);
frame.setJMenuBar(menuBar);
//TOOLBAR
JToolBar toolbar = new JToolBar();
JButton exitButton = new JButton("Kapat");
toolbar.add(exitButton);
//STATUSBAR
JPanel statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
frame.add(statusPanel, BorderLayout.SOUTH);
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 20));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
statusLabel = new JLabel("Ready.");
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(statusLabel);
//MAIN CONTENT OF THE PROGRAM
final JPanel mainContentPanel = new JPanel();
//RIGHT CLICK MENU
final JPopupMenu menuSag = new JPopupMenu("RightClickMenu");
JMenuItem menuRightClickRectangle = new JMenuItem("draw rectangle");
menuRightClickRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//CircleShape cs=new CircleShape();
mainContentPanel.add(new CircleShape()); //trying to draw.
mainContentPanel.repaint();
//mainContentPanel.repaint(); boyle olacak.
}
});
JMenuItem menuRightClickCircle = new JMenuItem("Daire çiz");
menuSag.add(menuRightClickRectangle);
menuSag.add(menuRightClickCircle);
mainContentPanel.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
menuSag.show(e.getComponent(), e.getX(), e.getY());
statusLabel.setText("X=" + e.getX() + " " + "Y=" + e.getY());
}
}
});
JButton west = new JButton("West");
JButton center = new JButton("Center");
JPanel content = new JPanel(); //framein icindeki genel panel. en genel panel bu.
content.setLayout(new BorderLayout());
content.add(toolbar, BorderLayout.NORTH);
content.add(statusPanel, BorderLayout.SOUTH);
content.add(west, BorderLayout.WEST);
content.add(mainContentPanel, BorderLayout.CENTER);
frame.setContentPane(content);
frame.setPreferredSize(new Dimension(400, 300));
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
The problem is nothing is drawn on the panel. I guess there is an event loss in the program but i don't know how to solve this issue.
Please modify the code in the following way. Add a new class like this:
class MainPanel extends JPanel {
private List<Rectangle> rectangles = new ArrayList<Rectangle>();
private void addRectangle(Rectangle rectangle) {
rectangles.add(rectangle);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Rectangle rectangle : rectangles) {
g2.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
}
Then, instead of
final JPanel mainContentPanel = new JPanel();
you should do:
final MainPanel mainContentPanel = new MainPanel();
And the action listener for the menu item becomes something like this:
menuRightClickRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// TODO: add your own logic here, currently a hardcoded rectangle
mainContentPanel.addRectangle(new Rectangle(10, 10, 100, 50));
mainContentPanel.repaint();
}
});
You can't do that by calling add method.To draw a shape you will have to override paintComponent method:
Example of drawing rectangle:
public void paintComponent(Graphics g){
g.setColor(Color.RED);
g.fillRect(50,50,50,50);
}
Hmm did a short example for you:
Test.java:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class Test {
private final JFrame frame = new JFrame();
private final MyPanel panel = new MyPanel();
private void createAndShowUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test().createAndShowUI();
}
});
}
}
MyPanel.java:
class MyPanel extends JPanel {
private final JPopupMenu popupMenu = new JPopupMenu();
private final JMenuItem drawRectJMenu = new JMenuItem("Draw Rectangle here");
private int x = 0, y = 0;
private List<Rectangle> recs = new ArrayList<>();
public MyPanel() {
initComponents();
}
private void initComponents() {
setBounds(0, 0, 600, 600);
setPreferredSize(new Dimension(600, 600));
popupMenu.add(drawRectJMenu);
add(popupMenu);
addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
checkForTriggerEvent(e);
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
private void checkForTriggerEvent(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
x = e.getX();
y = e.getY();
popupMenu.show(e.getComponent(), x,y);
}
}
});
drawRectJMenu.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addRec(new Rectangle(x, y, 100, 100));
repaint();
}
});
}
public void addRec(Rectangle rec) {
recs.add(rec);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Rectangle rec : recs) {
g2d.drawRect(rec.x, rec.y, rec.width, rec.height);
}
}
}

Categories