JavaBeans: How to encanpsulate GUI in Beans?

A

asd

Hello all,

I have a question for my home work.

I have to do a java beans component for the homework.
I am planning to do something like a list box that lists contents of a
directory.

The problem is that, I want to encapsulate the GUI components in the
beans. I want to do it as follows.

class DirLister {
private JList listBox;
private JScrollpane scroll;
}


When I do it like this the listBox and its contents don't show up on the
screen.

Is there a way to make the GUI contents of a bean private and make them
visible on the screen?
Thank you so much for your time.
 
R

RedGrittyBrick

asd said:
Hello all,

I have a question for my home work.

I have to do a java beans component for the homework.
I am planning to do something like a list box that lists contents of a
directory.

The problem is that, I want to encapsulate the GUI components in the
beans. I want to do it as follows.

class DirLister {
private JList listBox;
private JScrollpane scroll;
}


When I do it like this the listBox and its contents don't show up on the
screen.

Is there a way to make the GUI contents of a bean private and make them
visible on the screen?

Presumably you mean something like this?

class DirLister extends JPanel {
private JList listBox;
private JScrollpane scroll;
DirLister() {
listBox = new JList();
// populate list here
scroll = new JScrollPane(listBox);
add(scroll);
}
}

Then you'd instantiate your bean and add it to a JFrame's contentPane
(or do the equivalent for an applet).
 
A

asd

Presumably you mean something like this?

class DirLister extends JPanel {
private JList listBox;
private JScrollpane scroll;
DirLister() {
listBox = new JList();
// populate list here
scroll = new JScrollPane(listBox);
add(scroll);
}
}

Then you'd instantiate your bean and add it to a JFrame's contentPane
(or do the equivalent for an applet).


Thank you so much for your help RedGrittyBrick. I was looking for a way to
do it without extending any other class related to GUI.
Thanx again.
asd
 
R

RedGrittyBrick

asd said:
Thank you so much for your help RedGrittyBrick. I was looking for a way to
do it without extending any other class related to GUI.

Presumably you mean something like this:

class DirLister {
private JPanel panel;
private JList listBox;
private JScrollpane scroll;
DirLister() {
listBox = new JList();
// populate list here
scroll = new JScrollPane(listBox);
panel.add(scroll);
}
public JPanel getJPanel() {
return panel;
}
}

Instantiate DirLister then call it's getJPanel() to add the result to a
JFrame.
 
A

asd

Presumably you mean something like this:

class DirLister {
private JPanel panel;
private JList listBox;
private JScrollpane scroll;
DirLister() {
listBox = new JList();
// populate list here
scroll = new JScrollPane(listBox);
panel.add(scroll);
}
public JPanel getJPanel() {
return panel;
}
}

Instantiate DirLister then call it's getJPanel() to add the result to a
JFrame.

Thank you so much RedGrittyBrick. Unfortunately I dont know how to make
BDK call getJPanel. I just drag and drop them to the screen. The default
constructor gets called and it pops up on the screen. How do we specify
which component should be drawn on the screen?

In addition to that, I was following your advice by using JPanel. Now
Everything works fine. Unfortunately when the bean is resized on the BDK.
Only the size of The panel Changes. How can i change the size of the JList
when the size of the JPanel in Bean changes?
Thank you so much for your help and time.
Sincerely yours.
 
R

RedGrittyBrick

asd said:
Thank you so much RedGrittyBrick. Unfortunately I dont know how to make
BDK call getJPanel. I just drag and drop them to the screen.

BDK? Sounds like you are hampered by a bean-oriented development tool
which is preventing you from learning some Java fundamentals.
The default
constructor gets called and it pops up on the screen. How do we specify
which component should be drawn on the screen?

I'm not familiar with this BDK, I don't use a visual forms designer. I
do stuff like this:

class Foo extends JPanel {
Foo() {
add(new JLabel("See - not so hard!");
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Foo());
f.setVisible(true);
}
}
In addition to that, I was following your advice by using JPanel. Now
Everything works fine. Unfortunately when the bean is resized on the BDK.
Only the size of The panel Changes. How can i change the size of the JList
when the size of the JPanel in Bean changes?

By using a layout manager. Try
setLayoutManager(new BorderLayout());
...
add(scroll, BorderLayout.CENTER);
 
A

asd

asd said:
Thank you so much RedGrittyBrick. Unfortunately I dont know how to make
BDK call getJPanel. I just drag and drop them to the screen.

BDK? Sounds like you are hampered by a bean-oriented development tool
which is preventing you from learning some Java fundamentals.
The default
constructor gets called and it pops up on the screen. How do we specify
which component should be drawn on the screen?

I'm not familiar with this BDK, I don't use a visual forms designer. I
do stuff like this:

class Foo extends JPanel {
Foo() {
add(new JLabel("See - not so hard!");
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Foo());
f.setVisible(true);
}
}
In addition to that, I was following your advice by using JPanel. Now
Everything works fine. Unfortunately when the bean is resized on the BDK.
Only the size of The panel Changes. How can i change the size of the JList
when the size of the JPanel in Bean changes?

By using a layout manager. Try
setLayoutManager(new BorderLayout());
...
add(scroll, BorderLayout.CENTER);
Thank you so much for your help and time.
Sincerely yours.

Thank you so much for your help. I am actually good with Java. I am trying
to implement a bean that must run on BDK (Home Work).
I am having problems with BDK. Unfortunately I could not find any useful
information about BDK. There are no specifications for it I guess.
Thank you so much for everything you have done.
Sincerely yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top