Create Components automatically

Hello m8s!

Following problem: I want to create more components during the run of my program.

More details:

- My program is able to let the user choose a file. This can be done via one JTextField or one JButton("browse").

- Sometimes the user is allowed to choose more files after he choosed the first file

- In that case i want to create automatically a new Compoment (e.g. "browsePanel")

- But i want that all elements in it (JTextField and JButton) get their FocusListener,ActionListener and MouseListener.

- How can this be done?

- How can i use that component in my Listeners? (e.g. if(e.getSource() == browseButton))

- I cant use that code for components i dont know their Fieldname, etc...

Thanks for your help! And if you have question and did not understand my problem, please ask ;)

Greetings

Martin

Message was edited by:

ErlemanM

[933 byte] By [ErlemanMa] at [2007-11-26 19:19:39]
# 1
[bump]nobody knows the answer?where is the allknown "nobody"? ;)[/bump]
ErlemanMa at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

> [bump]

> nobody knows the answer?

>

> where is the allknown "nobody"? ;)

> [/bump]

I follow you till this point:

How can this be done?

What be done? Be specific.

- How can i use that component in my Listeners? (e.g. if(e.getSource() == browseButton))

What is keeping you from using it? (You know you can have multiple ActionListeners that listen to individual buttons, rather than chaining if statements).

- I cant use that code for components i dont know their Fieldname, etc...

Here is an example of a component that is used and has no field name:

JPanel panel = new JPanel();

panel.add(new JTextField());

zadoka at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

hmmm, my problem is the following:

- i want a function that returns me a JPanel that includes two components (JTextField and JButton).

- and i want that those included components have their specific actionlistener, because i need to control both components specific

- something like this will i need

if(userChoosedFilePath() &&userIsAllowedToChooseMoreFiles())

createBrowseField()

sure that useChoosedFilePath() will be handled with an extension of ActionHandler that checks for actions on the JTextField and JButton.

Hopefully that helps?

Sorry for my primitive english ;) Im doing my best...

Martin

Message was edited by:

ErlemanM

null

ErlemanMa at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 4

> hmmm, my problem is the following:

>

> - i want a function that returns me a JPanel that

> includes two components (JTextField and JButton).

> - and i want that those included components have

> their specific actionlistener, because i need to

> control both components specific

> - something like this will i need

I am following you so far.

> [code]

> if(userChoosedFilePath() &&

>userIsAllowedToChooseMoreFiles())

> createBrowseField()

>

>

> sure that useChoosedFilePath() will be handled with

> an extension of ActionHandler that checks for actions

> on the JTextField and JButton.

I think I understand what you want to do. It sounds possible but I don't see a question?

> Hopefully that helps?

>

> Sorry for my primitive english ;) Im doing my

> best...

Your English is fine, I just don't see what your Java question is specifically. Maybe posting an example or a pseudocode version of what you want to happen.

zadoka at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 5

hmm, i thought i did :)

ok, another try:

//pseudoCode of PseudoMain.java

public void createRightPanel()

{

// some other JPanels and JTextFields

//adding browsePanel to a Field called "rightPanel"

rightPanel.add(createBrowsePanel(),BorderLayout.SOUTH);

}

public Component createBrowsePanel()

{

//i thought about a custom class "BrowsePanel"

BrowsePanel bp = new BrowsePanel();

JPanel browsePanel = bp.getBrowsePanel();

//browseField and browseButton are Fields in PseudoMain.java

browseField = (JTextField) browsePanel.getComponent(0);

browseButton = (JButton) browsePanel.getComponent(1);

// adding Listeners (contained in same Class PseudoMain.java) to my Components

browseField.addMouseListener(new MyMouseListener());

browseField.addFocusListener(new MyFocusListener());

browseButton.addActionListener(new MyActionListener());

}

//

my Listener Classes check for following:

if(browseFieldContainsExistingFile() || UserChoosedFileByBrowseButton() && userIsAllowedToChooseMoreFiles())

{

// adding another BrowsePanel to my righPanel with all Listeners to it...

rightPanel.add(createBrowsePanel(),BorderLayout.SOUTH);

}

ErlemanMa at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 6

//class BrowsePanel.java

public class BrowsePanel extends JPanel

{

private JTextField browseField;

private JButton browseButton;

public BrowsePanel()

{

}

public BrowsePanel getBrowsePanel()

{

setLayout(new BorderLayout());

Icon icon = new ImageIcon(System.getProperty("user.dir")+"/icons/searchtopic.gif");

browseButton = new JButton("browse",icon);

browseButton.setEnabled(false);

browseField = new JTextField();

browseField.setEditable(false);

add(browseField,BorderLayout.CENTER);

add(browseButton,BorderLayout.LINE_END);

return this;

}

}

blub

ErlemanMa at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 7
I still can't find a question? I understand what you want to do, but I don't know what you are asking? What is your specific question?
zadoka at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 8

ok, if i have more than one browsePanels added to my rightPanel; how can i then communicate to all components?

at the moment my listeners work like this:

public class MyFocusListener implements FocusListener

{

public void focusGained(FocusEvent e)

{

// TODO Auto-generated method stub

}

//here is my problem: When i create more BrowsePanels i cannot talk to the previous browseField

//(or browsebutton) because they are overwritten by my "createBrowsePanel()" function.

public void focusLost(FocusEvent e)

{

if(e.getSource() == browseField)

if(new File(browseField.getText()).exists())

{

addFileToList();

if(UserIsAllowedToChooseMoreFiles)

createBrowsePanel();

}

}

}

maybe i have to add to browsePanel a specific SerialNumber to identify a component?!

ErlemanMa at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 9
Well, what about having your createBrowsePanel method add each browse panel to a list. Then you can iterate through them when you need to.
zadoka at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...
# 10
hmm, ok i磍l try your solution :)thanks for your help!
ErlemanMa at 2007-7-9 21:36:32 > top of Java-index,Desktop,Core GUI APIs...