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).
Related
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.
I am designing a Jframe using netbeans. I do have few questions.
Can we create a label for a field in a desired location(For eg.,we have a field named height, I need to display a label below it indicating height is in cm) conditionally?
Can we disable a field based on a condition?(by disable I mean it shouldn't be displayed in my frame)
Can someone suggest me whether we can achieve them through some examples.
Tried this, after some helpful suggestions
private void englishRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JLabel userlabel;
if (englishRadioButton.isSelected())
{
userlabel = new JLabel("Inches");
userlabel.setBounds(311, 59, 64, 36);
//userlabel.setLocation(307,55);
//userlabel.setSize(70,40);
userlabel.setForeground(Color.green);
userlabel.setText("Inches");
userlabel.setVisible(true);
System.out.println(englishRadioButton.getBounds());
inchesTextField.setVisible(true);
}
}
The textfield is visible only when I click the English radio button,at the same time I need to get a label but it's not displayed with the above code. Can I know where I am going wrong?
Please see the attached screenshots
When English button is clicked, I need a label beneath the second textfield as inches, I am disabling the text field when Metric is displayed. I am able to achieve the later one but not the former one
Thanks!!
Yes, relative placement of components is easily achieved with use of layout managers.
Yes, all components have a setEnabled(...) and a setVisible(...) method either of which can be called at any time during a program's run. The former helps you activate/inactivate components and the latter helps make them visible/invisible. If you want to swap complete "views", use a CardLayout.
Regarding:
Can someone suggest me whether we can achieve them through some examples.
Please, you first as I strongly believe that the onus of effort here should be yours, the questioner's, since you're the one asking the questions, and the one with the most to learn by coding as much as possible. Let's see your attempts and we can help you with them. Otherwise the best examples are to be found at the Swing Tutorials.
For links, please look here: Swing Tag Info.
Edit
You ask:
I tried the above posted code,conditionally disabling the text field works well but getting a label doesn't work. Can you please suggest on that?
I don't see you adding your JLabel to any component. If you are going to create a component on an event, you must add it to a component whose ancestor hierarchy eventually reaches a visible top-level component such as a JFrame. Then after adding a component to a container (say a JFrame), you must call revalidate() on the container to have its layout managers re-layout its components, and then repaint() to repaint any "dirty" pixels.
I again will re-iterate that you're far better off not using null layout and absolute positioning, but rather using layout managers and relative positioning. If you want a label with and without visible text, it's often best to add an empty JLabel to the GUI on GUI creation, and just set its text when needed, as long as the label is located somewhere that allows its text to shrink and expand.
Also, as to your current problem, you might wish to show a picture of what you're trying to achieve, and what you're getting. Or if you can't post a picture here yet, post a link to an image or images you've created, and then we'll post it for you.
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 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
Please bear with me as I've just started using NetBeans for the first time! Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time. It'll start with one panel, and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I've tried looking into utilizing LayeredPanes since that's what I've come after hours of researching this only.. I don't understand how to do it! I think using different panels would be much easier than using different frames, so that's why I'm just going to stick with layering panels.
If anyone could explain LayeredPanes, I'd be very grateful! I'm not sure my coding will help here, but if anyone needs it I'll put it up.
This tutorial should point you in the right direction, however, if you want to have items positioned over each other, you might also want to take a look at the CardLayout:
The CardLayout class manages two or more components (usually JPanel
instances) that share the same display space.
Reading your question I dont think that what you need is a LayeredPane,
Basically what I'm trying to do is create different panels that fit inside one frame, except that only one panel will be visible at a time.
Tell exactly what you want to achieve, I mean on what you are working on.
...and depending on what the user inputs, the panel that corresponds to what the user puts in pops up.
I'd like to suggest a JDialog , show dialogs depending on the user inputs. To make sure if this is what you need, you want to provide us with more information. :)