0

I am writing a program with JTabbed Panes. I want to have a button that gets some text from a JTextField, uses that text to create a new tab. The new tab will contain the name = Jtextfield.getText(), and it will print it in a JTextArea 10 times.

My real question is, how do I pass some data to an ActionListener for a button, or how would I go around that, because it is causing me trouble?

UPDATE:

Here is my attempt to solve this. I know the code is complicated, I'm a horrible programmer. I hope that you can help me see what is going wrong here. I am afraid that my CreateComponent method and the CreateNewFeedBlock actionListener are going wrong.

Here is some of my code, please bear with me.

private static JPanel mainPane;    
private JPanel categoryPanel; 
private JTextField categoryName;
private JPanel panel1;
private JTextField title1;
private JTextField url1;
private JPanel panel2;
private JTextField title2;
private JTextField url2;
private JPanel panel3;
private JTextField title3;
private JTextField url3;
private JPanel panel4;
private JTextField title4;
private JTextField url4;

public BIS() 
{
//unecessary code
categoryPanel = new JPanel();
categoryPanel.setLayout(new GridLayout(2,2));
JLabel emptyLabel1 = new JLabel("");
JLabel emptyLabel2 = new JLabel("");
JLabel insertCategoryName = new JLabel("Category name:");
categoryName = new JTextField(20);

panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,2));
JLabel insertTitle1 = new JLabel("Feed Title:");
title1 = new JTextField(20);
JLabel insertURL1 = new JLabel("URL:");
url1 = new JTextField(20);
panel1.add(insertTitle1);
panel1.add(title1);
panel1.add(insertURL1);
panel1.add(url1);




categoryPanel.add(emptyLabel1);
categoryPanel.add(emptyLabel2);
categoryPanel.add(insertCategoryName);
categoryPanel.add(categoryName);

JPanel addPanel = new JPanel(new GridLayout(6,1));
JButton addButton = new JButton("Create Your RSS Show");

addPanel.add(categoryPanel);
addPanel.add(panel1);
addPanel.add(addButton);
addPanel.setBorder(BorderFactory.createEmptyBorder(64, 64, 64, 64));

addButton.addActionListener(new CreateTab());

pane.add("Create Your Own", addPanel);

Timer timer = new Timer(15000, new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
allRun();
//System.out.println("U BO NIHER");
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
timer.restart();
}


public class CreateTab implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

panel = new JPanel();
while (!url1.getText().contains("http://"))
{
JOptionPane.showMessageDialog(panel, "The RSS Feed Link does not contain http//.");
url1.setText(JOptionPane.showInputDialog("Please enter the full URL to the RSS Feed?"));
}


rssListForTab = new ArrayList<rssInfo>();

rssInfo feedInfo = new rssInfo(title1.getText(), url1.getText());

rssListForTab.add(feedInfo);
rssListForAll.add(rssListForTab);

final Component newTab = createComponent(panel, rssListForTab);
pane.add(categoryName.getText(), newTab);


// newTab.get
// newTab.getComponent(2);

categoryName.setText("");
title1.setText("");
url1.setText("");

}
}

private JComponent createComponent(JPanel panel, ArrayList<rssInfo> rssList)
{
JLabel t1 = new JLabel(title1.getText(), JLabel.CENTER);
JTextArea u1 = new JTextArea(5,5);
JScrollPane u1Scroll = new JScrollPane(u1);
u1Scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
u1Scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
u1.setEditable(false);
u1.setFont(new Font("Tahoma", Font.BOLD, 12));
u1.setLineWrap(true);
u1.setWrapStyleWord(true);

JButton addFeedButton = new JButton("Add Feed");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

panel.add(addFeedButton);
panel.add(t1);
panel.add(u1Scroll);
panel.setSize(1,1);
panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));

addFeedButton.addActionListener(new createNewFeedBlock());
return panel;
}

public class createNewFeedBlock implements ActionListener  
{
public void actionPerformed(ActionEvent e)
{
rssInfo newFeedInfo = DisplayDialog();

JLabel t2 = new JLabel(newFeedInfo.getTitle(), JLabel.CENTER);
JTextArea u2 = new JTextArea(5,5);
JScrollPane u2Scroll = new JScrollPane(u2);  
u2Scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
u2Scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
u2.setEditable(false);
u2.setFont(new Font("Tahoma", Font.BOLD, 12));   
u2.setLineWrap(true);
u2.setWrapStyleWord(true);

rssListForTab.add(newFeedInfo);

panel.add(t2);
panel.add(u2Scroll);

}
}

