swing design questions

C

conrad

1) I want to have a menu bar and options selected from
the menu bar will change the content pane. Would the
ideal way of doing this be as follows: for the menu item
selected add the necessary componenets to the content
pane for that menu item, and when finished just remove
those components from the content pane.

The alternative methods to this seem to be: just use
multiple windows or use internal frames. Again, each
menu item will correspond to a unique interface.
As such, what method is typically followed? From
a design perspective, is one of these methods
typically held as being a cleaner solution over the
others?

2) In terms of populating your content pane with
swing components, I've seen the method of
following a has-a relationship. You might
start out with a class that creates a panel and
adds some buttons. Another class, say for
creating a check box, would extend your
panel/button class. In the case from 1) above,
where a menu system creates a branching
effect for different kinds of interfaces(content
panes with different components that are removed
when a different menu item is selected), what kind
of design methodology should be followed? It
doesn't seem like the above panel/button <-- checkbox
containment example would work here.

I hope that was clear.
 
J

John B. Matthews

conrad said:
1) I want to have a menu bar and options selected from
the menu bar will change the content pane. [...]
2) In terms of populating your content pane with
swing components, I've seen the method of
following a has-a relationship.
[...]

You might look at the approach taken in SwingSet2, which uses a JToolBar
of JToggleButtons to replace the content pane of a JTabbedPane:

<http://www.java2html.com/examples/SwingSet2_demo/SwingSet2.java.html>

The drag feature of JToolBar is especially appealing.

<http://java.sun.com/docs/books/tutorial/uiswing/components/toolbar.html>
 
M

Mark Space

conrad said:
1) I want to have a menu bar and options selected from
the menu bar will change the content pane. Would the
ideal way of doing this be as follows: for the menu item
selected add the necessary componenets to the content
pane for that menu item, and when finished just remove
those components from the content pane.

This is the way I would start. If the items to be added are fairly
simple, this is this almost certainly the easiest way to go.

If your components are more complex, the second ways seems attractive.
Create the objects one, keep a reference to them, and then just swap
them into the content pane when you need them. This way also might be
more amenable to using a GUI layout editor like Matisse (which comes
with NetBeans).


2) In terms of populating your content pane with
swing components, I've seen the method of
following a has-a relationship. You might
start out with a class that creates a panel and
adds some buttons. Another class, say for
creating a check box, would extend your

You started out good, then lost me. I'm not sure that "class" is the
right level of decomposition here. Methods work fine. It's also a bit
odd to me that a whole class would be devoted just to a checkbox.
Although, if you had a very specialzied sort of checkbox, I guess it
might be worth while.
panel/button class. In the case from 1) above,
where a menu system creates a branching
effect for different kinds of interfaces(content
panes with different components that are removed
when a different menu item is selected), what kind
of design methodology should be followed? It
doesn't seem like the above panel/button <-- checkbox
containment example would work here.

Again I'm not following. A simple example might help. Could you post
up an SCCEE? I don't see anything wrong with adding a checkbox to
panel, but the whole "checkbox needs a second class" thing has me confused.

<http://www.pscode.org/sscce.html>
 
C

conrad

This is the way I would start.  If the items to be added are fairly
simple, this is this almost certainly the easiest way to go.

If your components are more complex, the second ways seems attractive.
Create the objects one, keep a reference to them, and then just swap
them into the content pane when you need them.  This way also might be
more amenable to using a GUI layout editor like Matisse (which comes
with NetBeans).


You started out good, then lost me. I'm not sure that "class" is the
right level of decomposition here.  Methods work fine. It's also a bit
odd to me that a whole class would be devoted just to a checkbox.
Although, if you had a very specialzied sort of checkbox, I guess it
might be worth while.


Again I'm not following.  A simple example might help.  Could you post
up an SCCEE?  I don't see anything wrong with adding a checkbox to
panel, but the whole "checkbox needs a second class" thing has me confused.

<http://www.pscode.org/sscce.html>

Adapted from Introduction to Java Programming by
Y. Daniel Liang:

ButtonDemo.java:

public class ButtonDemo extends JFrame {
protected MessagePanel messagePanel =
new MessagePanel("Welcome to Java");

private JButton jbtLeft = new JButton("<=");
private JButton jbtRight = new JButton("=>");

public static void main(String[] args) {
ButtonDemo frame = new ButtonDemo();
/* init frame stuff */
frame.setVisible(true);
} // main

public ButtonDemo() {
messagePanel.setBackground(Color.White);

// Create Panel to hold JButtons
// omitted for brevity

// Set layout
// omitted for brevity

// register listeners
// omitted for brevity
} // Constructor

} // ButtonDemo


CheckBoxDemo.java:

public class CheckBoxDemo extends ButtonDemo {
private JCheckBox jchkCentered = new JCheckBox("Centered");
private JCheckBox jchkBold = new JCheckBox("Bold");
private JCheckBox jchkItalic = new JCheckBox("Italic");

public static void main(String[] args) {
CheckBoxDemo frame = new CheckBoxDemo();
/* init frame - omitted for brevity */
frame.setVisible(true);
} // main

public CheckBoxDemo() {
// Set mnemonic keys
// omitted for brevity

// Create Panel to hold check boxes
// omitted for brevity

// register listeners
// omitted for brevity
} // Constructor

} // CheckBoxDemo


The author continues with this method a step further
and extends CheckBoxDemo with a RadioButtonDemo.
 
C

conrad

conrad said:
Adapted from Introduction to Java Programming by
Y. Daniel Liang:

public class ButtonDemo extends JFrame {
  protected MessagePanel messagePanel =
    new MessagePanel("Welcome to Java");
  private JButton jbtLeft = new JButton("<=");
  private JButton jbtRight = new JButton("=>");
  public static void main(String[] args) {
    ButtonDemo frame = new ButtonDemo();
    /* init frame stuff */
    frame.setVisible(true);
  } // main
  public ButtonDemo() {
    messagePanel.setBackground(Color.White);
    // Create Panel to hold JButtons
    // omitted for brevity
    // Set layout
    // omitted for brevity
    // register listeners
    // omitted for brevity
  } // Constructor
} // ButtonDemo

public class CheckBoxDemo extends ButtonDemo {
  private JCheckBox jchkCentered = new JCheckBox("Centered");
  private JCheckBox jchkBold = new JCheckBox("Bold");
  private JCheckBox jchkItalic = new JCheckBox("Italic");
  public static void main(String[] args) {
    CheckBoxDemo frame = new CheckBoxDemo();
    /* init frame - omitted for brevity */
    frame.setVisible(true);
  } // main
  public CheckBoxDemo() {
    // Set mnemonic keys
    // omitted for brevity
    // Create Panel to hold check boxes
    // omitted for brevity
    // register listeners
    // omitted for brevity
  } // Constructor
} // CheckBoxDemo
The author continues with this method a step further
and extends CheckBoxDemo with a RadioButtonDemo.

That doesn't answer Mark Space's question, AFAICT.  He was asking about "a
whole class [that] would be devoted just to a checkbox."  You show a class
that contains not just a checkbox but a JFrame, mnemonics, [J]Panels,
listeners, /ad infinitum/.

I was clarifying what I was originally
asking about in terms of design. Is this
a common approach when building GUI
applications in Java?

In terms of my first solution proposed,
a menu item corresponding to a unique
content pane, it doesn't seem like such
an approach is doable.

Maybe I need to think about it more.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top