I am trying to create a class which could be a subpanel. It extends JPanel
and implements AdjustmentListener. The JPanel is used here as a container
and it keeps JLabels and JScrorrButtons layouted with gridBagLayout.
This class instantiated in application doesn't work, none of components
(labels and scroolButtons) don't even show up.
[code]
public class parameteManager extends JPanel implements
AdjustmentListener{
public JPanel pane;
public JScrollBar scrbF;
public JScrollBar scrbA ;
public JScrollBar scrbC ;
public JTextField wartF ;
public JTextField wartA ;
public JTextField wartC ;
public parameteManager(int a,int b,int c,int d,int e,int f,int g,int
h,int i, int j,int k,int l,java.lang.String z) {
scrbF = new JScrollBar(JScrollBar.HORIZONTAL, a, b, c, d);
scrbA = new JScrollBar(JScrollBar.HORIZONTAL, e, f, g, h);
scrbC = new JScrollBar(JScrollBar.HORIZONTAL, i, j, k, l);
wartF = new JTextField ();
wartA = new JTextField ();
wartC = new JTextField ();
pane=new JPanel();
pane.setSize(500,300);
GridBagLayout gridbg = new GridBagLayout();
GridBagConstraints constr = new GridBagConstraints();
constr.insets=new Insets(2,2,2,2);
pane.setLayout(gridbg);
//adding components to the panel , to set all constraints needed for
gridBagLayout buildConstraints function is used.
buildConstraints(constr, 0, 0, 1, 1, 30, 2,3,0);
constr.fill = GridBagConstraints.NONE;
constr.anchor = GridBagConstraints.EAST;
JLabel label1 = new JLabel("Faza:", JLabel.LEFT);
gridbg.setConstraints(label1, constr);
pane.add(label1);
//amplituda
//pokaz wart czestotl
wartC.setEditable(false);
wartC.setText(""+scrbC.getValue());
buildConstraints (constr,1,2,1,1,0,0,3,0);
constr.fill= GridBagConstraints.HORIZONTAL;
constr.anchor = GridBagConstraints.CENTER;
gridbg.setConstraints(wartC, constr);
pane.add(wartC);
pane.setBorder(BorderFactory.createTitledBorder("Manager
O"+z));
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void adjustmentValueChanged(AdjustmentEvent e) {
Object source = e.getSource(); //scrbF scrbA scrbC - zrodlo
zdarzenia
if (source == scrbF) {
int value=scrbF.getValue();
wartF.setText(""+value);
}
if (source == scrbA){
int value=scrbA.getValue();
wartA.setText(""+value/10);
}
if (source == scrbC){
int value=scrbC.getValue();
wartC.setText(""+value);
}
}
private void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy,int px,
int py) {
//gbc.insets = new Insets(5,5,3,5);
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.ipadx=px;
gbc.ipady=py;
}
private void jbInit() throws Exception {
}
}
//the applicaion that uses parameteManger class
public class Chart extends JFrame implements AdjustmentListener{
parameteManager managerx,managery;
JPanel contentPane;
public Chart(){
super ("Chart");
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane,
BoxLayout.PAGE_AXIS));
//contentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
managerx= new parameteManager(2,0,0,6,60,0,60,100,2,0,0,100,"X");
managery= new parameteManager(2,0,0,6,60,0,60,100,2,0,0,100,"Y");
contentPane.add(managerx);
contentPane.add(managery);
setContentPane(contentPane);
setTitle("Chart");
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args){
JFrame frame = new Lissajous1();
ExitWindow exit = new ExitWindow();
frame.addWindowListener(exit);
frame.show();
}
public void adjustmentValueChanged(AdjustmentEvent e) {
Object source = e.getSource(); //scrbF scrbA scrbC - zrodlo
zdarzenia
if (source == managerx.scrbF) {
int value=managerx.scrbF.getValue();
}
if (source == managerx.scrbA){
int value=managerx.scrbA.getValue();
}
if (source == managerx.scrbC){
int value=managerx.scrbC.getValue();
}
}
private void jbInit() throws Exception {
this.setResizable(false);
}
}
[/code]
what could be wrong with it... maybe it is the case of event handling??
any idaes?
Thomas Weidenfeller - 18 Jul 2005 10:58 GMT
> public class parameteManager extends JPanel implements
Class names in Java are supposed to start with an uppercase letter.
> AdjustmentListener{
>
[quoted text clipped - 5 lines]
> public JTextField wartA ;
> public JTextField wartC ;
I don't see a point in making all the components public. If you need
access to them from the outside (why?), provide setters/getters.
> public parameteManager(int a,int b,int c,int d,int e,int f,int g,int
> h,int i, int j,int k,int l,java.lang.String z) {
> scrbF = new JScrollBar(JScrollBar.HORIZONTAL, a, b, c, d);
> scrbA = new JScrollBar(JScrollBar.HORIZONTAL, e, f, g, h);
> scrbC = new JScrollBar(JScrollBar.HORIZONTAL, i, j, k, l);
I would guess you want JSliders, not scroll bars. Since I can't figure
out what you want to scroll.
> wartF = new JTextField ();
> wartA = new JTextField ();
> wartC = new JTextField ();
> pane=new JPanel();
You add all your widgets to this panel, but you never add that JPanel to
anything (e.g. not to the class). But since the class implements JPanel
it bears the question if you indeed intended to work with two JPanels.
If not, get rid of that "pane".
> pane.setSize(500,300);
Do you really want to hard-code your pane size? And, if you pack the
panel (a good idea if you use GBL), than that setting is meaningless.
> GridBagLayout gridbg = new GridBagLayout();
> GridBagConstraints constr = new GridBagConstraints();
> constr.insets=new Insets(2,2,2,2);
Maybe you want to give the Java Look and Feel Design Guidelines a good
look (a pointer is in the FAQ), so to come up with distances according
to the guidelines.
> pane.setLayout(gridbg);
>
[quoted text clipped - 44 lines]
> }
> }
Again, sounds like you want JSliders, not scrollbars at all.
Also, consider implementing separate listeners for each event instead of
that if/else stuff.
> private void buildConstraints(GridBagConstraints gbc, int gx, int gy,
> int gw, int gh, int wx, int wy,int px,
> int py) {
Consider using the long GridBagConstraints constructor instead of your
own method.
> //gbc.insets = new Insets(5,5,3,5);
> gbc.gridx = gx;
[quoted text clipped - 9 lines]
> private void jbInit() throws Exception {
> }
Did you use some GUI builder to come up with that stuff? If yes, drop
that builder for the moment, and first spend some time to learn the
basics and do things properly by hand.
> }
>
[quoted text clipped - 35 lines]
> frame.addWindowListener(exit);
> frame.show();
You never pack the window.
> }
>
[quoted text clipped - 14 lines]
> }
> }
See above.
> private void jbInit() throws Exception {
> this.setResizable(false);
[quoted text clipped - 6 lines]
> what could be wrong with it... maybe it is the case of event handling??
> any idaes?
http://java.sun.com/docs/books/tutorial/uiswing/
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
mgdz - 19 Jul 2005 09:46 GMT
Thank you :), your coments hepled me a lot.