Creating a JToolTip-like Component - java

I'm writing a custom component that displays some bioinformatics data, and I'd like to be able to show additional information about the location the mouse is at when the user holds down a certain key. This seems like an obvious job for a tooltip, but there are a few problems that seem to be preventing this from working. First, I want to have the tooltip follow the mouse and change its text dynamically. This works somewhat by overriding getToolTipText and getToolTipLocation for the component, but the tooltip flickers as the mouse position is updated and doesn't display over the sub-components (it's a JPanel with some JTextPanes inside it). I also don't think there's any way to make it display immediately without a call to the ToolTipManager, which I believe would change the delay for all other components.
It looks like there are workarounds for some of these problems, but they're rather complicated and inelegant so I'm thinking a good solution would be to just create my own component, fill it with the relevant information and show it myself. However, this needs to be some kind of top-level component because it needs to be able to extend slightly beyond the borders of the parent component or even the containing JFrame and be drawn over everything else. The only objects I know of that have this functionality outside of JToolTip are JFrame and JDialog, which have borders with titles and close buttons which I don't want. Is there some way to accomplish this?

One option is to use a glass pane. In this case your tooltip won't be able to go outside of the frame, but you can easily position it relative to how near it is to a side of the frame. Some example code that draws a bubble (which you can fill with text in the paint method) that follows the mouse.
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(new Dimension(500, 500));
JPanel glassPane = new JPanel();
glassPane.setOpaque(false);
glassPane.setLayout(null);
frame.setGlassPane(glassPane);
frame.getGlassPane().setVisible(true);
final MyInfoBubble mib = new MyInfoBubble();
mib.setBounds(10, 30, 100, 50);
((JPanel)frame.getGlassPane()).add(mib);
frame.getContentPane().addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent me) {
mib.setBounds(me.getPoint().x, me.getPoint().y, 100, 50);
}
});
((JPanel)frame.getGlassPane()).validate();
((JPanel)frame.getGlassPane()).repaint();
frame.setVisible(true);
}
static class MyInfoBubble extends JPanel
{
public MyInfoBubble()
{
setVisible(true);
}
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLUE);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
}
}

Related

Make JScrollPanel dynamically resizable with JPanel drawing

I have a JScrollPanel and a JPanel added to it. I would like to draw to the JPanel and make the scrollbars of the JScrollPane appear whenever the drawing exceeds the size of the panel and be able to scroll the drawing both vertically and horizontally.
I have tried consulting with various forums and the official docs and tried a few things (setting the borders, the preferred size, etc.) but none seems to yield the desired effects.
I have a JFrame (with GridBagLayout, btw.) :
JFrame frame1 = new JFrame("Application");
frame1.setVisible(true);
frame1.setMinimumSize(new Dimension(580,620));
frame1.setResizable(false);
frame1.setLocationRelativeTo(null);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The relevant components are :
JPanel panel1 = new JPanel();
JScrollPane scrollPane = new JScrollPane(panel1);
frame1.add(scrollPane, gbc_panel1); //added with layout constraints
JPanel :
panel1.setBackground(Color.BLACK);
panel1.setPreferredSize(new Dimension(500,500));
panel1.setMinimumSize(new Dimension(360,360));
panel1.setMaximumSize(new Dimension(1000,1000));
JScrollPane :
scrollPane.setAutoscrolls(true);
The relevant code from the action event
of a button that does the drawing :
Graphics g;
g = panel1.getGraphics();
panel1.paint(g);
g.setColor(new Color(0,128,0));
/* this is followed by some more code that
does the drawing of a maze with g.drawLine() methods */
The code does the drawing perfectly, I just can't seem to figure it out how to make the scrolling and dynamic resizing happen.
I would appreciate any helpful comments or remarks!
Thank you!
Ultimately rewriting the paint method did the trick as #MadProgrammer suggested. I was just hoping that I could do the painting without having to define my custom JPanel class, but looks like it doesn't work that way.
The custom class looks like this:
class Drawing extends JPanel {
int mazeSize;
public Drawing(JTextField jtf)
{
try {
this.mazeSize = Integer.parseInt(jtf.getText());
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, "ERROR! Invalid size value!");
}
} // the constructor gets the size of the drawing from a textField
public Dimension getPreferredSize() {
return new Dimension(mazeSize*10,mazeSize*10);
} //getPreferredSize - this method is used by the scroll pane to adjust its own size automatically
public void drawMaze (Graphics g)
{
/* some irrelevant code that does the desired drawing to the panel by calling g.drawLine()*/
} // drawMaze method that does the de facto drawing
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawMaze(g);
}// paintComponent() #Override method - this was the tricky part
}//Drawing JPanel subclass
It is also worth noting (if some noob like myself happens to stumble upon this question), that after instantiating the new JPanel subclass in the action event, I had to add it to the JScrollPanel in the following way, instead of just simply using its add() method:
Drawing drawPanel = new Drawing(textfield1);
scrollPane.getViewport().add(drawPanel);
Again, thanks for the suggestion!
Once finished with the program (a random maze generator that uses a recursive backtracking algorithm), I will make the source code available at my github profile.