I think that the problem consists inside the CreateNewFeedBlock and/or createComponent method. I know this code is very hard to read, but please see if you can help me somehow on finding what is going wrong.

Please look at anything that you see wrong and just tell me, I am doing my best to solve this but I'm helpless. I hope that your help will take me through this challenge.

UPDATE**

This code is not working properly, whenever I click addFeedButton, instead of adding one feed to the corresponding tab where it is being invoked from, it adds one feed to all the tabs in the pane.

I am pretty sure it is a variable scope problem, could somebody help me find a way around it? Thank you!

Don Code
  • 781
  • 3
  • 12
  • 25
  • *"pass some data to an ActionListener for a button, or how would I go around that"* You just need to make sure the component with text is visible within the scope of the `ActionListener`. – Andrew Thompson Dec 02 '12 at 12:19
  • 1
    [whathaveyoutried](http://whathaveyoutried.com)? – kleopatra Dec 02 '12 at 12:51
  • @kleopatra I posted some code of my attempt to do this. I know it's complicated but it's the best of me, I'm not a very good programer. I hope you can help me spot the bugs. thanks – Don Code Dec 02 '12 at 13:55

1 Answers1

0

When the Click-Action is invoked, the ActionListener gets a reference to the causing Object passed to it as part of the ActionEvent-Object. So in the Event-handling function you can use eg:

public void actionPerformed(ActionEvent e) { 
    JButton sourceButton = (JButton)e.getSource();
}

If you want to reference to the TextField from inside the function, eigther you try to address it directly (easy if the actionHandler is defined within the same class as the Objects) or you create your own custom button-class that includes a reference to the textfield (can be useful if you have actionhandlers defined seperately/in a parent-class) eg:

class MyTabButton extends JButton{
   private JTextField tabTextField;
   public MyTabButton(JTextField text){
      tabTextField = text;
   }
   public JTextField getTabTextField(){
      return tabTextField;
   }
   //... someOtherStuff
}

then you would get it in the actionHandler by

public void actionPerformed(ActionEvent e) { 
    MyTabButton sourceButton = (MyTabButton)e.getSource();
    String text = sourceButton.gettabTextField().getText();
}
Legionair
  • 423
  • 3
  • 8
  • 1
    rarely a good idea to extend a JSomething :-) Instead, implement the Action/Listener to take the reference. – kleopatra Dec 02 '12 at 12:36
  • @kleopatra: why a bad idea? I'm using this quite often, to make the structure of my program simpler (especially when having many different dynamically generated Interfaces), never had any problems with it... – Legionair Dec 02 '12 at 12:40
  • 1
    Subclassing is only for_extending functionality, here functionality inherently related to its _button-ness_. The requirement (having a target), is related to the _action-ness_ - would be the same for a variety of components - so the best placed to stick it in is ... the action :-) As a general rule: never-ever subclass any JSomething, they are designed to be used as-are. – kleopatra Dec 02 '12 at 12:43
  • @Legionair when you say some other stuff, in the MytabButton class, what goes there, i have never defined something like that? – Don Code Dec 02 '12 at 12:50
  • @kleopatra: never heard about this rule yet, but the action-ness makes sense, never thought about it like that yet ;-) – Legionair Dec 02 '12 at 12:56
  • @theSun: basically whatever other functionality you want to implement there, for this simple example the written code is enough.. but as kleopatra pointed out, it might be better to actually look into her suggestions – Legionair Dec 02 '12 at 13:05
  • @Legionair do you think you could take a look at my code and maybe tell me what is going wrong??? From my observation, the CreateTab is working fine. Whenever I say addFeedButton (which is the CreateComponent and CreateNewFeedBlock) I add new components to different tabs. Inconsistently. For example, if my tab 2 is focused, and i say add feed, it will add a feed in tab 3, but not in 2. I think that it has something to do with the scope of my variables. – Don Code Dec 02 '12 at 14:23