Update component position at run time - java

I'm currently stuck with the following problem:
I need to dynamically update component positioning at run time. I'm making a form editor for an external application. I use wrapper-classes for standard Swing components, currently JPanel and JLabel. Panels are using TableLayout. I'm storing each component position in a field for each component. When something is changed, I need to recursively update all positions. Here is my method for updating positions:
public void updatePositioning() {
Component[] comps = getComponents();
removeAll();
for (Component comp:comps) {
System.out.println("Moving component "+comp + " to x="+pos.get(comp).getX()
+" to y="+pos.get(comp).getY());
c = new TableLayoutConstraints(String.valueOf(pos.get(comp).getX())+","
+String.valueOf(pos.get(comp).getY()));
add(comp, c);
if (comp instanceof EditPanel) ((EditPanel)comp).updatePositioning();
}
repaint();
revalidate();
}
I know, it's rough, but it's not working. All the components are seems to belong to 0,0 grid cell. X's and Y's are correct, as I've seen through debugger. Here is how I add components to my panel:
public void addComponent(TableLayouted comp, int x, int y) {
c = new TableLayoutConstraints(String.valueOf(x)+","+String.valueOf(y));
add((JComponent) comp, c);
//saving position of the component
pos.put((Component) comp, comp.getTablePositon());
System.out.println("Component "+comp+"added to x="+x+"y="+y);
}
Any suggestions?

Any suggestions?
JComponent.getTopLevelAncestor().validate()

I have finally solved it with the following code:
Component[] comps = getComponents();
removeAll();
layout = new TableLayout();
layout.setColumn(columns);
layout.setRow(rows);
setLayout(layout);
Which means, fully re-creating all layouts is working. Thanks everyone for help!

Related

Java Mouse Position couting area that was scrolled

I didnt know how to make a good title for this.. but here we go:
I am trying to get the accurate mouse position when I click on a label, using a MouseAdapter I added to my the JViewPort of the JScrollPane
My panel itself is added inside the JScrollPane.
And when I try to get the coordinates of the mouse position is always relative to the area of the panel that is visible.. its not counting the area that is not visible, because of the scroll..
I dont know If I made myself clear, I hope so..
Already tryed using getMousePosition from JViewPort and from JScrollPane and also did not work..
Thanks alot in advance!!
Here is some code:
The construction of the frame that has the panel inside it..
public GraphViewer(ArrayList<TimeSlot> graph) throws HeadlessException {
final MyCustomPanel panel = new MyCustomPanel(graph);
panel.setPreferredSize(panel.getLargestSize());
scroll = new JScrollPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
JViewport v = scroll.getViewport();
ViewportDragScrollListener l = new ViewportDragScrollListener(panel);
v.addMouseMotionListener(l);
v.addMouseListener(l);
}
What about adding JViewport.getViewPosition() to the coordinates obtained from getMousePosition()?
evento mouseClicked...
x= event.getX();
luego:
Point pos = scrollImagen.getViewport().getViewPosition();
pos.translate(x, y);
...
double nuevaPosx = pos.getX();

Get shown component in JScrollPane

I have a JScrollPane containing a JPanel. I fill this JPanel with many buttons.
Is there any possibility to get the currently shown buttons?
I know I can access the children of a JPanel via jpanel.getComponents() but those are all components in this pane; I want only the ones that are currently on screen.
As already commented to #mKorbel's answer:
it's correct that you need the child bounds
it's correct that you need to intersect those bounds with "something"
it's wrong that you need the containing viewport (nor the scrollpane)
JComponents have an API to get their currently visible part independently of how/where exactly they are currently shown, so the "something" is the JComponent's visibleRect:
Rectangle visibleRect = myPanel.getVisibleRect();
for (Component child : myPanel.getComponents()) {
Rectangle childBounds = child.getBounds();
if (childBounds.intersects(visibleRect)) {
// do stuff
}
}
I assume that this container is already visible on the screen, then I suggest
1) to extract JViewPort from JScrollPane,
2) addChangeListener to JViewPort
3) each visible JComponent(s) returns Rectangle
4) and Rectangle#intersects returns Boolean value if is JComponent(s) visible or not in JViewPort
How about asking the components if they're visible:
for ( Component component : jpanel.getComponents() ) {
if ( component instanceof JButton && component.isShowing() ) {
// We've found a button that is showing...
}
}
Component#isShowing()
scrollPane.getViewport().getView()
scrollPane.getViewport().getViewRect()

Java GridLayout is laying out all my JPanels in the top left corner

