I would like to know how can I implement a navigator tree for my application in swing. I got a long sequence of panel, and I would like to have on left side of the screen a kind of "navigator". It seems easy as concept, but I really do not know where to start.
To get you a picture is something like this:
I would like to realize the "red zone" :)
Actually I'm able to construct the tree iterating through the arraylist provided by the controller, in this way:
Iterator<SectionPanel> sectionIterator = sectionPanelList.iterator();
while (sectionIterator.hasNext()) {
root.add(new DefaultMutableTreeNode(sectionIterator.next()));
}
The question is: if I want to remove a particular object, how can I find it within the tree? Moreover, how can I connect an action with the relative JPanel ? I mean "when I click on Panelx, I would like the scroll to move and let me see that particular panel ".
Thanks for your attention.
Simone
for showing one single JPanel, you can
use JTree and to returns String value from TreeSelectionListener
put JPanels to the CardLayout
selection acme from JTree returns String value as paramanter for CardLayout#show(Container parent, String name)
for multiply the logics could be the same, but there will be x_times number of combinations
Related
I'm writing a GUI. In that GUI I have a dropdown box in which I can select different persons. Each person has a CardLayout Pane and in that Pane a table with information about themselves and things they own.
I have written a class called PanelTableItems(Person person).
On program start I use this class to create mutliple of these tables (one for each person in my program). And each CardLayout Pane has one of these tables. I have just implemented a ListSelectionListener to store the last selected row which I am using a private function to get the selected item from the table. However as I implemented this selection listener I tried with a print and for some reason it seems that if I have two persons in my program the selection listener made two prints even though the "second" table was not in view and therefore not selected.
Here is my question:
How do I make sure to only operate the table that is currently in "view" using the CardLayout? The second pane is hidden but it seems that all function calls to the first pane also manages to run on the second one as they are of the same type.
I could post a MCVE, but this is more of a theoretical/solution question than an actual coding question.
Thanks in advance.
On person selection, you could just switch visible panel with:
CardLayout cl = (CardLayout) cards.getLayout();
cl.show(cards, "idOfTheSelectedPersonPanel");
So, you should have registered those panels already in the layout each one with different id in regard to the person it represents.
Hidden (not visible) panels do not get any user input - if you see such behaving then your code does something wrong.
In ListSelectionListener you need to filter to process only events that something new is selected.
I suggest you to recheck the way you are adding the components to your cards you are maybe adding all tables to the same container witch makes only the last one visible, try adding each table to a JPanel or a JScrollPane.
Iam studying about JTree right now.
But i donĀ“t know how to render content of choosen JTrees node in e.g. bottom part of my application.
Lets say i would make application with content - see my picture.
I know how to make JTree, but how to manage - if i click on e.g. SUV just bottom part of my application will change the content, JTree remains and upper part as well. still just bottom part changes its content. e.g. suv - info about SUV, CABRIO - info abou cabrio.
Do I need another JPanels for upper part which will be stable?
Do I need another JPanels for botton part which will not be stable?
I really need help with this issue, or some link, where can i find answer on my question
Thanks
You need two separate components:
A JTree
A separate JPanel
You need to add a TreeSelectionListener to the JTree. That will let you know when the user has selected one of the values in the JTree, and you can use that selection to change the contents of the JPanel.
You'll need another JPanel for your area labeled, "This part remains the same". You will probably want to put the two JPanels in a JSplitPane with a VERTICAL_SPLIT.
I'm intentionally leaving out some of the implementation details because it looks like you're completing a homework assignment.
I have a list of entities where each entity render into widget based on JPanel. Widgets have dynamic behaviour - once placed on panel it can be changed by underlying entity. This happens automaticaly. Moreover some widgets can be resized by different actions, on button click for example.
The question is how to organise them into something like JList but without rubber stamp technics. In other words I wanna JList where each item rendered with cellrenderer stay "alive".
Right now I have implemented quick-and-dirty component based on JPanel with vertical BoxLayout, it uses JList's renderer component and it's model... but my implementation is too dirty...
Um.. yeah, using JTable is not suitable too.
Do you have some ideas?
If you don't want rubber stamping to take place then you'll have to create your own JList implementation that uses actual components.
You could try and work around the rubber stamping effect by caching each component for each row in your renderer and bind values into it and return that instance when JList asks the renderer for it. This is pretty risky because if you have 20 rows being displayed you'll have to cache 20 instances in your renderer, and only when the row isn't visible can you reuse one. That would mean if you had 5 unique configurations (A,B,C,D,E) of components you might have 10 of type A, 5 of type B, 2 of type C, and 3 of type D, and 0 of type E being displayed. However, you can't simply reuse one of those components without knowing if its being displayed or not. So you'd have to take into account if the row is being displayed and if it's the right type for the row you are rendering. And you'll have to clean up after the row is hidden.
Another option is make a single component for the row that encapsulates all X variations you have and put those on a CardLayout. Then you can simply cache one per row being displayed, and simply swap the card being displayed upon rendering that row. I think that might be the simplest option for you.
The harder part is going to be routing events click mouse clicks, keyboard events, etc to those active components to have them respond like normal components. Re-rendering the buttons when the user clicks them, and so forth is going to be challenging. Not impossible, but tedious.
Finally, variable row height JList is a pain. Especially in your calculations to figure out if a row is displayed or not because you can't simply do easy math like: int rowHeight = jlist.getHeight / model.size(). It doesn't work. You have to calculate each row's height and them up to figure out if a row is visible or not.
Doing what you're talking about is a lot of work, and very tricky coding to work around some of the assumptions of JList to make it work. In the end you might find it easier just to implement your own List control that makes different design decisions. Either way its going to require you are good at Swing to get it to work.
Ok. I don't find any implementation of such component. Let it be first one.
https://github.com/wertlex/JActiveList
P.S. I don't think this is proper way implementation... but it works.
use JList and ActionListener XD
In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.
The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().
Up to this point I have tried the following options, and had the following issues:
- Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table.
- JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions).
- JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.
I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).
Thankful for any suggestions! Thanks!
I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.
Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.
EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.
Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).
I can now easily getLocation() for each JTextField. Simple, really!
I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.
I'm having trouble figuring this one out.
The situation is no difficult to imagine so I hope I could post this without a sample code.
I have JPanel with CardLayout. The panel contains several other panels as individual tabs. What I'm trying to achive is to create a JButton that will make the main panel show last opened tab (the one before the current was shown). No big deal I thought at first.
So I started by thinking how to get the current visible tab (panel) and found out that there is no native method for that in Panel or rather CardLayout that only contains methods first(), last(), next() and previous() which are no use to me.
Then I thought that maybe to achive my goal I might go through all components of the main panel and test which one is visible which would be the current one. I find it a little complicated, fine, but how do I make this panel show by CardLayout if I don't have its card name to call CardLayout method show(parent, name)?
At this point, I have no idea. I've been searching over the Internet but haven't found a solution, only that few people have been trying to figure something similar out.
So I hope someone who already delt with this kind of issue comes across this topic :-)
Thanks for any suggestions.
Best regards
Martin S.
This brings up the question of how you arrived at the current card in the first place. If you used next() to get to the current card, then previous() will get you to the previous card and vice versa. If you use first() or last(), then you're out of luck.
Of course, you could always duplicate some of the information stored by the CardLayout to achieve your goal: if you maintain an array of component names (and update it whenever you call addLayoutComponent) as well as a previous index (and update this with next, previous and other calls), then you can just lookup the name of the previous component and use show to show the previous card.
Update: Since you know the tab names, here's how you can maintain the previous tab name:
String prevTab, curTab;
public void showTab(String newTab) {
prevTab = curTab;
curTab = newTab;
layout.show(parent, curTab);
}
public void goBack() {
layout.show(parent, prevTab);
}
You can create a map object Map. And add panels & names to card layout and to the map. Afterwards, if you have the previously shown JPanel, you get its' name from map via map.get(JPanel p).
And to find what JPanel was visible before the moment, you'll need to add ComponentListener to each JPanel. And make it to change some static variable on componentHidden event (e.g. static JPanel prev_shown).