Why do my components appear next to each other?

I'm currently trying to create a program that moves a rectangle over a background Image with keyboard keys. The problem I'm facing is that when I draw the components they are simply placed next to each other, instead of the square overlaying the background image. Here's the code to display both the components;
JLayeredPane panel = new JLayeredPane();
panel.setLayout(new FlowLayout());
add(panel);
paintBackground pb = new paintBackground(bimg);
panel.add(pb, 1, 0);
paintPlayer cc = new paintPlayer(startX, startY);
panel.add(cc, 2, 0);
pack();
setVisible(true);
I believe the problem is that the paintPlayer component is set to full size, and there seems to be a background. The paintPlayer component code looks like this:
public Dimension getMinimumSize() {
return new Dimension(800,600);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(800,600);
}
#Override
public Dimension getMaximumSize() {
return new Dimension(800,600);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
System.out.println(startX + startY );
g.fillRect(startX, startY, 30, 30);
}
I've had a go at setting the component size to just the size of the rectangle, but that way I can't move the rectangle by using the first two values in fillRect. The background for the rest of the space filled by the component (800x600) seems to be opaque.
When added, the components just display next to each other, like this: https://gyazo.com/57245c518e02778c36ffc89ba75d5a81. How do I go about adding the paintPlayer ontop of the paintBackground, so that it only covers the rectangle on the background Image.
I've done a fair bit of searching but I can't seem to work it out. Perhaps something to do with the layout? One other thing I've noticed is that by doing this, neither the frame or the pane benefit from a setBackground, as it's not visible.
Cheers for any help.
This is the default Constructor of JLayerdPane.
public JLayeredPane() {
setLayout(null);
}
You see it uses normaly AbsolutLayout. And if you read here:
Note: that these layers are simply a logical construct and LayoutManagers will affect all child components of this container without regard for layer settings.
You should understand what is wrong. Check OverlapLayout.

Graphics are blocked out when I add a JPanel to the frame (java)

I have some Graphics2D graphics displayed in my frame, and then I added a JPanel to the entire frame so that I can add a mouselistener to clicks anywhere in the panel. However, the graphics disappear, I assume blocked out by the frame. I tried setting the panel to visible false, but that didnt do anything. How can I keep my mouselistener listening on the entire window, and still display my graphics?
EDIT:
Heres a bit of code: EDIT:(and some more)
//adding the panel and mouselistener
JPanel allPanel = new JPanel();
allPanel.addMouseListener(this);
frame.add(allPanel);
//...
//drawing some of the graphics
public void draw() {
frame.add(new CustomPaintComponent());
// Display the frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
JPanel allPanel = new JPanel();
allPanel.addMouseListener(this);
frame.add(allPanel);
}
static class CustomPaintComponent extends Component {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.fillRoundRect(10, 10, 50, 50, 10,10);
//...
Three problems jump out...
First, JFrame uses a BorderLayout as its default layout manager, this means that only one component can occupy any of its available five positions. By using add(Component) you add each component to the CENTRE position, overriding the first component you added to it...
Second, JPanel is opaque by default, meaning, even if you did get both components to occupy the same space, the top component would block the lower one
Third, you should be using paintComponent instead of paint
Take a look at Performing Custom Painting for more details
The solution could be to add the MouseListener directly to the custom component
Another choice would be to use a BorderLayout on the lower component, make the top component transparent (using setOpaque(false)) and add it to the lower component directly...
I believe your problem is that JFrame can have only one component added to it (by default). You add a CustomPaintComponent, which paints your graphics. Then, you add the JPanel, which automatically removes the CustomPaintComponent
Are you trying to paint the custom drawing on top of the JPanel? If that is the case, simply move that code over to the JPanel (but instead of using a CustomPaintComponent, put it in the paintComponent(Graphics g) method of the JPanel)

Drawing a selection box using Swing

I have written an application with a panel and three buttons. I want to add selection this buttons using the mouse. I mean like we have in Windows on the Desktop. I press the left mouse button and with the movement of the mouse the area selection is growing.
Is there a specific interface in this or do I have it manually call the appropriate methods for event listeners and there draw transparent rectangle? Here is a picture:
So I have a problem when I paint rectangle using event mouse-dragged, button is repainting so user see blinking button. I want to this button don't disapear when I paint rectangle. I think that I need to use glassPane. This is my conception. I have a frame. In frame I add panel with button and I need another panel where I will paint transparent rectangle. I am thinking then my button will not be still repainting. What do you think about this conception. Or maybe someone have another idea. This is code:
#Override
public void mousePressed(MouseEvent e) {
startPoint=e.getPoint();
setOpaque(true);
Graphics2D g2 = (Graphics2D)getGraphics();
Rectangle2D prostokat = new Rectangle2D.Double();
prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
g2.setComposite(AlphaComposite.getInstance(rule, alpha));
g2.draw(prostokat);
g2.setColor(Color.BLUE);
g2.fill(prostokat);
}
#Override
public void mouseDragged(MouseEvent e) {
setOpaque(true);
Graphics2D g2 = (Graphics2D)getGraphics();
Rectangle2D prostokat = new Rectangle2D.Double();
prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
g2.setComposite(AlphaComposite.getInstance(rule, alpha));
g2.draw(prostokat);
g2.setColor(Color.BLUE);
g2.fill(prostokat);
paintComponent(g2);
}
int rule = AlphaComposite.SRC_OVER;
float alpha = 0.85F;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
zaznacz rys = new zaznacz();
JFrame frame = new JFrame();
JButton Button = new JButton("1");
JPanel panel = new JPanel();
panel.add(Button);
rys.add(panel);
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setOpaque(false);
frame.add(rys);
}
});
}
}
I know that code is no perfect but almost work. I have a little problem. When I press the mousebutton and dragging my button disapear.
I don't need advise like "your code is wrong". I know that and I want to somebody help me what I must correct. I know that I shouldn't use paintComponent() in mouseEvents but only that way I can paint transparent rectangle. Or maybe you can othet idea how I can draw transparent rectangle. I try and try and I think that i must change mouseDragged method. because when I delete code from this method and only draw rectangle over a button all is ok. But problem is when I need draw rectangle by dragging mouse. I should change paint but I don't have idea how. Anyone can help me or try help me?
I think that that code doesn't works in this forms(main), maybe someone will debug that for you, please follows with Joey's advices
hmmm very offensive words, anyway follows (Oracle Java tutorial) http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

