some inquiries in working with jFrame - java

I have a j tree in the left side of the window, I want to make every node in it have a different content when clicking on it. but with in the same window.
(example: same as what we have in our computers, downloads, documents, pictures, ...etc )
I want to know how its done.
And I would like to display a document with a long text and some pictures in some of the nodes, I want to know the best way of doing that.

Place a JPanel that uses a CardLayout in the center of your window. Then add each node's content to that panel. CardLayout shows only one of its children at a time, and will always have a preferred size that accommodates all of the children, both visible and invisible.
Display long text using a JTextArea (if it's all in the same font), a JEditorPane (if it's predefined HTML that you are packaging with your application), or a JTextPane (if the text is dynamic, and has images and/or uses multiple fonts). All of those should have setEditable(false) called on them, and should be placed in a JScrollPane.

Related

.How can I spread my text over multiple jtextpanes?

I´m building a simple word-editor in java. Currently, everything´s working fine. Now I want to create "Pages", like in word. The JTextPane representing a Page is supposed to check if it´s full and then create a new JTextpane under it. With a scrollbar I would be able to scroll between them. So far this wouldn't be problematic. However, all the pages should belong to a single document, and if I were to delete a line on let´s say page 2, every line on every other page will be moved up. (For example) Is there an easy way to do this, or will I have to create DocumentListeners for each JTextPane, changing everything on each change? Also, is there a way to extend selections over multiple pages?
Personally I havn't tried anything as of yet, since I want some tips before writing myself into a corner. I thought that I could make the pages uneditable, and instead use a caretListener to check the position the user clicks on, to edit an invisible infinite JTextPane containing the actual document, which would write it´s content to the visible pages.
Lots of unknowns, but maybe the following will give you something to think about.
all the pages should belong to a single document,
Agreed.
The JTextPane representing a Page is supposed to check if it´s full and then create a new JTextpane under it
Maybe add each text pane to a JScrollPane, but don't display the scrollbars or Border of the scroll pane.
if I were to delete a line on let´s say page 2, every line on every other page will be moved up
You would need to manually control the viewport of each scroll pane. The first page would position the viewport at offset 0. The next page would position the viewport at the offset that represents your page height.
Then any changes to the Document should be automatically reflected in all the text panes.
You would also probably need to use setAutoScrolls(false) to prevent the viewport from scrolling as you drag the mouse.
is there a way to extend selections over multiple pages?
Selection is a property of the text pane, not the Document.
Not sure what will happen as you try to drag the mouse from one text pane to another.
I'm guessing you might need some special logic. Maybe using mouseEntered/Exited events to trigger this type of processing.

GUI building trouble

I have a GUI setup that looks like this:
It displays a document and enables user to edit it by adding/removing JTextAreas and text in them.
I have a problem when trying to open the document. Document itself is made up of layers of JPanels on top of each other. There are never more than 6 layers of JPanels at a single point in the GUI.
Since I don't know what the height of the JTextArea will be, I have to make it relative to the parent (in other words, not specify it).
Because the layout manager (MigLayout) doesn't know the exact size of the component (it's relative to the parent), it first asks its parent for the parent's size. That size is again unknown and the parent's parent is asked and so on (until a level 1 or 2 JPanel). In the end it has to ask a lot of components for their size (a method checkParent(Container) within MigLayout was called over 100 000 times just for the example above).
What I need to do is set the height of the JTextArea (or the JPanel that it resides in) only when the document is being opened, so the layout manager doesn't have to ask JTextAreas parent, causing a recursive hell.
After the document is opened I have to reset the height to default (so the height adjusts when the text is added/removed).
I have absolutley no idea how to do this, or if this is the way to go, I'm only sure that the thing I described above is the problem.
Several notes:
this is not a MigLayout bug, I have been to the forums(link below)
check this out (the last post)
I can't put a JScrollPane into the JTextArea as its task isn't only to hold the information, but to display exactly how much room it takes
I can easily get the heights of the JTextAreas when saving the document, thus having them at my disposal when opening it again
EDIT:
The document referenced in this question is not "the document" as thingy used in JTextAreas and similar, but "a document" as in custom class in my program (which I didn't mention because it is irrelevant, unless it's understood as "the document").
Because your Document models a hierarchical structure, you need a tree. Because your interface requires both a control and a view for each node, you need a table. Outline, seen here, might be a suitable choice. Your DocumentTreeModel would hold a reference to the document's root Element, just as the example's FileTreeModel holds a reference to the root File. Your RowModel would include a JLabel, a JCheckBox and a JTextArea.

java swing- Possible to add JLabel next to application icon/title?

Is the above question possible? The effect I'm trying to achieve is similar to how MS Word displays "Document- Microsoft Word (Technical Preview)" in this picture link: http://img.blogsolute.com/ms-word-2010.png, but with a colored background.
You can set the title of any frame you create by passing the title string to the constructor of the JFrame. You can't, however, add any controls to the 'decoration' portion of the frame - i.e., the title bar.
What you probably can do, however, is create an undecorated frame, and manually add the decoration using customised Border objects. This effectively allows you to put any controls you like around the outside, and the root pane will happily work inside it.
Why do you need JLabel for that? You can use setTitle("") for this purpose

JTable Header contains Image and on top of that image I want to place 3 buttons in a single header

JTable Header contains Image and on top of that image I want to place 3 buttons in a single header. My requirement is to Create a "Play List" table in which user can add their favourite songs.
So in the header I want to put a "Play List" title and "+" button to insert new playlists and "Export" and "Import" buttons.
How I can do that? Thanks in Advance.
I doubt(1) this use-case actually calls for cramming extra components into a JTable header. E.G. Take the UI of DukeBox.
We can see the play list on the left (a JTable) with a Filter text field and Random check-box above it, and the Enqueue & History buttons below.
This was created with a nested layout. The 'outer layout' is BorderLayout, that panel has the table in the CENTER, and nested panels in the NORTH & SOUTH. The NORTH panel has another BorderLayout, while the SOUTH uses a GridLayout.
If using a nested layout does not give you some ideas, I suggest you post a drawing or better, ASCII art, of the UI as it should appear at the smallest size, as well as a representation of it when it is resized (where is the extra width/height assigned?).
1) I have that suspicion every time I hear words to the effect "Wouldn't it be great if we had a component that..?". Of course, there are some classic counter-examples where the standard widget tool-kit seems lacking (e.g. a date picker or switch list), but these are common components to which a name can be put. If the person asking cannot put a 'catchy name' to the custom component, there is a good chance they are over (or under) thinking the matter.

NetBeans: How to select JLabel that resides inside JFrame?

I developed a small desktop application in Java. At some position in my UI I drag and drop a JLabel from Palette to JFrame. Later-on i remove its text by right click -> Edit Text. I did this because i am populating this label dynamically using setText() method. Now i need to reposition this Label, for this i first have to select that label and then drag n drop it to new location. But i am unable to do so because there is no text in that label and hence its not visible :(
Is there any way through which i select this label?
The easiest way is add a few spaces to a label instead of an empty string. You may also put a label inside a panel with layout like Flow or Grid (where you can set margin) and drag the panel instead. If you're using layout like Free Design, Absolute or Null, you may also manually rescale the label (selecting it through Inspector view).

Categories

Resources