SOLVED:
Just found out what the problem was, after trying to make an SSCCE.
It had to do with my cell class, I didn't realise I was overriding getX() and getY() from the JComponent class.
After renaming these accessors it all works as expected
========================================
I have a JPanel with a GridLayout set at 3 rows x 3 cols.
I'm trying to add JPanels to each cell in the gridlayout to fill up all 9 cells.
Each one of these JPanels has an overriden paintChildren method which will paint some kind of rectangle starting at the top left of the JPanel - the end result will be each cell has a rectangle in it starting at the top left of the cell.
After adding all the JPanels to the gridlayout, they all appear in the top left corner overlapping each other (I have confirmed they are overlapping), instead of being laid out in a 3x3 grid.
How can I get them arranged in the 3x3 grid?
(Simplified) Code:
public class Panel extends JPanel {
public Panel(int x, int y) {
layout = new GridLayout(x, y, 2, 2);
setLayout(layout);
populateGrid();
}
public void populateGrid() {
removeAll();
for (int i = 0; i < 9; i++)
add(new Cell(50,50));
}
}
public class Cell extends JPanel {
public Cell(int x, int y) {
// x/y values used to define rectangle
setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));
setBackground(Color.WHITE);
}
public void paintChildren(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, x, y);
}
}
Make sure you import the right Panel. (And not the java.awt.Panel class.) Or, better, rename your class to GridPanel or something similar to avoid confusion / clashes.
You probably don't want to override paintChildren. Overriding paintComponent sounds like a better option in this case.
You may want to set minimum / preferred / maximum size for the Cell class.
Hard to make more observations without the actual code that instantiates and uses your Panel class.

Find components in Java's GUI hierarchy

With this code I am able to find what tab is selected but I need to do stuff with what is inside the tab. How do I work with the hierarchy?
EditPane.addChangeListener(new ChangeListener() {
// This method is called whenever the selected tab changes
public void stateChanged(ChangeEvent evt) {
JTabbedPane pane = (JTabbedPane)evt.getSource();
// Gets current tab
int sel = pane.getSelectedIndex();
}
});
The component that is inside the tab is a JScrollPane.
You don't need the index of the pane, you need the component selected underneath.
use getSelectedComponent() - e.g.
JTabbedPane pane = (JTabbedPane)evt.getSource();
JComponent myComponent = pane.getSelectedComponent();
To clarify your original goal, you want to manipulate the client object living in the JScrollPane. You're missing some objects.
in your JScrollPane you need to invoke getViewport().getViewportView() from the ScrollPane. (Source: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html )
# Dasdasd
I already checked it out but it only returns ViewPorts and ScrollBars
yes that correct, (probalby there you put JPanel) then you have to repeats your steps again, until as you will not find JPanel into ViewPort, that's possible get JComponents another way(s), but this is very good lesson for Hierarchy of JComponents
Component[] components = xxx.getComponents();
for (int i = 0, l = components.length; i < l; i++) {
if (components[i] instanceof JScrollPane) {
JScrollPane scr = (JScrollPane) components[i];
Component[] components1 = scr.getComponents();n

JScrollPane always clears my Panel

Hy.. I have a JPanel, and in this contentPanel I added some other custom panels and give them locations etc. So now I added a JScrollPane to the contentPanel and always when I scroll down it clears my contentPanel, but the panels are still there but not visible...
How can I make them visible again?
That's my code to add the Panel into the contentPanel. The x,y,j are some settingsstuff for the location because I have an fixed window.
private void reloadContentPanel() {
int x = -200, y = 0, j = 1, row = 4;
EventPanel panel = null;
int i;
for(i=0; i < this.images.size();i++)
{
panel = new EventPanel(this.images.get(i).getAbsolutePath(),
this.images.get(i).getName());
panel.setLocation(x+(j*200), y);
j++;
if(i == row) {
x = -200;
y += 205;
j = 1;
row += 5;
}
this.contentPanel.add(panel);
}
this.repaint();
}
Thanks
it sounds like you are not using a LayoutManager correctly.
after creating your JFrame (i'm guessing within your constructor) add the following (for example):
this.setLayout(new FlowLayout());
this will certainly not be the best layout manager for what you are trying to do but will stop the add calls from overriding the displayed component.
you will need to read further about LayoutManagers
besides this, it's not really advisable to extend JFrame. It's better practice to treat JFrame as a member of your class just like all the other components.
I have the answer! :)
I use a GridLayout not a FlowLayout, so it's fine and it automatically refreshes the panels =)

Categories