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.
Related
My goal is to have a program with 3 panes. A mulitfactor Auth. The first pane will have the user type in a passphrase, while the second pain will allow the user to pick a image from a drop down list. But I want the 3rd pane to launch just to the right of the 2nd pane after the use selects a image in the same "Main" stage.
Not looking for someone to code a program just point me in the right direction to what im trying to do. My searching skills are failing, either im not explaining it right or theres another word for this.
Edit:
This is my idea of how i want it to work. Now that i look at it using a border pane probably makes since, But im still stuck with, How can I launch each section of the border at a different time, i.e when something is clicked.
I would go about it by having 3 panes side by side and just blank for the first FXML file you load in. I would then have another FXML file with the same layout that contains what you want to show up in those panes.
Then with that, you can have the controller on request (like when a user hits submit or however you are wanting these to show up) grab the content inside of the pane on the second FXML file by ID and load it into the pane.
I've done something similar with changing anchor panes and keeping the toolbar from the original so I can add more on this when I get home and should be able to supply some code that is modified to fit your issue.
Edit 1: Sorry I was in a hurry to submit that dive I had to go but I am on mobile now so I can edit but not able to add a lot, just felt I needed to say, there are different options for what you can use to do this which is why I just said a pane instead of anything specific. Just wanted to submit something so you can start looking in the right direction till I am able to update.
Edit 2: Alright now that I am home I tried this out and was able to get this working. Here is how I did it.
So I had two FXML files. One with the 3 areas that you have your items, however, only the box that you want to show when it starts is shown. Each area is enclosed by an AnchorPane. I used the AnchorPane as a container so I can swap out what is inside of it. I then had a second FXML file that had all of the boxes you want to show all of which enclosed in AnchorPanes. Here are pictures explaining what I mean.
I have the first pane named initial.fxml and the second named grabfrom.fxml. For the pane names, I just have it as pane1, pane2, and pane3. Lastly, the methods I have are show2() and show3() and call them from the FXML when the respective buttons are clicked inside of the AnchorePanes.
With initial, I just load that up as normal from the start method in my main class and that is all that is needed to be done with that. We only had it so we could display something that does not have the boxes showing before needed.
Now for the important part
With what I have in show2(), which is called when the button inside of the first pane (which is there from the start) is pressed.
public void show2() throws IOException{
AnchorPane toSetPane2=(AnchorPane) FXMLLoader.load(getClass().getResource("grabfrom.fxml"));
toSetPane2=(AnchorPane) toSetPane2.lookup("#pane2");
pane2.getChildren().setAll(toSetPane2);
}
What this is doing is loading the grabfrom.fxml into a temp var that we cast to an anchor pane. (Do note that this works since as you can see in the screenshot the whole FXML file is an anchor pane. If you're not using it that way you can take out the casting and cast to something you are using or not even cast depending on what it is.)
It then set the var we just made to just the AnchorPane we need, which is the second one since that's the one we are adding. It does this with the .lookup("#ID"); method to get just the pane we need.
Lastly, it sets everything inside of the current pane2 to toSetPane2.
This could all be compressed down into one line, however, I have left it as is for easier reading.
You should be able to use this method of loading in a portion of your application for loading in the third one and for that matter any other parts you want to in any situation.
Edit 3:
Also as #Swatarianess had said, there are stackpanes, this method will work with anything that you can set an ID to so they would work just as well. I used AnchorPanes because I have done a fair bit with them and had some code I could recycle whilst making a test for it so it was easier. All you would do if you were using those though is just cast to a StackPane instead of an AnchorPane like this:
public void show2() throws IOException{
StackPane toSetPane2=(StackPane) FXMLLoader.load(getClass().getResource("grabfrom.fxml"));
toSetPane2=(StackPane) toSetPane2.lookup("#pane2");
pane2.getChildren().setAll(toSetPane2);
}
The transition between panes could be done with a stackpane.
I am building an application with several tabs containing JPanels with JTextPanes. While testing, I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that? Also, how can I force that the text, once the line has been completed, jumps to the next line on the JTextPane?
Thanks!
UPDATE: I opted for the first option that Julien Gauthier mentioned, which is using JTextArea and enabling the Wrapping. It worked neatly. Given that the size of my text boxes (or areas) is more than enough for the limit I established as text capture (800 characters), I did not need to add Scrolls to the text areas. And also, I did not had any trouble with my save-text-to-file functions with JTextAreas instead of JTextPanes. Thanks a lot for the help!
I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that?
Add the text pane to a JScrollPane and then add the scroll pane to the frame. Then the text will wrap and scrollbars will appear when required.
Read the section from the Swing tutorial on Text Component Features for a working example of a text pane.
You have several possibilities :
You can first replace the JTextpane by a JTextArea, and use jTextArea.setLineWrap(boolea wrap).
You can use an editorKit with your JTextPane, wich will wrap the text. Please look this example : http://java-sl.com/tip_html_letter_wrap.html
You can use a JScrollPane, but the result will be ugly with long sentences.
Good luck.
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.
I'm trying to figure out how to manage this layout in order for it to work. I have some ideas, but rehauling the whole thing is quite a bit of work to do.
This is how it looks like (in JTextAreas: "component name (parent (parent))"):
I have explaind the structure at the end of the question, if you feel the need to know.
This GUI is supposed to be very dynamic. You should be able to add and remove chapters, pages, questions and answers.
The GUI in the image above is made using nested JPanels (up to six layers on the thickest parts!) which most don't have thier size specified so they can adjust to the changes in the document. However, a lot of time is consumed (about a second per page) when drawing the document because the program keeps recalculating sizes of all the JPanels until they fit. So, unless I can specify the initial size (MigLayout) of a component, this method won't cut it for me.
Only alternative I have come up with is trying to put it all in one layer using MigLayout, which is doable, but I don't know how well does it work with the dynamic part of the whole thing. Removing and readding all the components (document could have over a hundred pages!) doesn't really seem as an option. Since most of the components are nested one onto another and are to move as one, this makes this solution even more difficult.
Also, all widths are fixed, while all of the heights within a page are flexible.
I really don't know how to go about this. Should I modify one of the existing ideas to work, or are there maybe libraries which are used in this type of situations? Is there another way?
Any ideas?
Also, as promised, this is the structure explained:
So, the thing important here is the JPanel inside a tab. It contains the DOCUMENT.
Document itself is made up out of random number of CHAPTERS. Each CHAPTER contains random number of PAGES. PAGES have MARINGS and CONTENT. On the image, pink and red parts are the MARGNIS, while everything within is CONTENT(green). CONTENT contains a single TITLE(blue). TITLE is made out ofa single JTextArea. After the TITLE, CONTENT can contain a random number of QUESTION(orange). QUESTION contains a JLabel(number) and JTextArea in one row, and below is a it's ANSWER PANEL. ANSWER PANEL contains up to five ANSWERS(yellow). Each ANSWER has a JCheckBox, JLabel (letter) and a JTextArea all in the same row.
Here I have some things marked out:
You seem to have the design you need. Break down each section and apply the required layout to achieve that section. Each section should be a self contained component.
So to my mind, start by modelling the data. You need a Document model, which contains a list of Chapters, which contains a list of Pages, which is made up of a list of Titles, which is is made up of a list of questions.
I would then provide a view for each level of the model. This will allow you to concentrate on the individual needs of each view, in isolation and reuse the code logic. It also means if you need to make changes, they will be more easy to make and reflected through the entire program
You seem to have the right idea for the Document/Chapters, being laid out within tabs.
I'd follow through. Each Page would be a self contained component, possibly using something like a GridLayout.
Each Content section would be its own component, consisting of the title editor and then the questions.
Here I'd use a BorderLyout, placing the title editor at the north position and the question panel in the center. You could then use something like a GridLayout for the questions pane.
As for the margins, you can achieve hese through the use EmptyBorders
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.