I am Creating JLayeredPane (mainFrame) and add stuff to it like this:
JPanel textP= new Scribe(text); //this returns panel with xome text
textP.setVisible(true);
mainFrame.add(shakespeare, new Integer(6));/*This is important!!!*/
mainFrame.repaint();
mainFrame.validate();
Later I am trying to remove it like this
mainFrame.remove(6);
and it throws No such child: 6.
Other elements drawing images work fine:
JPanel imageP = new ImagePanel(image);
imageP.setVisible(true);
mainFrame.add(imageP, new Integer(5));
mainFrame.repaint();
mainFrame.validate();
//after a while
mainFrame.remove(5);
These panels are placed and removed from the pane with different order and timings defined by user input.
remove(int) removes the n'th component from the container, it doesn't relate to the layer the component was added to
The best thing is to try a remove the component by its reference, as it's not always possible to know that anything was added/inserted before your component since you added it
Related
I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}
In my main class I initially start out by creating the java frame. Later I create a new object from another class entitled 'keybinding' and pass it the JFrame. It then applies the action maps for certain keys to the root pane of the JFrame.
I was previously writing this application in the educational IDE BlueJ where this code worked fine. But I'm in the process of moving it to regular IDE, in this case JDeveloper. All the code works apart from the this keybinding.
With the below code, using JDev, calling getRootPane() returns null.
Stage.java
jf = new JFrame("The Title");
jf.setSize(800,600);
//etc
Keybinding keys = new Keybinding(this);
Keybinding.java
KeyStroke pressLeft = KeyStroke.getKeyStroke("LEFT");
stage.jf.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(pressLeft, "pressLeft");
The parameter to the KeyBinding class should just be
new KeyBinding(jf);
Then the code in the KeyBinding class can access the frame as follows:
public KeyBinding(JFrame frame)
{
JRootPane rootPane = frame.getRootPane();
rootPane.getInputMap(...)
}
I'm trying to make a simple pie chart appear on a jpanel in netbeans with JFreeChart and got this:
public void createPieChart()
{
DefaultPieDataset myPie = new DefaultPieDataset();
myPie.setValue("Apples",new Integer(12));
myPie.setValue("Oranges",new Integer(23));
myPie.setValue("Mangos",new Integer(7));
myPie.setValue("Pears",new Integer(22));
JFreeChart myChart = ChartFactory.createPieChart3D("Damo's Fruit Sales", myPie,true,true,true);
PiePlot3D pie3D = (PiePlot3D)myChart.getPlot();
ChartPanel myPanel = new ChartPanel(myChart);
lowerMain_PNL.removeAll();
lowerMain_PNL.add(myPanel,BorderLayout.CENTER);
lowerMain_PNL.revalidate();
}
I get no compiler errors and when it runs the window appears with the button, but when I press the button my pie chart doesn't appear. Anyone know what I could be missing?
Check the layout manager of lowerMain_PNL. Netbeans form designer uses GroupLayout by default, so unless you changed it, that's what you got. Adding to a container using GroupLayout at run time is tricky, especially if the component contains more than one subcomponent (And requires adding components to the layout, instead of using the usual add() methods).
Change it to BorderLayout instead, since you are using BorderLayout constraints.
This is probably a simple question, I'm not very used to Java programming. But I need to create a dialog with a CheckboxTree (a variant of JTree with checkboxes, see http://www.javaworld.com/javaworld/jw-09-2007/jw-09-checkboxtree.html)
Please note: I have created the JDialog in the graphical environment of NetBeans, so it has generated code for adding buttons etc. So I need to know how to add this tree after the creation of the main parts, so to speak... Maybe that's the problem, because if I do something like this:
JPanel panel = new JPanel();
this.setContentPane(panel);
Then I actually see the tree showing up in the dialog, but all the buttons and all are gone...
I have been able to add it to a JFrame and an optionspane, but I want it in a custom JDialog. Could anyone please explain to me in very simple terms what I need to do?
Here are my feeble attempts so far:
Constructor for the JDialog:
public MetadataUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
Container panel = getContentPane();
panel.add(getTree());
panel.repaint();
this.pack();
}
getTree method that creates the checkboxtree:
private static CheckboxTree getTree() {
DefaultMutableTreeNode root, child;
root = new DefaultMutableTreeNode("root");
child = new DefaultMutableTreeNode("Colors");
root.add(child);
child.add(new DefaultMutableTreeNode("Cyan"));
child.add(new DefaultMutableTreeNode("Magenta"));
child.add(new DefaultMutableTreeNode("Yellow"));
child.add(new DefaultMutableTreeNode("Black"));
CheckboxTree checkboxTree = new CheckboxTree(root);
checkboxTree.setVisible(true);
return checkboxTree;
}
This repainting and all that is the last attempt based on something I found Googling, but it made no difference whatsoever, so I'm guessing I'm way off.
The simplest way to add the tree and make it visible would be appreciated. It seems to work exactly as a JTree with regards to adding it, but I cannot make it work. So even if no one has experience with this particular checkboxtree plugin, the same (simplest) code for using a JTree in a JDialog would probably do!
EDIT:
In response to Andrew, here is the same thing (my best attempt) with a regular JTree:
private static JTree getTree() {
DefaultMutableTreeNode root, child;
root = new DefaultMutableTreeNode("root");
child = new DefaultMutableTreeNode("Colors");
root.add(child);
child.add(new DefaultMutableTreeNode("Cyan"));
child.add(new DefaultMutableTreeNode("Magenta"));
child.add(new DefaultMutableTreeNode("Yellow"));
child.add(new DefaultMutableTreeNode("Black"));
JTree tree = new JTree(root);
tree.setVisible(true);
return tree;
}
EDIT 2:
In response to Maxim, I'm confused. Things that are obvious to you are probably lost on me. Borrowing some stuff from your code this is the best I could come up with (doesn't work):
public MetadataUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
JScrollPane s = new JScrollPane();
s.getViewport().add(getTree());
getContentPane().add(s, BorderLayout.CENTER);
setVisible(true);
}
If you indeed created your JDialog with Netbeans GUI (it's a JDialog form) and the component you are trying to integrate into what you already have provides a zero argument constructor, try the following:
open your existing JDialog in Design mode
In the Navigator view right-click [JDialog] or whatever your top-level node is (should be a sibling of "Other Components") and select Add From Palette > Beans > Choose Bean
enter a fully qualified name for the class which represents your JTree component (ex. com.example.jtree.SomeJTreeComponent) and confirm. If the component is trully a JTree it will probably get added without any problems.
(optional) At this point the tree may or may not be enclosed within a JScrollPane. If it is not, you can manually achieve this by repeating parts of step 2. on your newly added component and choosing Enclose in this time around.
You will need to program other stuff by hand. I suggest you read a JTree Tutorial or refer to the documentation of your 3rd party component to help you through it.
You might also want to read more about the tool you are using to build your GUI.
UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(new Color(207,255,247)));
UIManager.put("InternalFrame.inactiveTitleBackground", new ColorUIResource(new Color(207,255,247)));
JDesktopPane baTabbedPane = new JDesktopPane();
JInternalFrame iframe = new JInternalFrame("Cheapest To Deliver",true,true,true,true);
iframe.setSize(400,150);
baTabbedPane.add(iframe);
why is my Internal Frame's title background not set on startup?
I've tried setting it on the overall JFrame init but made no difference (By contrast I could change other JFrame ui component look n feel such as MenuItem.background in this location so I thought it might have been because the JInternalFrame was not a top-level component i.e. under a tabbed pane, that maybe it needed changing at some other point, but where?)
Any tips on the correct place to call UIManager.put() for JInternalFrame?
got it eventually - the call to put() works fine after JInternalFrame creation but I did make it before I added the component to a container. I then still had to set it's UI:
JInternalFrame iframe = new JInternalFrame("blah",true,true,true,true);
UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(new Color(248,250,175)));
UIManager.put("InternalFrame.inactiveTitleBackground", new ColorUIResource(new Color(248,250,175)));
javax.swing.plaf.basic.BasicInternalFrameUI ui =
new javax.swing.plaf.basic.BasicInternalFrameUI(iframe);
iframe.setUI(ui);
I think you need to make all calls to UIManager.put before you create any Swing components.