Drawing Swing components inside a Graphics object - java

is there any way I can make a Java Graphics object (g) draw a swing component?
like a button or JLabel
something like g.drawJLabel ?
I'm trying to add a clickable text (Link) inside my graphic object but it's not possible to add a mouse listener to the graphics objects

Your clickable text can be an JLabel with an mouselistener
JLabel hyperlink=new JLabel("Click me");
hyperlink.addMouseListener(new MouseAdapter()
{
#Override
public void mouseEntered(MouseEvent m)
{
hyperlink.setForeground(Color.blue);
}
#Override
public void mouseExited(MouseEvent m)
{
hyperlink.setForeground(Color.black);
}
#Override
public void mouseClicked(MouseEvent m)
{
if (Desktop.isDesktopSupported() &&
Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI(hyperlink.getText())));
}
}
Make sure that your graphical object has some form of layout manager otherwise your text won't show up
Then inside your graphical objects which I assume all of them extends some form of JComponent you can add this text to your object and your text will still receive user input

Related

Right click focus in Swing

Today in my Swing app when I left click, the text area gets focused. When I do a right click on any other text field,the popup menu appears but that text area is not focused. The focus remains on the field left clicked on before. How do I remove the focus from the previous field and make it appear on the field that is right clicked upon ?
if (e.isPopupTrigger()) {
ContextMenu menu = new ContextMenu();
menu.show(tree, e.getX(), e.getY());
}
Doing this on mouseRelease.
Assuming you meant 'focus' instead of 'highlight':
Create a subclass of JTextField, add a mouse listener and force the text field to request focus whenever the mouse button is pressed, released or clicked (three versions to make sure it works on every platform).
import javax.swing.JTextField;
public class TextFieldRClick extends JTextField {
public TextFieldRClick() {
super();
createMouseListener();
}
public TextFieldRClick(int cols) {
super(cols);
createMouseListener();
}
private void createMouseListener() {
this.addMouseListener(new java.awt.event.MouseAdapter() {
#Override
public void mousePressed(java.awt.event.MouseEvent evt) {
requestFocusInWindow();
}
#Override
public void mouseReleased(java.awt.event.MouseEvent evt) {
requestFocusInWindow();
}
#Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
requestFocusInWindow();
}
});
}
}
You can then create text fields as directly 'new TextFieldRClick()' giving them the ability to get the focus when the user right-clicks them.
I had the same problem, this solved it for me.
EDIT: Changed requestFocus() to requestFocusInWindow(). requestFocus() is said to be platform dependent and should no longer be used.

How to create clickable textcontainer

I have 16 JTextarea's placed on my panel (in gridlayout). I didn't use the design tools netbeans for this. The code generates them for me:
for (int i = 0; i < 16; i++) {
JTextArea vak = maakVak(gridLayoutPanel); //make new JTextArea and add them to gridlayout.
tekstvakken.add(vak); //save Jtextarea to ArrayList.
}
This is the method for generating a new JTextArea and adding them to the GUI.
public JTextArea maakVak(JPanel p) {
JTextArea vak = new JTextArea(80, 120);
vak.setEditable(false);
p.add(vak);
return vak; //return JTextarea to save in the ArrayList
}
I have an ArrayList that contains objects from a class that I made for the software. The Objects contain multiple Strings. I need to "print" all the strings from one object to one JTextArea, and I do that for the first 16 objects in my ArrayList (hence I've only 16 JTextArea's).
This works fine, I have 16 JTextArea's on my GUI and they contain the correct Strings.
Now I want to add more functionality to my software, and I need in some way to make these JTextArea's clickable (when this event occurs, a screen should pop-up where I can change/delete the object).
How is this possible (with minor changes)?
JTextArea is a java.awt.Component, so it has access to the addMouseListener method.
textArea.addMouseListener(new MouseListener(MouseEvent e) {
//implemented methods go here
});
This will create an anonymous inner class which implements MouseListener, and I found it to be the simplest way to do this.
Otherwise, you can just have your class implement MouseListener. This accomplishes the same thing as you still have to override the methods, but it affects the whole class rather than an anonymous class that is only used once.
If the Strings that will be placed in the JTextAreas aren't very lengthy, I would suggest using JButtons instead, so you could use the addActionListener method instead of having to also override four other methods that you won't use.
add a mouseListener to JTextArea in maakVak
public JTextArea maakVak(JPanel p) {
JTextArea vak = new JTextArea(80, 120);
vak.setEditable(false);
vak.addMouseListener(textAreaMouseListener());
p.add(vak);
return vak; //return JTextarea to save in the ArrayList
}
private MouseListener textAreaMouseListener() {
return new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
JTextArea vak = (JTextArea) e.getComponent();
//display popup to make changes
}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
};
}

