Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Stevo Johnson

Greenhorn
+ Follow
since Jun 01, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stevo Johnson

Wow! Worked like a charm. Thanks so much for your help Rob!
11 years ago
First attempt: setColumns(1)

Result: did not do anything.


SecondAttempt: setPreferredSize( new Dimension( 0,0 ) )

Result: The JTextAreas now expand and contract horizontally as I adjust the JSplitPane divider and the text re-wraps correctly in both cases. This is desirable. Unfortunately the height of each JTextArea is now fixed. What I would like is for the height to adjust along with the width so that the JTextArea has enough area to display all of its text. Unfortunately there is not a setPreferredWidth( int i ) method.

Perhaps I should be using a spring layout on the JPanel instead of a vertical box layout? I've never used a spring layout before and the Java API says it was not designed for manual use. How hard is it to use?


Thanks so much for the feed back, I will keep trying!
11 years ago
Hey everyone,

Here's what I'm working with:

1) I've got a collection of JTextAreas. Each concerns a separate item so I would like to maintain them as separate JTextAreas.
2) The JTextAreas are added to a JPanel using a vertical box layout. The desired effect is to have the JTextAreas positioned in a vertical column, expanding to fill the width of the JPanel.
3) The JPanel is set as the viewport view of a JScrollPane (using default scrollpane layout).
4) The JScrollPane is set as one of the children of a JSplitPane (using default split orientation).

My problem:

When I move the JSplitPane divider to give the JScrollPane more space, all of the JTextAreas expand horizontally to fill the new space and re-wrap their text correctly. When I move the JSplitPane divider to give the JScrollPane less space, the JTextAreas do not compress to fit within the new horizontal constraint. Instead, a horizontal scroll bar appears on the JScrollPane. I never want the user to have to scroll horizontally. Instead I want the JTextAreas to be compressed horizontally, forcing them to expand vertically, and a vertical scroll bar can appear if necessary. Setting the JScrollPane's horizontal scroll bar policy to NEVER did not solve the problem, it just hid the horizontal scroll bar so I could not scroll to see the content even if I wanted.

It seems to me that when the JScrollPane gets wider it resizes the JPanel to fill the space but when it gets smaller it does not. Any suggestions?

Thanks!
11 years ago
Hello everyone.

Here's what I would like to do:

1) Define data types in XSD including facets.
2) Bind those types to Java classes with facet enforcement written into the generated setter methods.
3) Instantiate and manipulate the data in Java.
4) When sending data to other application users, write the Java objects to an XSD conformant XML file for transmission over network.
5) Receive XML files from network, unmarshall data into Java Objects for receivers application.

I have looked at JAXB and XMLBeans as means of accomplishing this. I beleive I read in a tutorial that JAXB does not incorporate any facet enforcement into generated setter methods. Instead, to check XSD compliance you must rewrite the object to XML and check the XML file against the XSD. Is this accurate? I could not find whether or not XML Beans writes facet enforcement into setters.

Can anyone lend me some advice here or point me towards some better tutorials? Am I looking at the right technologies here?

Thanks!
Solved my problem!

When declaring the class you must specify the generic behind the "implements Iterable"

i.e.



Originally I forgot the <E> after Iterable in the class declaration.
11 years ago
Hey everyone!

I am writing my own ListModel that supports generics and provides an API to ArrayLists. I decided to implement the Iterable interface so I could use foreach loops on my ListModels but I'm running into trouble.

My iterator implementation in MyListModel looks like this:




In my test code I am able to write this fine:



But when I switch to a foreach loop I get errors:



Namely this one: "Error: Type mismatch: cannot convert from element type Object to Integer"


What I've been reading about the foreach loop suggests that if the top one works the bottom one should to. Anyone know what I am doing wrong here?
11 years ago
Alright so I went with the approach in my second reply and got this working beautifully. Each time I drag the component (mouseDragged event) I:

1) make the component being dragged invisible
2) get that components top level container and use the findComponentAt method with a converted mousePoint to find the highest visible component underneath the component I just made invisible
3) set that highest component as the target component
4) then make the dragged component visible again.

This allows me to drag and reposition JPanels on top of other JPanels!!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now I'm trying to expand my program to include JSplitPanes as well but I'm running into problems.

