Please, help!
How can I update my JFrame content(add new JPanel, add button, etc) after some action take place, like press button?
Thanks!
You add the new components the same way you add them when you first create the gui eg.
existingPanel.add(new JLabel("New Label"));
Then once added you tell the container that you added to that new stuff has been added by calling revalidate(). This allows it to layout its child components according to its layout manager
existingPanel.revalidate();
Related
Whenever I run my application, Java automatically sets focus on the first component on the frame.
Here's a screenshot of the problem:
As you can see, Java has set focus on the first button (Add a new word).
I've tried this solution but the problem is not solved :(
Mainframe mainframe = new Mainframe();
mainframe.requestFocusInWindow();
mainframe.setVisible(true);
Now, How can I disable auto-focus in my application?
You can work around this by setting your initial focus to the JFrame's Content Pane. Just add following code to the JFrame after you add all other elements to your JFrame.
getContentPane().requestFocusInWindow();
Hi I am using GWT and I want to create a new panel whenever a button is clicked. Which widget in GWT will help me to create a new panel like that of Java's Jpanel?
http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/ui/Panel.html please look at "Direct Known Subclasses". You can add any, don't forget to add your panel to RootPanel or to a panel which is already visible.
I am creating a Swing based application , which actually consist of two buttons as shown below -
now what i want is when first button is clicked it must perform a action like showing a panel containing label, textfields , and some buttons in the same frame as shown below -
and when second button is clicked it will show another panel in the same frame as shown below ..
the thing is i am not understanding how to make this interface in action by providing event handler and action listener .. So please letme me know how can i archive this . Any help would be appreciated .. Thanks
There are 2 approaches.
CardLayout based.
Create all the panels (empty, panel with fields, panel with list) and add them in a container with CardLayout (the empty one is default). On click buttons swap visible cards (panels) showing necessary one.
Recreation based.
On click button create a new panel with new content. Remove the old one from container and add the newly created pane. After then call:
container.revalidate();
container.repaint();
I created a JFrame Class with Netbeans 7.3 and added two panels from the palette.
I have added a button in the first panel on the click of which I want to add a new button in the second panel(topoPane).
Below is the button click event that I have written for the same. But, the button is not getting added to the panel even when the event is getting called.
Please tell me what's wrong in it.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
System.out.println("Creating the Button");
JButton but = new JButton();
but.setBackground(Color.red);
but.setText("New Button");
but.setBounds(500, 500, 500, 500);
topoPane.add(but);
topoPane.revalidate();
}
From your use of setBounds, it is obvious that you are using a null layout. Because of this you need to call repaint() as containers with no layout do not automatically repaint added components on revalidate.
Apart from the fact that calling repaint is good practice, layout managers can remove the need to make this call along with manage the sizing and positioning of components. This makes it a good reason to use a layout manager.
public void tabbedPane(){
JPanel tab1 = new JPanel();
JButton btn = new JButton("Buton - 1");
btn.setPreferredSize(new Dimension(50, 20));
btn.setLocation(0, 10);
tab1.add(btn);
JTabbedPane tabPanel = new JTabbedPane();
tabPanel.addTab("tab1", null, tab1);
tabPanel.addTab("tab2", tab1);
tabPanel.addTab("tab3", btn);
tabPanel.setPreferredSize(new Dimension(450, 150));
tabPanel.setLocation(50, 0);
mainPanel.add(tabPanel);//Main panel on frame
}
When I run my application, I see only tab2 and tab3 pane, and I have many issues:
tabPanel.setLocation doesn't work
tabPanel.addTab("tab1" ...) doesn't work
btn.setPreferredSize(new Dimension(50, 20)); when I
click "tab2" it works correctly, however when I click "tab3" it doesn't change button
size.. why?
and i use null layout
tabPanel.setLocation doesn't work
Don't use setLocation(...) but instead use nested components and layout managers to achieve a pleasing and easy to maintain GUI layout.
tabPanel.addTab("tab1" ...) doesn't work
With Swing, you can only add a component to one container, that's it. The component will only show up in the last container that it was added to.
btn.setPreferredSize(new Dimension(50, 20)); when I click "tab2" it works correctly, however when I click "tab3" it doesn't change button size.. why?
Again, you will want to study the layout managers
and i use null layout
You almost never want to do this as this will make your application not look correct on any platform but your own and will make it very very difficult to maintain and upgrade. It is much better to use the layout managers and let them do the heavy lifting of laying out and sizing components for you.
What is your objective with this?
A JTabbedPane is used to organize views, I see you're trying to add a JPanel as a first tab, this is the 'main goal' of the JTabbedPanes.
tabPanel.addTab("Tab 1", tab1);
Try to add the tab like this, you're passing a 'null' value as the icon, which must not affect at all, but if you're not using an icon, then just add the panel as a tab with the intended name.
On second adding, you're adding again the same component (tab1).
On third adding, you're trying to add a component already on a container (tab1). This will make this component to appear only in the last container you add it to. Besides, component is a JButton. I cannot see the goal of a JButton as a tab.
For the setLocation(x, y) issue, check the layout you're using on the container.
Again, I think the main issue here is that you're not correctly approaching your problem, or you're not using the required tools.