Please help me with the following code
can anyone help me please with this problem. When I click on the button in the layerOne Class it creates TestLayerTwo () Below is my TestLayerOne Class where I have TestLayertwo () created under "btnActionPerformed". what I should do there so that LayerTwo shows up.
Code:
// TestLayerOne Class
package test.gui;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestLayerOne extends JPanel {
TestLayerTwo layerTwo;
private JButton btn;
public TestLayerOne() {
initComponents();
}
private void initComponents() {
btn = new JButton();
setLayout(new GridBagLayout());
btn.setText("Button");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnActionPerformed(evt);
}
});
add(btn, new java.awt.GridBagConstraints());
}
private void btnActionPerformed(ActionEvent evt) {
// layerTwo
layerTwo = new TestLayerTwo ();
System.out.println("This is a Test");
}
}
// TabPanels Class
package test.gui;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class TabPanels extends JFrame{
JFrame myFrame = null;
// main to run and test this part of the program
public static void main(String [] args) {
new TabPanels();
}
public TabPanels() {
JTabbedPane tp = new JTabbedPane();
add(tp);
tp.addTab("Tab1",TestLayerOne()
tp.addTab("Tab2", TestLayerOne());
tp.addTab("Tab3", TestLayerOne());
setPreferredSize(new Dimension(1360,768));
// myFrame.setPreferredSize(new Dimension(1360,768));
pack();
setVisible(true);
}
}
// layerTwoClass
package test.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestLayerTwo extends JPanel {
/**
* Creates new form TestLayerTwo
*/
public TestLayerTwo() {
initComponents();
}
private void initComponents() {
lbl = new JLabel();
setLayout(new GridBagLayout());
lbl.setText("This is a Test ");
add(lbl, new GridBagConstraints());
}
private JLabel lbl;
}
Re: Please help me with the following code
What do you do with the instance you create? You need to add it to something in a GUI that is being shown to be able to see it.
Re: Please help me with the following code
I created the layout instance ( layerTwo ) but I am totally lost and cant figure out what I should add that instance to inorder it to show up in Tab1 instead of TestLayerOne after clicking on the button. Thats where I got stuck.
Re: Please help me with the following code
Have you tried adding to a container that is being shown in the GUI?
Re: Please help me with the following code
all I have is GridBagLayout and I cant really do
Code:
layerTwo.add ( new GridBagLayout());
also I tried with the Code:
layerTwo.setLayout (new GridBagLayout ());
and that did not work.
Re: Please help me with the following code
someone please help .. i am learing so help me please
Re: Please help me with the following code
Quote:
what I should do there so that LayerTwo shows up.
Where do you add the component you are not seeing (layerTwo) to the GUI? You do add ONE component (btn) on line 33.
BTW The code you posted does not compile.
Re: Please help me with the following code
Re: Please help me with the following code
Code had its main commented out but I changed it now. Also I tried with
Code:
add (layerTwo, new GridConstraints());
but it didnt work either. After refreshing both layerOne and LayerTwo shows up but the target is only the LayerTwo will show up not the layerOne.
Re: Please help me with the following code
I can not tell from one statement where it is located in the program. You need to post all code to show where it is and when it is executed.
Re: Please help me with the following code
I tested and it executes fine for me. Only thing it doesnt do is when I click the button layerTwo does not take over. But I I didnt have any compiling error at all
Re: Please help me with the following code
If you need help with the new code, post it.
Otherwise mark this thread as solved.
Re: Please help me with the following code
I already edited my code in this thread and took out the commented part and you should be able to compile now
Re: Please help me with the following code
The code in post #1 will not compile because of line 71:
tp.addTab("Tab1",TestLayerOne()
The end of the statement is missing.
Please post code that compiles, executes and shows the problem.
Re: Please help me with the following code
I guess when I copy pasted my code I somehow missed that part but still I am pasting my code again.
Code for TestLayerOne
Code:
package test.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TestLayerOne extends JPanel {
TestLayerTwo layerTwo;
private JButton btn;
public TestLayerOne() {
initComponents();
}
private void initComponents() {
btn = new JButton();
setLayout(new GridBagLayout());
btn.setText("Button");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnActionPerformed(evt);
}
});
add(btn, new GridBagConstraints());
}
private void btnActionPerformed(ActionEvent evt) {
// layerTwo
layerTwo = new TestLayerTwo ();
add (layerTwo, new GridBagConstraints());
System.out.println("This is a Test");
}
}
code for TestLayerTwo
Code:
package test.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestLayerTwo extends JPanel {
/**
* Creates new form TestLayerTwo
*/
public TestLayerTwo() {
initComponents();
}
private void initComponents() {
lbl = new JLabel();
setLayout(new GridBagLayout());
lbl.setText("This is a Test ");
add(lbl, new GridBagConstraints());
}
private JLabel lbl;
}
code for TabPanels
Code:
package test.gui;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class TabPanels extends JFrame{
JFrame myFrame = null;
// main to run and test this part of the program
public static void main(String [] args) {
new TabPanels();
}
public TabPanels() {
JTabbedPane tp = new JTabbedPane();
add(tp);
tp.addTab("Tab1",new TestLayerOne());
tp.addTab("Tab2", new TestLayerOne());
tp.addTab("Tab3", new TestLayerOne());
setPreferredSize(new Dimension(1360,768));
// myFrame.setPreferredSize(new Dimension(1360,768));
pack();
setVisible(true);
}
}
Re: Please help me with the following code
After you change the contents of a container by adding a component to it, you need to tell the container the redo the laying out of its components. Look at the Container class API doc for a method that tells the container to lay out its subcomponents again.
Re: Please help me with the following code
if I use validate (); layerOne (button) still stays
Re: Please help me with the following code
It should stay until you remove it.
Re: Please help me with the following code
little help with removing please. I tried with layerTwo.remove (btn); but that didnt work also I am thinking there should be a way to remove everything if I have more than one button.
Re: Please help me with the following code
What container contains the component you want to remove?
What container did you validate?