Component for glass pane console-style text display - java

I'm trying to provide a progress report for a slow operation, in the form of text scrolling up from the bottom of the screen with details on what's going on - it's an effect you may have seen a few times in video games when they're loading maps, making network connections and suchlike.
Glass pane seems to be the way to get the text overlay, that much I have working. My problem is exactly what component to use for the actual text display.
JTextArea can display text, but as far as I can see, it can only do it from the top of the screen down - is there a way to make it scroll text up from the bottom of the screen?
JLabel by contrast can align the first line of text to the bottom of the screen, and even take appended text on that line, but when I add more lines separated by newline characters, it just seems to swallow them up even after calling repaint and validate. Is there a way to make it scroll up with the new text?
Or is there another component I should be using instead?

I really like JXLayer for effects layered over Swing components. JXLayer was at one point scheduled to be included in Java 7. Unfortunately the moving around that has been going on Java.net lost all the good content that the author had. There are still some other great resources around (Java 7 required for this one) on the web. I use JXLayer to provide panels with a busy state having a web-like spinner and greyed out appearance.
Another alternative (not as capable as JXLayer IMHO) is MigLayout has absolute positioning, which is maybe easier than the GlassPane.

JLabel would be the easiest. Otherwise you will have to override paintComponent to do anything fancy like animating the text movement.

Related

Confused about Java Layouts

I'm not very experienced with Java Swing Layouts. I'd like to make a layout looking like the one in the picture. Is there any easy way to understand and create layouts?
There are tutorials on the oracle website and there are wysiwyg layout-building tools that show you interactively what you are building. One of them I have personally used in the past is the Window Builder you can get as an eclipse plugin but it is by no means the only one.
In the end though what it comes down to is experience. Fool around with them, try them out, get to know them. If you dont code with them yourself it is difficult to understand them properly.
Is there any easy way to understand and create layouts?
I cannot judge what others would find easy, but my approach to laying out containers is to look for sub-sections of the GUI that would be easily done with a particular layout, then working outwards from that.
For example, it seems a single row GridLayout might be well suited to displaying the top two text panes. Then the rest of the GUI can be created by placing the panel with the two text panes into the PAGE_START constraint of a BorderLayout, with the third text pane in the CENTER and the text field in the PAGE_END.
Done.

Creating a fully dynamic GUI

I'm currently trying to learn JavaFX and FXML (and Java) and decided to write a textbased RPG. The basis for this was already written quite some time ago, but now I wanted to do the whole thing better. Including the visuals, that is, the GUI.
First of all: I'd like to do this using FXML. That does not mean however that I'm not interested in seeing a way using basic Java.
What I want to build is a fully dynamic GUI. No Matter how it is resized, the components (and ideally the text as well) would be at the same location, relative to the other components / window border.
The window would have some kind of top line with several buttons for saving, the menu, overview and whatnot. Below that, on the left side, would be Character information: Health, Experience, Money etc. On the right side would be the text output (using a Scrollpane) plus a text field, for user input. Below the text input/output I'd place the buttons used for actions and decisions. Bottom left corner does not contain anything, though it should be a separate area.
At first I tried using Splitpanes, not knowing that they can be resized anytime and have visible Dividers. Now I'm not sure what to do.
A Gridpane would give some of the functionality I need (separate the areas), but also does not give the flexibility I want (unless I just don't know how to do it). I couldn't get it to work. So I tried using simple Panels. But with them I couldn't figure out how to keep the panels keep their relative position and size, and how to make the Buttons stick to the borders.
So what would be the best way to go about this? GridPane? Panel? Something else I'm missing? Since I don't really know how to achieve this, any help in any direction would be highly appreciated.
Have you read the layout tutorial?
From your description, it sounds like a BorderPane might be best for the overall layout (i.e. the root of your scene graph): I'm not quite sure if you could easily make this give you the empty bottom left corner you want. Alternatively you could use a GridPane as you suggested, with appropriate ColumnConstraints and RowConstraints applied to size the cells in the pane.

Swing method to show or hide part of a panel on button click?

I'd like to be able to show and hide (and therefore grow and shrink the jpanel as appropriate) in my swing application. I've seen something similar on websites that I imagine uses JS and JQuery.
What I'm after is the ability to click a button (that might say 'Advanced options') that then 'slowly' grows the panel and reveals (setVisible(true)) components.
Is this possible using Swing? I've tried looking round the web, and SO but I only seem to get JQuery questions, or unhelpful Java posts. This is the closest I've found.
Use a Swing Timer to animate your slide. For example, this link has code that places the components in a JLayeredPane and slides one component over the other, again using a Swing Timer: slide effect with JPanel.
Another useful effect is to fade out from one JPanel to another. For an example of this that uses a Swing Timer as well as a CardLayout, please check out this answer.

JScrollPane: Disable Scroll-by-dragging

I am using some JGraphX components but this question can be applied to a general purpose of java.
I have a component inside of a JScrollPane. The issue I am having is that I have the ability to pan in the view by "grabbing" the view and moving it around like Google maps. but if the cursor leaves the viewport the scrolling changes directions. I have done some research and found the issue causing the problem. It can be found part of the way down on this page where it starts talking about how the scrollpane works.
http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
Move the cursor over the image and press the cursor. Continuing to
press the cursor, drag to a point outside the image and pause. The
visible area of the image moves toward the cursor. This
scroll-by-dragging functionality is enabled by the scroll pane, and
with the JComponent API, but it is implemented by the custom component
that displays the image.
It has the little demo so you can see how it works as well as the code is available for download.
So by default the scrollpane has a scroll to drag implemented but it is actually opposite of how I need mine to scroll. The JGraphX library I am using has extended most of the classes used to benefit itself in some way but the basic idea still applies. I really just need to know of the easiest way to disable this. Yhe page says its enabled by the scrollpane and the JComponent API, but what actually gets enabled?
Not sure but I think you need to use:
component.setAutoscrolls( false );
This method from JComponent looks like the best starting point to investigate. You could retrieve the component in your scroll pane and call that method with false on it. That's the default, but it's possible JGraphX has it enabled by default.
Alternatively, you could get the MouseListeners and/or MouseMotionListeners from the scroll pane and replace them with something that overrides the dragging behaviour. Since those are interfaces, you could use proxy objects for that. Seems somewhat hacky, though.

Lightweight Component over Heavyweight Component problem

The code pretty huge and involves a lot of different class/methods:
But here is the gist:
There is a main frame : A_Main
Selecting something in the main frame A: opens a JDialog B_Dialog
This B_Dialog has a JPanel on it: C_Panel
This C_Panel comprises of a textfield and a button
On clicking on the textfield/button: opens a tooltip and another JPanel: D_Panel
Now, the problem is:
1) The tooltip overflows the size of B_Dialog and therefore gets truncated
2) D_Panel however; even if its outside the boundary of B_Dialog gets displayed fully
2.1) There are some texfields and drop down menus in this D_Panel
2.2) The mouse events function correctly in this D_Panel items (drop down menus)
2.3) But Keyboard events do not function correct (Textfield)
I would be glad if you could help!
Thanks!
This can only be done in newer versions of the JDK.
See, Mixing Heavyweight and Lightweight Components.
Now, when I try to enter something in the JAR JPanel's text field, I am not able to do so as this pops out of the border of the main JDialog that contains it.
Add a JScrollPane around the JPanel, and allow it to expand both horizontally and vertically. If this doesn't work, you may need a customized Layout Manager, or use one of the default ones like GridBagLayout.
Also, you'll need to gain focus before you can enter text, but that doesn't seem to be the problem here.

Categories