Re: Creating a component by putting together others




Ricardo Palomares Martinez <rpm.PUBLI@xxxxxxxxxx> writes:

There is somewhat usual to have a JTextField for the user to type a
pathname, and a JButton next to it to launch a Choose File Dialog. In
fact, in the app I'm writing is going to be usual that I'd rather
create a custom component to tie both fields together.

But I'm not sure how to do that. I've thought of putting both
components (JTextField and JButton) inside a JPanel, and thus I would
write my custom component by extending a JPanel with two private
properties holding the text field and the button. Would that be
sensible or should I extend JComponent (which I think is more complex)?

That is quite sensible. Extending JPanel is probably the easiest way to
do this, as you observe. The only thing that can get tricky if you go
this route is that if you have multiple instances of this, you might
find it tricky to make sure the layout aligns properly if each one lays
out differently. Particularly if there is an associated label, which
may be a different size in each item.

You also want to make sure you have reasonable resizing behavior in the
JPanel, which can probably be achieved most easily with BorderLayout
with the text field in the center, with label left and button right.

Note that you may also want to include a JLabel to identify what
pathname this is supposed to be.


--
Thomas A. Russ, USC/Information Sciences Institute
.



Relevant Pages

  • Re: How do i know which superclass to inherit from...
    ... class that extends nothing and constructs the elaborate JFrame. ... This is a pane, so it can be included somewhere in a dialog, so it must be a JPanel (which is my reasoning for extending JPanel: it's ultimately a JComponent, and JPanel is the most convenient for me to extend). ... I make my panes components because I see them as atomic, indivisible visual areas which are logically connected and can be independently used and manipulated. ...
    (comp.lang.java.programmer)
  • Re: CardLayout works, but "cards" randomly disappear
    ... private void exitForm(java.awt.event.WindowEvent evt) { ... JPanel pBG = new JPanel; ... JButton pBtn = new JButton; ...
    (comp.lang.java.programmer)
  • Re: MyPanel extends JPanel is not displayed
    ... > I want to write a buttonbar which inherits from JPanel. ... public class WidgetPanel extends JPanel{ ... JButton forward = null; ... // now add the components directly to the panel. ...
    (comp.lang.java.gui)
  • Re: How many warnings is too many?
    ... >> there is no point extending JPanel. ... >> JComponent is absurd. ... in extending if you're implementing a true custom component. ...
    (comp.lang.java.programmer)
  • Re: Newbie question
    ... > The extending style I think is less error prone. ... > keep specifying which panel you mean over and over. ... JPanel. ... "MyEmailProgram ...
    (comp.lang.java.gui)