So I have big panel with: setLayout(null). Then I want to add button to it. In big panel's constructor I add such code:
JButton button = new JButton("eeee");
button.setBounds(100, 100, 100, 50);
add(button);
And it's okay. But when I repaint big panel button hides. It appears again when I move mouse to place where it should be... How to prevent this strange behaviour? Maybe its connected with the fact I add button in constrcutor?
you need to let the paint panel components first with "super.paint (g)" if you do not it does not draw the components added
#Override
public void paint(Graphics g){
super.paint(g); //draw your button as default
// hear, you can draw others things
}
Related
I have no idea what the heck is going on while I'm trying to render an image on a JPanel in a JFrame. In fact I just want an image displayed in a fixed position on my JFrame.To achieve this I have a JPanel of a fixed size (min, max, pref size set) on my JFrame. On press of a button, I add another panel to mentioned panel, having the same size. The "child" panel has an overriden paint method to draw an image. So far so good, once I press the button and add that child panel, nothing happens at all. If I then click on the empty panel, the image is drawn as it should be, not overflowing the bounds of the child panel. However, all other components width increases drastically, stretching them out of the JFrames bounds. One would think that at least a scrollbar would appear, but no, the components are then out of reach. I'm attaching two screenshots displaying that very logic behavior.
The code is as follows:
void setPoster(BufferedImage poster) {
ImagePanel ip = new ImagePanel(poster);
ip.setSize(new Dimension(222, 327));
panelPoster.add(ip);
ip.setSize(new Dimension(222, 327));
}
Inside ImagePanel:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
System.out.println("Paiting imagepanel with size " + getSize());
}
Before screenshot:
After screenshot:
Could anyone enlighten me on what could possibly occur here?
I fixed the strange resizing by setting the layout to null on both the JFrame and the JPanel inside my tabbed pane.
setLayout(null);
jPanel4.setLayout(null);
Anyway, that's not needed when using the JLabel instead of the JPanel to hold the image.
However, my ImagePanel would not draw the image anymore, so I replaced it by a JLabel which draws the image just fine:
void setPoster(BufferedImage poster) {
JLabel imageLabel = new JLabel(new ImageIcon(poster));
panelPoster.add(imageLabel);
imageLabel.setSize(new Dimension(222, 327));
}
Note that without setting the size of the JLabel, it won't display anything.
I've spent a fair bit of time researching how to change the background color of JFrame, but haven't managed to make anything work. My code in its current state looks like this:
final ImageIcon cardIcon = new ImageIcon("cardImages/aceSpades.gif");
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g) {
super.paintComponent(g);
cardIcon.paintIcon(this, g, 20, 20);
}
};
JFrame window = new JFrame("Deck of Cards");
window.add(panel);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setPreferredSize(new Dimension(200,200));
window.pack();
window.getContentPane().setBackground(Color.green);
window.setVisible(true);
This is the result of all of the searches I've done on stackoverflow, so some help would be appreciated. I know I need to change the content pane instead of the actual frame, but all of my efforts seem to result in the default grey background. Thanks in advance.
edit: panel is for an image that's being loaded in.
edit 2: Sounds like panel is blocking the background from changing.
The JPanel you're adding to the frame is blocking the background color. Either set the panel's background color via panel.setBackground or make the panel transparent by setting panel.setOpaque(false).
So I have a JPanel that has an inner border (it's toggled based on MouseEnter/MouseExit, as a sort of a rollover effect). I also have a JLabel. The problem is that the JLabel seems to be positioned relative to the border - not the actual edge of the JPanel. So whenever I move my mouse over the panel, the label shifts over a couple of pixels. I would prefer it to stay stationary.
So I guess my question is, what's the best way to change the border of a panel without affecting the positions of the components inside the panel?
Here's the mouselisteners for the panel:
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
panel.setBorder(BorderFactory.createBevelBorder(1, Color.BLACK, Color.BLACK));
}
#Override
public void mouseExited(MouseEvent e) {
panel.setBorder(null);
}
});
The JLabel is added simply using borderlayout:
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("testlabel");
panel.add(label,BorderLayout.PAGE_END);
You could try using an EmptyBorder when the bevel border is not in use. Give it the same width/height you would the bevel border.
I don't do a lot of messing around with layouts or their managers but that's what I would try.
Edit
Since it seems you may wish to have an overlay type effect instead of a border, you could create a custom JPanel class and include some code in the paintComponent(Graphics g) method to draw this overlay.
Something similar to:
public class OverlayBorderJPanel extends JPanel
{
boolean containsMouse = false; //set to true by mouseListener when contains
BufferedImage overlay = //you would need to load an image border here,
//rather than having a java created border
//You could have alpha so it is half see-through
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (containsMouse)
{
g.drawImage(//use 0,0 position with panel width/height)
}
}
}
I think it would work with something like that, but you may need to call the panel's repaint() method in the listener as well.
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);
}
}
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