Remove Panel from layout only - java

I have a frame where the layout is null. There is a panel attached to it that has a flow layout.
My code has it so that a button press creates a new panel that gets added to the first panel (the one attached to the frame). Then I have a mouse listener that lets you drag the newly created panel around.
panel.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
panel.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent me) {
me.translatePoint(me.getComponent().getLocation().x-x, me.getComponent().getLocation().y-y);
panel.setLocation(me.getX(), me.getY());
}
});
However, when I press the button for it to create a new panel, it creates a new panel while returning the it to the flow layout. I've tried removing the panel but when I drag the new panel over it, it gets erased. While if I revalidate and repaint the panel after removing it, it vanishes.
So how do I prevent it from getting erased or remove it from the layout only?

Try changing the layout to null, I don't think what you're asking is possible.

Related

repaint() not working when called from mouse listener method

I am making a Solitaire program as a side project and I am having problems with the paint window I have made.
In the program, I am having a line start at one point that ends at the position of my mouse click. When I click on the window, it successfully reads my clicks and changes the xcor and ycor variables to my mouse click position, but fails to repaint a line using the new coordinates.
public class Game_Play extends JFrame {
public int xcor = 0;
public int ycor = 0;
public void setup() { //sets up JFrame
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(0, 0);
frame.setTitle("Circles");
frame.add(new MouseHandler());
frame.addMouseListener(new MouseHandler());
frame.addMouseMotionListener(new MouseHandler());
frame.setVisible(true);
}
//listener and painting subclass
class MouseHandler extends JPanel implements MouseListener, MouseMotionListener {
//when mouse pressed, the Xcor and Ycor
//will be changed to the current mouse
//x and y cords, then it will call
//repaint() to repaint the line using the
//new Xcor and Ycor locations
public void mousePressed(MouseEvent me) {
System.out.println("mouse pressed");
xcor = me.getX();
ycor = me.getY();
//prints out new cords
System.out.println(xcor + " xcor");
System.out.println(ycor + " ycor");
repaint();
}
public void mouseReleased(MouseEvent me) { //tests to make sure listener is working
System.out.println("mouse released");
}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseMoved(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
//paints the line with the Xcor and Ycor values
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("repaint check"); //test to see if repaint has been called
g.drawLine(100, 100, xcor, ycor);
}
}
}
Note: repaint() is being called from the MouseListener method mousePressed, I also have tried calling it from different MouseListener and MouseMotionListener methods to no avail.
Note: The paintComponent method notifies me if it has been called successfully, and when I click, the paintComponent method does not execute.
Note: I did notice that if I click on the screen to set the new cords then hit the maximize button on the window, it will successfully call the repaint method with a redrawn line using the new cords.
Note: the setup() method is being called from another class in another file, the code is as follows:
public static void main(String[] args) throws IOException {
deck_Create();
deck_Shuffle();
game_setup();
BufferedImage_array_Setup();
//being called here
Game_Play a = new Game_Play();
a.setup();
//
}
Last Note: I have searched high and low for the fix to this problem, only coming up with similar problems that didn't help me. Any Feedback given is greatly appreciated.
If there are any questions, let me know and I will address them for you in a few.
Thanks!
Some comment on your code:
public void setup() {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(0, 0);
frame.setTitle("Circles");
frame.add(new MouseHandler());// your panel
frame.addMouseListener(new MouseHandler()); // your listener, also a panel, but not the one you added to your frame
frame.addMouseMotionListener(new MouseHandler()); // yet another listener, also not the panel you added to your frame
frame.setVisible(true);
}
You probably meant to write:
public void setup() {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(0, 0);
frame.setTitle("Circles");
JPanel p = new MouseHandler();
frame.add(p);
frame.addMouseListener(p);
frame.addMouseMotionListener(p);
frame.setVisible(true);
}
Note that having your UI components implement listener interfaces is not a good idea. What if you want to have two mouse listeners for different components in the panel? You can't have both listeners on the panel.
A better way is to have the listener interfaces implemented by anonymous classes, following the seperation of concerns guideline.
Another thing is to add the listeners to the components that should handle them. You should be registering these listeners on the panel, not the frame containing the panel.
And finally, you should be setting the panel as the content pane using setContentPane. Usually it's best to have the panel dictate what its size should be by overriding setPreferredSize. In that case you don't need to set the size of the containing frame, rather you call pack to size the frame to the preferred size of its subcomponents.

Can't add a mouse listener to a JScrollpane which contains a list

I have a JScrollPane (myListScroll) that is added to a JPanel (which in turn is added to another JPanel before being added to a JFrame). This JScrollPane (myListScroll) consists of a list of strings. I want to be able to handle mouse events when clicking on the different items in this list.
In the code below I want to try if something happens if I click i the JScrollpane but nothing happens. What is wrong? Why is "test" not written?
JScrollPane myListScrol = new JScrollPane(myList);
myListScrol.getViewport().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("test");
}
});
I should have added the listener to myList and not to myListScrol.

JSplitPane Resize Cursor

I can't seem to change the resize cursor of JSplitPane by calling setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); Does anyone know how to get around this? I am using Nimbus UI.
Calling setCursor on a JSplitPane component will set the cursor only for left & right (or top & bottom) components.
To set the cursor for the divider component, you can use:
Component divider = ((BasicSplitPaneUI)splitPane.getUI()).getDivider();
divider.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
We can add code for mouse listener in addPropertyChangeListener() listener of JSplitPane and after loading of GUI we can fire this event to bind mouse listener to divider. Here is my code:
splitPanehor.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, (pce) -> {
Component divider1 = ((JSplitPane) pce.getSource()).getComponent(2);
divider1.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
ExomDataGUI.f.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
}
#Override
public void mouseExited(MouseEvent e) {
ExomDataGUI.f.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
});
And we can fire this event after showing GUI in following way:
splitPanehor.firePropertyChange(JSplitPane.DIVIDER_LOCATION_PROPERTY, 219, 220);

Java. Swing. Absolute position

I need to set component's location in a window.
I need to draw component on a GlassPane near another component, which was clicked. I pass the component, which raises click event to some manager, there I want to get coordinates where to paint.
public void mouseClicked(MouseEvent e) {
ApplicationManager.getInstance().drawOnGlassPane((Component e.getSource());
}
public void drawOnGlassPane(final Component caller) {
mainFrame = (JFrame) SwingUtilities.getWindowAncestor(caller);
JPanel glassPane = (JPanel) mainFrame.getGlassPane();
glassPane.setVisible(true);
Point where = caller.getLocationOnScreen();
JButton btn = new JButton("on glass pane");
btn.setBounds((int) where.getX(), (int) (where.getY() + caller.getHeight()), 50, 20);
glassPane.add(btn);
}
}
The new component appears in a wrong place. How could I set correct location?
this question is described including example in the tutorial about How to Use Root Panes, another example here and here

Get X and Y of a click on an ImageIcon, Java

I'm looking to add interactivity to an image but cannot see a way off adding a mouselistener to it. I would like to get the X & Y of where whas clicked on the image.
The Flow if the image is:
tileset = new ImageIcon("xx.png"); //ImageIcon Image that wants to be clicked
label.setIcon(tileset); // assigned to a label
panel.add(label); //assigned to a panel
tileScrollPane = new JScrollPane(panel); // Assigned to a scrollable pane
frame.add(tileScrollPane, BorderLayout.CENTER); // then onto a JFrame
You should be able to add a MouseListener to the label:
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent event) {
// Handle click - coordinates in event.
}
});

Categories