Java Swing CardLayout: get componente associated to a String - java

is there a command that returns the componente associated to a speciefied String name in Java Swing CardLayout? I have many panels in my CardLayuot which are at first empty, at runtime i have to "update" those panels to add buttons in it (the panels already have GridBagLayout), i would prefer NOT to make panels all over again and replace the old ones, as it would be slower to charge, and instead get those panels and add something. Thanks guys! (i checked this but there's nothing like what i need https://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html)

Every Component, such as JPanel, has a name field, and you can set it with setName(). If your program already has a List<JPanel>, you can search the list and check getName(). If not, you can add your panels to a Map<String, JPanel> and get() the one you want by name.

Related

Separating multiple JTables

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.

can Java Swing JPanel Hold a String Value?

Can a Java Swing JPanel hold a String value than can be modified/accessed where I can save some information?
I see no other option but to implement my own class holding a JPanel and a String... was just trying to save some space/coding..
I like the setName/getName of the Component super class... is there any inconvenience in using that ??
Yes it can. Any Swing component can hold client properties for the specific component.
See the putClientProperty(...) and getClientProperty(...) methods of JComponent. Using this approach you can define any number of client properties:
panel.putClientProperty("Title", "Panel1");
panel.putClientProperty("Description", "some text for the description");
You can also use the setName(..) and getName() methods if you just want to uniquely identify the panel with a string name. Many IDE's will use this property.
Of course if you are creating a panel with multiple Swing components and related instance variables then you would probably extend JPanel and customize its behaviour.
I like the setName/getName of the Component super class... is there any inconvenience in using that ??
If you feel the "name" property adequately describies the data you want to store then this is the most efficient way to store the data. However, if the data is not really the name of the component then don't force the data just because it is easy to use. Also it is possible some IDE's may use this property for generic debugging or messaging. That it may check display this value in an error message to help identify a specific component.
Several solutions, and it's hard to know what you're looking for here. You could create a MyJPanel class that extends JPanel and is identical except including a String field with getter/setter. You could also store information in silly ways like by setting/getting the name of the JPanel. (That is use setName and getName of the Component superclass.) Another solution is to add a JLabel or some other component with that information to the JPanel, and if necessary, making it invisible or hidden.
No, I think it cannot hold a String value.
You can see all getters/setters here:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html
Also, you can check the source code (if you want to go that far).
There's no getter/setter useful for holding a String value
(I mean ... e.g. no setText or setTitle).
Of course, you can add e.g. an invisible JTextField to your JPanel and
set the String into the JTextField. But that doesn't seem very nice to me.
JPanels hold JComponents, i.e. JButtons, JLabels, etc. A String is not a component. It would be best if you'd just use a JLabel with a String as its parameter, then add that to a JPanel.
You can add a Component like JLabel/JTextField and use setVisible(false). That object can hold strings.
note: Only for Buttons.
You could use setAtionCommand() but it is indented for something very specific. It would be horrible practice for anything else.

JPanel check if values changed

Lets say I have 2 JPanels.
I create both of them while program is loading and set 2nd to visible(false). 2nd JPanel uses information entered in 1st JPanel. I want to use that information.
However since both panels are created at the same time the value from 1st Panel will be null.
I need something like repaint(), but it should repaint all JTextFields with new variable values. I can't find anything useful in java api.
Is there something like this in java?
If not, what are my options?
Create a method in the second JPanel that sets all the values up.
Pass a reference to the second JPanel into the first.
When the first is ready it can call the method in the second to set all the values.

Using CardLayout in NetBeans GUI Builder

I am using the NetBeans 7.2.1 GUI Builder. I could do all of this by hand. Instead, I am using my current project to learn how this tool works so that I can make an informed decision of when to use it in the futre (if at all).
Now with help of archived questions here, I have figured out how to change the LayoutManager to a CardLayout. I have also added three JPanels to the layout (although, there seems to be a bug...maybe a question about that later). The first JPanel displayed by the CardLayout will have two buttons. Each button will cause the CardLayout to display one of the other two JPanels. To do this, I found that I can use CardLayout#show(Container, String).
I need to know what the value of the String is for each JPanel. Doing some further research, I found that NetBeans generates a line of code such as
getContentPane().add(addCardsPanel, "card2");
So I can use "card2" to show addCardsPanel. It would be convenient to use a more applicable String. Will NetBeans allow me to set this identifying String to whatever value I wish? If so, how do I do it?
In the Navigator window select the panel you want.
In the Properties window scroll down to the Layout group. You'll see a Card Name property. Knock your self out ;)

Showing the previous tab of CardLayout

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).

Categories

Resources