Painting a custom JButton and a text line

I'm designing an optimization system for public transport in a big city. So I have a map with some points on it, but don't care about it)
All I need is: my own JButton, which looks like a color-filled circle and a small text tag near it. I got some problems while overriding the paintComponent() method.. the round button is painted correctly, but not the text.
BUT, when i'm resizing the window manually, the text appears for a second, then it gets repainted again and dissapears.
Hope you guys understood my needs, thanks for help ;)
import java.awt.*;
import javax.swing.*;
public class JRoundButton extends JButton {
String label;
Color color;
int x,y;
public JRoundButton(Color color,int x,int y,String str)
{
label=str;
this.x=x;
this.y=y;
this.color=color;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension size = getPreferredSize();
setPreferredSize(size);
this.setBounds(0, 0, 10, 10);
setContentAreaFilled(false);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawChars(label.toCharArray(), 0, label.length(), 12,12);
g.fillOval(0,0,8,8);
}
public void paintBorder(Graphics g)
{
g.setColor(Color.white);
g.drawOval(0,0, 9, 9);
}
public static void main(String[] args)
{
JButton button = new JRoundButton(Color.GRAY,150,150,"Times Square");
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.black);
frame.setSize(300, 300);
frame.setVisible(true);
frame.add(button);
}
}
Seems that the call to 'setBounds( 0, 0, 10, 10 )' sets a component footprint that is too small to accomodate the text string. Extending the bounds to 100px wide and bringing down the point size to 6 looks to work okay.
1) NEVER set properties of the button in the paintComponent() method.
Dimension size = getPreferredSize();
setPreferredSize(size);
this.setBounds(0, 0, 10, 10);
setContentAreaFilled(false);
Get rid of the above code.
2) Dont set the Font of the Graphics object in the paintComponent() method. Thats what the setFont(...) method is used for.
3) There is no need to do any custom painting. If you want a circle, then add an Icon to the JLabel.
4) Don't override the paintBorder() method. If you want a Border then create a custom border and add it to the button using the setBorder() method.
In short there is no need to extend the button. Get rid of your JRoundButton class. Your code should simply look something like:
JButton = new JButton("Times Square");
button.setFont( new Font("Arial",Font.BOLD,14) );
button.setIcon( new OvalIcon(Color.WHITE, iconSize) );
Of course you will need to create an OvalIcon class but that is easy to implement since there are only three methods and you already know what the painting code should be.
I'd just cheat and use a unicode circle in the JButton's text. E.g.:
import javax.swing.*;
JFrame frame = new JFrame();
frame.getContentPane().add(new JButton("<html><font size='+10' color='red'>●</font> I'm next to a red circle!</html>"));
frame.pack();
frame.show();

Categories