• Post Reply Bookmark Topic Watch Topic
  • New Topic

GUI with inner classes problem...  RSS feed

 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a GUI where are several classes:
main class - the frame with GridBagLayout
inner class - JPanel, that is part of one GridBagLayout's cell.
Problem is that, when i crete it on one main class - it displays the panel with components on gridbag correctly.
But, when i want to add a component from this inner class - the gridbag cell is empty - i think it's just one simple basic problem, but i can't find solution.
OK here is the code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class hero extends JFrame{
startPanel s; //startPanel component from inner class below

public static void main(String[] args){
hero h = new hero();
}


public hero(){
JFrame f=new JFrame("Hero ... early development");
f.setSize(640,480);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
f.setLocation(100, 100);
Container container=f.getContentPane();

GridBagLayout GB = new GridBagLayout();
container.setLayout(GB);
GridBagConstraints c= new GridBagConstraints();
c.weightx = 1;
c.weighty = 1;
c.fill=GridBagConstraints.BOTH;
// ---- some other nonimportant components -----
c.gridx=3;//startPanel - which is from inner class//and it doesent display it.... why???
c.gridy=0;
s=new startPanel();
GB.setConstraints(s, c);
container.add(s)
//but when i do it in this class here it works ok ...
//but i don't want to do it all in this contructor method
c.gridx=3;
c.gridy=1;
JPanel b3=new JPanel();
b3.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
JButton stats=new JButton("stats");
b3.add(stats);
GB.setConstraints(b3, c);
container.add(b3);
// ---- some other nonimportant components -----
f.setVisible(true);



}
public class startPanel extends JPanel{
JButton start, options;
public startPanel(){
JPanel s=new JPanel();
GridLayout GL = new GridLayout(2,0);
s.setLayout(GL);
s.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
start=new JButton("Start!");
options=new JButton("Options");
s.add(start);
s.add(options);

}
}

}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you are creating a new JPanel (called "s") inside the constructor of your startpanel, and are adding everything to *it* instead of the startpanel itself.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh ... and what should i change ...
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok ... i think i got it...
my code should look like this then:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's it...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!