Re: swing design questions
conrad wrote:
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
Mark Space wrote:
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.
conrad wrote:
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.
Mark Space wrote:
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>
conrad wrote:
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.
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/.
--
Lew