How to call a panel in swing - java

I Create a frame and in that frame I put one panel n I made a tabbed pane also. I placed 4 icons in the main panel..and I create many panels out side that frame but in the same package. I just want when I click one of the button then it will display the panel out side that class with in the tabbed pane.
Frame
Pakage
Main screen(Where tab pane and buttons are present)
Panel1.java
Panel2.java
Panel3
I just want to know how to call Panel1.java inside tab pane. I am Using NetBean.

If I understand your question correctly, you want to add the panels created in the panel classes to your tabbedPane inside your main class.
You will need to instantiate the Panel1, Panel2 object etc in your pane to then add them, such as:
public static void main(String[] argv){
JTabbedPane tabPane = new JTabbedPane();
tabPane.add(new Panel1());
tabPane.add(new Panel2());
}
But seeing your code would be helpful to fully understand your problem.
EDIT:
To do this in response to a button press, check out this tutorial on Action Listeners:
http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Related

Create Custom Look and Feel for a JTabbedPane

I am trying to create a JTabbedPane that will always fill the top part of the Component with the tabs. Like so:
And this is how it look now:
And the code for this part:
JTabbedPane tab = new JTabbedPane();
tab.addTab("Items", items);
tab.addTab("Categories", categories);
setContentPane(tab);
//Also tried to create a JTabbedPane class and see if i could remove the labels and manually add two buttons to the top but without success.
I want the tabs to use up as much space as possible without actually shrinking the content of the panels.
So could anyone tell me how to customize the JTabbedPane, the look and feed or just the tabs themselves in order to do that.
The easiest approach is probably to create your own component:
Create a "main" panel that uses a BorderLayout.
Create a panel that uses a GridLayout and add your buttons to this panel. Then add this panel to the "main" panel using BorderLayout.PAGE_START. Each of these button will display a specific panel when clicked.
Create a second panel that uses a CardLayout. Add this panel to the "main" panel using BorderLayout.CENTER. Each of these panels will represent a tab.
The other option is to look at the TabbedPaneUI and find a method that paints each tab and modify the code for your requirement.

Using a button to add a jPanel over a JFrame

I've been looking for a while and I can't find an helpful answer for my problem.
I'm using the Netbeans GUI Builder but right now I'm stuck: my intention is to use a button to make visible a jPanel object.
I've tried to directly insert this jPanel inside the JFrame (leaving it not visible until the button is pressed). The thing is, I could access the jPanel without the need of pressing the button: the panel wasn't visible but I could access the textfield if I tried to click over the blank space the panel is supposed to appear onto.
So now I have a separated class jPanel1 but I don't know how to add it to the JFrame.
This is the code I'm using for my button
private void modifyActionPerformed(java.awt.event.ActionEvent evt) {
JPanel newpanel=new modifyUserData(this.thisUser);
this.getContentPane().add(newpanel);
newpanel.setVisible(true);
}
but when I run it and press the button I get an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
Any ideas?

Reload main JFrame from another JFrame

I have a main JFrame containing a JPanel with some stuff in it. Among other things, another JPanel with a list of sub-JPanels and JComponents in it.
What I want to do is to refresh the window (or only the list) by pressing a button in another JFrame.
I tried to do it by
Mainclass.frameIWantToRefresh.invalidate();
Mainclass.frameIWantToRefresh.validate();
Mainclass.frameIWantToRefresh.repaint();
But it doesn't work and I have no idea how to do it in another way.
Any clues?

Having a JButton open a new JPanel

I am creating a program which currently has 3 classes. These include the JFrame class in addition to the 2 other JPanel classes. Does anyone know how I can make a JButton in one JPanel class remove that specific JPanel and have the JFrame add in a new JPanel (from another class)?
For example, let's say that the JFrame class is called frame, and the two JPanel classes are called panel1 and panel2. If a button is clicked in panel1, how do you tell the frame to remove panel1 and add panel2?
Well it depends from what you mean with the "remove" word.
A good approach will be to use the MigLayout using the hidemode property. The initial state of your form will contain both panels in the JFrame ,but the second panel will be invisible.
When you press the button of the first JPanel, it will set the visibility of this panel to false, and the visibility of the second panel to true.
On the other hand, if you want to remove completely from the frame the first panel, give a name to it using the method panel.setName(panelsName), and then retrieve every sub-component of the JFrame and put them in a collection.
Then iterate through this collection, and check for the name of every component.
If component.getName().equals(panelsName) then use the frame.remove(component) method to get rid of it. After that, it is possible that you have to call validate and repaint in your frame, though I am not sure if it is necessary. Just try it out;)

How to display a jpanel thats been passed as an argument in netbeans?

I have used the Netbeans GUI Designer to come up with a JFrame and I wish to pass in two JPanels to the 2 dummy JPanels already placed inside the JFrame using the Netbeans GUI Designer.
However, the code below doesn't work and the JFrame comes up blank. Why is that so?
public Summary_Page(JPanel jp1,JPanel jp2) {
initComponents();
this.jp1=jp1;
this.jp2=jp2;
this.setVisible(true);
}
If this method is inside the class which extends JFrame, then just add the following line inside the Summary_Page() method:
this.add(panel);
You need to add panel to the parent component. If your class extends JFrame for example, use
this.getContentPane().add(panel);
(there is no need to set it visible). JFrame - is a window that has header and buttons(exit, minimize) but you want to add components in white rectangle so you getting content pane at first. If you have another parent component instead of JFrame you can add component this way:
this.add(panel);
Then if you want to add more then one component you need to use one of Layout Managers.

Categories

Resources