Mouse over events with JButton

I'm trying to create a custom mouse over event on a JButton. The reason being that my JButton is currently an image, so I had to remove all the borders and animations and what not. so I did this:
btnSinglePlayer.setOpaque(false);
btnSinglePlayer.setContentAreaFilled(false);
btnSinglePlayer.setBorderPainted(false);
And that works perfect to only display the image, and the button does in fact work. I want to know if there's any pre-built methods perhaps that can do this, or how I would go about learning to do what I want.
More specifically, what I want the image to do when I mouse over is for it to get just a bit bigger.
I have tried these so far, and did nothing:
btnSinglePlayer.setRolloverIcon(singlePlayerButton);
btnSinglePlayer.setPressedIcon(singlePlayerButton);
for Icon to use implemented methods in API
you can to use ButtonModel with ChangeListener
(by default) for JButtons JComponents there no reason to use Mouse(Xxx)Listener or its MouseEvent, all those events are implemented and correctly
As an alternative You can achieve this by registering MouseListener to the JButton and override mouseEntered() ,mouseExited() , mousePressed() and mouseReleased() method.For Example:
final ImageIcon icon1 = new ImageIcon("tray.gif");
final JButton button = new JButton(icon1);
final int width = icon1.getIconWidth();
final int height = icon1.getIconHeight();
button.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width + 10, height,Image.SCALE_SMOOTH)));
//button.setIcon(icon1);
}
public void mouseExited(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width , height,Image.SCALE_SMOOTH)));
}
public void mousePressed(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width + 5, height,Image.SCALE_SMOOTH)));
}
public void mouseReleased(MouseEvent evt)
{
icon1.setImage((icon1.getImage().getScaledInstance(width + 10, height,Image.SCALE_SMOOTH)));
}
});
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

Undecorated button: JButton or JLabel with MouseListener?

I want to implement a button that does not show borders or anything else, except for an image that changes when you hover over it. Clicking on the image (showing the hover image) will execute some code.
I also would like to put all this in a separate class so I have a reusable component.
Extending a JButton delivers me the methods addActionListener() and so forth. But using the setAction() method removes the images that I set in the constructor. So it's not watertight, as I cannot use an Action in combination with this class. And I certainly do not want to override method like setAction().
public class JHoverLabel extends JButton {
private final Icon normal;
private final Icon hovered;
public JHoverLabel (Icon normal, Icon hovered) {
this.normal = normal;
this.hovered = hovered;
setIcon(normal);
setFocusPainted(false);
setMargin(new Insets(0, 0, 0, 0));
setContentAreaFilled(false);
setBorderPainted(false);
setOpaque(false);
addMouseListener(new HoverListener());
}
private class HoverListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {
setIcon(hovered);
}
#Override
public void mouseExited(MouseEvent e) {
setIcon(normal);
}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
}
}
Extending a JLabel seems to do what a want in combination with a MouseListener, but I feel like using the wrong component here, because 'click-on-me-to-do-something' basically leads me to a JButton.
So what should I use? A JLabel or a JButton?
use implemented methods for JButton.setXxxIcon
button.setRolloverIcon((Icon));
button.setPressedIcon(Icon);
button.setDisabledIcon(Icon);
instead of MouseListener you can implements ButtonModel
So what should I use? A JLabel or a JButton?
Use a JButton. Your mouse challenged users will thank you.

how to change the color of text of tab on mouse over of JtabbedPane?

I need to change the color of text of tab of JtabbedPane on MouseOver.
Is is possible using the Mouse Listener or is there any different property to do that?
Thanks
Jyoti
There is not a built-in property or method to do this.
One option is to create a custom JLabel (or other component) add a MouseListener that would change the color on mouse entry/exit.
Example, something like this:
class CustomMouseOverJLabel extends JLabel{
public CustomMouseOverJLabel(String text) {
super(text);
addMouseListener(new MouseAdapter(){
#Override
public void mouseEntered(MouseEvent e) {
setForeground(Color.BLUE);
}
#Override
public void mouseExited(MouseEvent e) {
setForeground(Color.RED);
}
});
}
}
Then when you make a call to addTab(title, item), also set custom title components like so:
yourTabbedPane.setTabComponentAt(index, new CustomMouseOverJLabel("title"));
The tabbed pane tutorial has an example of tabs with custom components that might help.

Categories