Java Swing Design question

S

Sid

Hi All.

To begin with i am pretty new at swing. I have a application wherein
i want to render forms from a MenuBAR, meaning user clicks on Add
record the add form shows up, search.. edit so forth.
What i am doing is i have a single container and in that container i
draw the menubar and set up listeners on the items to render respective
forms. whenever a item is clicked i clear the container and add the
new formpanel.

1. I read that listeners should not be doing too much work as it slows
down the response time, so is there any better way to render these
individual forms.

2. Going with the same design i needed a Scrollpane to my container and
in turn to the forms, I want to add the Scrollpane to all forms so it
makes sense to add it to the container but when i do the forms dont
clear out anymore and the same welcome form is on the screen.

Pseudo code

Class A
{
private container c;
FormInterface formInterface;
Panel panel;
JScrollpane ps;

public A() {
c = getContentPane();
createMenuBar() // heres where the menu comes and action listeners
called.
formInterface = new CompanyInfoForm();
panel = formInterface.renderForm();
ps = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

c.add(ps);
}


Sample action listener

public void AddRecordListener()
{
System.out.println("Add record called");

if (panel != null)
{

FormPanel.removeFromParent(panel);
c.remove(ps);
System.out.println("removing panel ...");
}
// Create a new form
formInterface = new RegistrationForm(connection);
panel = formInterface.renderForm();
// Add to Scrollpane
ps.add(panel)
c.add(ps) ;

}

The problem here is w/o the scrollpane i can switch between forms just
fine its only with the pane i cannot render forms, any help/pointers
be greatly appreciated..


Thanks much
Sid.
 
Y

Yamin

Question 1
Unless you're doing some crazy long calculation or accessing a
network/database, don't worry about code in the listener. You'll just
end up complicating the code for no reason. Even if you do, depending
on the application and its target audience, you might just say 99.99%
of the time this operation will take less than 1 second...i'll just
block the GUI. Its can be okay for 'load' or 'submit' forms type of
operations. Obviously using multiple threads is the proper way to do
this.

Question 2.
First of all, you really only have 1 container that is changing. Since
you add the scroll pane (ps) to the container on init, you don't need
to add it again. Your listener should really only need to know about
the scroll pane and nothing else.

// Create a new form
formInterface = new RegistrationForm(connection);
panel = formInterface.renderForm();
//remove old form
ps.removeAll()
// Add to Scrollpane
ps.add(panel)

Not sure if that's the problem though.

Yamin
 
T

Thomas Weidenfeller

Sid said:
To begin with i am pretty new at swing. I have a application wherein
i want to render forms from a MenuBAR, meaning user clicks on Add
record the add form shows up, search.. edit so forth.

Please consider adapting general GUI terminology, as it is used
throughout Java instead of Windows-specific terminology.
What i am doing is i have a single container and in that container i
draw the menubar

You do not draw the menubar. You just add a menu bar to your top-level
container. Swing will lay out and draw the bar for you.
and set up listeners on the items to render respective
forms. whenever a item is clicked i clear the container and add the
new formpanel.

Sounds like you want to give CardLayout a look.
1. I read that listeners should not be doing too much work as it slows
down the response time, so is there any better way to render these
individual forms.

Listeners run in the EDT and indeed if you occupy the EDT other events
are not processed. This becomes relevant if it annoys the user at that
certain moment, e.g. when the user wants to interact with the GUI or the
GUI is not updated in a timely fashion. So, this is the case for your
particular application (I doubt it), you need to run these activities in
a separate thread. See Sun's Swing tutorial (a pointer to it, and many
other resources can be found via the link in my signature).
2. Going with the same design i needed a Scrollpane to my container and
in turn to the forms, I want to add the Scrollpane to all forms so it
makes sense to add it to the container but when i do the forms dont
clear out anymore and the same welcome form is on the screen.

Please consider reformulating that statement in an easy to understand form.
Pseudo code

Please provide real code (look up the meaning of SSCCE in the document
linked in my sig).

/Thomas
 
S

steve

Hi All.

To begin with i am pretty new at swing. I have a application wherein
i want to render forms from a MenuBAR, meaning user clicks on Add
record the add form shows up, search.. edit so forth.
What i am doing is i have a single container and in that container i
draw the menubar and set up listeners on the items to render respective
forms. whenever a item is clicked i clear the container and add the
new formpanel.

1. I read that listeners should not be doing too much work as it slows
down the response time, so is there any better way to render these
individual forms.

2. Going with the same design i needed a Scrollpane to my container and
in turn to the forms, I want to add the Scrollpane to all forms so it
makes sense to add it to the container but when i do the forms dont
clear out anymore and the same welcome form is on the screen.

Pseudo code

Class A
{
private container c;
FormInterface formInterface;
Panel panel;
JScrollpane ps;

public A() {
c = getContentPane();
createMenuBar() // heres where the menu comes and action listeners
called.
formInterface = new CompanyInfoForm();
panel = formInterface.renderForm();
ps = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

c.add(ps);
}


Sample action listener

public void AddRecordListener()
{
System.out.println("Add record called");

if (panel != null)
{

FormPanel.removeFromParent(panel);
c.remove(ps);
System.out.println("removing panel ...");
}
// Create a new form
formInterface = new RegistrationForm(connection);
panel = formInterface.renderForm();
// Add to Scrollpane
ps.add(panel)
c.add(ps) ;

}

The problem here is w/o the scrollpane i can switch between forms just
fine its only with the pane i cannot render forms, any help/pointers
be greatly appreciated..


Thanks much
Sid.

This is a job for "cardlayout" , forget trying to mod your existing forms.
setup 1 card layout for each of your menu options, then just flip between the
cards, based on the user options.

This should fairly much cover you for any changes you want to make in hte
future.


steve
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top