My previous solution worked like this:

1) The mouseDragged() method is given a mouseEvent as an argument. This mouseEvent contains a point which is calculated as the difference between the position of the mouse cursor and the top left corner of where the JPanel is DRAWN.
2) Next I moved the JPanel about by using the mouseEvent's point and translating the BOUNDS of the JPanel using setBounds().
3) The next time the JPanel is drawn, its BOUNDS are referenced and it is DRAWN accordingly.
4) Return to step 1.

This solution does not work when I am trying to drag a JPanel that is the child of a JSplitPane. In step 3, the bounds of the JPanel are ignored because the JPanel is restricted to reside within the JSplitPane. I don't mind that the JPanel is restricted there, the problem is that the bounds are used when the mousePoint is converted to the coordinate system of the topLevelContainer. If the bounds dot not correspond to where the component is drawn, it will not correctly determine the cursor's location.

While I was typing this problem up I poked around the API and managed to find a solution (below).

SOLUTION:
I fixed this by using getLocation() and setLocation() instead of getBounds() and setBounds(). I believe that bounds are a component preference (like size) and not a component rule. When I call setLocation() on a JPanel that is a child of a JSplitPane, neither the location nor the bounds of the JPanel change because the JSplitPane won't allow it.

Why it works:
Originally both the bounds of the JPanel and where it was being drawn were changing together, so the bounds correctly referenced the drawing location. With the JSplitPane the bounds were being changed but the drawing location was not, so there were errors. By using setLocation() we have ensured that the bounds will not change unless the drawing location does so again, they will correspond to each other and we get rid of our errors.

Another problem I found in my old code is that I was translating the JPanel's location before converting the mousePoint to the coordinate system of the top level container. This caused the mouse's movement to be counted twice.
11 years ago
Weird....I went to the API pages for container, jcomponent, etc. and did a find on getcomponentzorder. I'm using firefox so as I typed getcomponent it found the method getcomponent() but as soon as i typed 'z' firefox said there were no hits.

I followed your link and tried it again and this time it was found. Perhaps before I had not waited for the page to fully load?

Anyway, thanks for the reply, but does anyone know about 1) or 2)?
11 years ago
Thanks for the reply Craig!

I am not sure yet if I am going to try to draw the "ghost" component as I drag and use mouseEntered and mouseExited events to get the target component or if I am going to take this approach:

1) Make the dragged component invisible when the mouse is released.
2) Find the highest component under the mouse cursor and use that as the target component.
3) Make it visible again.
4) Move the dragged component to the new parent component.

Will post again when I get this working.
11 years ago
Hey all!

3 questions here:

1) Does each component within a container have a unique z-order int? In other words, when I use container.setComponentZOrder( component, 0 ) does that increment the z-order of all the other components in that container? If so I should be able to use this to implement "bring to front" "sent to back" etc. ala Microsoft Office.

2) I know the z-order effects the order that components are drawn in but does it also effect when mouseEntered and mouseExited events occur? If I add two overlapping components to a container, the second one added is drawn on top and receives mouse events for the overlapping region. If I bring the first component to the top using setComponentZOrder I want to change both the drawing order and the mouse event receiver.

3) Why aren't getComponentZOrder and setComponentZOrder documented in the Java API? Eclipse tells me they exist and provides documentation on them...

Thanks so much!
~Stevo
11 years ago
Hello everyone!

I am writing a swing program that utilizes draggable JPanels. I would like to be able to drag a JPanel, drop it in another JPanel and have the dropped panel become a child of the panel it was dropped onto. This way if I then drag the parent panel around the new child will move with it. So far I have been using mouseEntered and mouseExited events to track which JPanel the mouse cursor is in. I would like to use that information to find out which component the mouse is over when a dragged component is released. The problem is that, because I'm moving the panel as I drag it, the mouse never leaves this component.

Is there a way that I can disable the "enter-ability" of the component I am dragging? So that mouseEntered and mouseExited events are generated as though the panel I am dragging did not exist?

Alternatively, could I do something like:
1) Make the panel I am dragging invisible.
2) Find the highest visible component under the MouseCursor
3) Make the panel I am dragging visible again.

Let me know if you guys have any suggestions!
11 years ago