Results 1 to 9 of 9
  1. #1
    ankit1801 is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default creating a frame on button click event

    i hav wriiten a program in whch certain data is displayed on a text field on a button click event. now i hav been asked to remove the textfield and instead of that, display same data in another frame which should appear when the button is clicked. how do i do that??????

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    You can create a new JFrame and add the textfield or a label with the data to that
    Java Code:
    JFrame frame = new JFrame();
    frame.setVisible(isVisible);
    frame.setSize(width, height);
    frame.add(component);
    or use a message dialog

    Java Code:
    JOptionPane.showMessageDialog(parentComponent, message, title, typeOfMessage);
    JOptionPane.showMessageDialog(parentComponent, message);

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Bad advice.

    1. No need to add a component after, instead of before, making the top level window visible. And if done, requires revalidation of the component hierarchy.

    2. pack is preferred to setSize unless otherwise mandated by the requirement.

    db

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Agreed with you db.
    sanjeev,संजीव

  5. #5
    ankit1801 is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    hey thnx but i am a bit confused about in which part shld i write the code for the 2nd frame. before the class in which i hav written the code for 1st frame, or create a subclass in the same class or shld i write it aftr that class???

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please avoid non-standard abbreviations in your posts as you're making it hard to read. Can you elaborate on your current problem a bit and use standard English in the reply? Also, why are you using multiple JFrames rather than either dialogs or swapping views via a CardLayout?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  8. #8
    ankit1801 is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    sorry for the use of non-standard abbrevations. i am new here. Posting the same again with correction. will be carefull later

    i am pasting my code. the output is seen in a TextField. i am asked to remove the textfield and show the output in a new frame which should appear when user presses the VIEW button. i am unable to do so. Please help
    CODE:

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.regex.*;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.JFileChooser;
    
    class Reaction
    {
    	String reaction_id;
    	ArrayList reactants = new ArrayList();								// creating array list to store reactants and products
    	ArrayList products = new ArrayList();
    	ArrayList modifiers = new ArrayList();
    
    	public void setReactionID(String reaction_id){
    		this.reaction_id = reaction_id;
    	}
    	public void addModifier(String modifier){
    		modifiers.add(modifier);
    	}
    	public void addReactant(String reactant) {
    		reactants.add(reactant);
    	}
    
    	public void addProduct(String product) {
    		products.add(product);
    	}
    
    	@Override public String toString(){							//to store information about  reaction
    
    		String to_return;
    		to_return = "\n\nReaction ID: " + reaction_id + "\n\n";
    
    		System.out.println("Reaction ID: " + reaction_id);
    		//display all reactants of the reaction
    		for(int i = reactants.size(); i > 0; i--) {
    			to_return = to_return + "\tReactant: "+ (String)reactants.get(i-1) + "\n";
    			System.out.println("Reactant: "+ (String)reactants.get(i-1));
    		}
    		//display modifiers
    		for(int i=modifiers.size();i>0;i--){
    			to_return = to_return + "\tModifier: " + (String)modifiers.get(i-1) + "\n";
    			System.out.println("Modifier : " + (String)modifiers.get(i-1));
    		}
    
    		//display all products of reaction
    		for(int i = products.size(); i > 0; i--) {
    			to_return = to_return + "\tProduct: " + (String)products.get(i-1) + "\n";
    			System.out.println("Product: " + (String)products.get(i-1));
    		}
    
    		return to_return;
    	}
    }
    public class Test extends JFrame						//creation of interface
    {
    	JPanel panel = new JPanel();
    	JPanel panel2 = new JPanel();
    	JButton view = new JButton("View");
    	JTextField filepath = new JTextField(20);
    	JButton browse = new JButton("Browse");
    	JTextArea textarea = new JTextArea(80,80);
    	JScrollPane scrollpane = new JScrollPane(textarea);
    	BufferedReader br;
    	ArrayList reactions;
    	JFileChooser filechooser;
    
    	public Test() {										//constructor of class test
    		super("Project");
    		panel.add(browse);
    		panel.add(filepath);
    		panel.add(view);
    		panel.add(scrollpane);
    
    		//add events for browse button
    		browse.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent ae)
    			{
    				filechooser = new JFileChooser();					//adding file chooser
    				filechooser.showOpenDialog(new JFrame());
    				filepath.setText(filechooser.getSelectedFile().getAbsolutePath());
    				String f_name=filechooser.getSelectedFile().getAbsolutePath();
    				File file = new File(f_name);
    				filechooser.addChoosableFileFilter(new TextFilter());
    
    
    			}
    
    
    		});
    
    		view.addActionListener(new ActionListener(){	//add events for view button
    			public void actionPerformed(ActionEvent ae)
    			{
    				processData();
    				displayData();
    			}
    		});
    
    
    
    		getContentPane().add(panel);
    		//getContentPane().add(panel2);
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    
    	public void displayData(){
    		for(int i = reactions.size(); i >0; i--)
    			textarea.append(reactions.get(i-1) + "");
    
    	}
    
    	public void processData(){					//pattern matching
    		try{
    			Reaction reaction = new Reaction();
    			reactions = new ArrayList();
    
    			br = new BufferedReader(new FileReader(new File(filepath.getText())));
    
    			Pattern p_reaction_start = Pattern.compile("<reaction");
    			Pattern p_reaction_end = Pattern.compile("</reaction>");
    			Pattern p_reactant_start = Pattern.compile("<listOfReactants>");
    			Pattern p_reactant_end = Pattern.compile("</listOfReactants>");
    			Pattern p_product_start = Pattern.compile("<listOfProducts>");
    			Pattern p_product_end = Pattern.compile("</listOfProducts>");
    			Pattern p_species = Pattern.compile("<speciesReference");
    			Pattern p_mod = Pattern.compile("<modifierSpeciesReference");
    			Pattern p_modifier_start = Pattern.compile("<listOfModifiers>");
    			Pattern p_modifier_end = Pattern.compile("</listOfModifiers>");
    
    			int first_occurance = 0, next_occurance = 0, flag_reactant = 0, flag_modifier = 0, flag_product = 0;
    			Matcher matcher;
    			String line;
    			while((line = br.readLine()) != null) {
    
    				matcher = p_reaction_start.matcher(line);
    				if(matcher.find()){
    					System.out.println(line);
    					first_occurance = line.indexOf('"');
    					next_occurance = line.indexOf('"', first_occurance + 1);
    					reaction.setReactionID(line.substring(first_occurance + 1, next_occurance));
    				}
    
    				matcher = p_reactant_start.matcher(line);
    				if(matcher.find()) {
    					flag_reactant = 1;
    				}
    
    				matcher = p_reactant_end.matcher(line);
    				if(matcher.find()) {
    					flag_reactant = 0;
    				}
    				matcher = p_modifier_start.matcher(line);
    				if(matcher.find()) {
    					flag_modifier = 1;
    				}
    				matcher = p_modifier_end.matcher(line);
    				if(matcher.find()) {
    					flag_modifier = 0;
    				}
    
    				matcher = p_product_start.matcher(line);
    				if(matcher.find()) {
    					flag_product = 1;
    				}
    
    				matcher = p_product_end.matcher(line);
    				if(matcher.find()) {
    					flag_product = 0;
    				}
    				matcher = p_mod.matcher(line);
    				if(matcher.find() && (flag_modifier == 1))
    				{
    					first_occurance = line.indexOf('"');
    					next_occurance = line.indexOf('"', first_occurance + 1);
    					Pattern pat222 = Pattern.compile("\"");
    					String temp2 = line.substring(first_occurance , next_occurance);
    					String strs1[]= pat222.split(temp2);
    					String temp3 = strs1[1];
    					Matcher mat222 = pat222.matcher(temp2);
    					temp2=mat222.replaceAll("");
    					Pattern pat333 = Pattern.compile("_");
    					Matcher mat333 = pat333.matcher(temp2);
    					temp2=mat333.replaceAll("");
    					Pattern pat444 = Pattern.compile("space");
    					Matcher mat444 = pat444.matcher(temp2);
    					temp2=mat444.replaceAll(" ");
    					temp2=temp2.trim();
    					if(flag_modifier == 1) {
    											reaction.addModifier(temp2);
    					}
    				}
    
    
    				matcher = p_species.matcher(line);
    				if(matcher.find() && (flag_product == 1 || flag_reactant == 1)) {
    					System.out.println(next_occurance);
    					first_occurance = line.indexOf('"');
    					next_occurance = line.indexOf('"', first_occurance + 1);
    					String temp1 = line.substring(first_occurance, next_occurance);
    					Pattern pat22=Pattern.compile("\"");
    					String strs[]=pat22.split(temp1);
    					String temp=strs[1];
    					Pattern pat33=Pattern.compile("_");
    					Matcher mat33=pat33.matcher(temp);
    					temp=mat33.replaceAll("");
    					Pattern pat44=Pattern.compile("minus");
    					Matcher mat44=pat44.matcher(temp);
    					temp=mat44.replaceAll("-");
    					Pattern pat66=Pattern.compile("plus");
    					Matcher mat66=pat66.matcher(temp);
    					temp=mat66.replaceAll("+");
    					Pattern pat55=Pattern.compile("space");
    					Matcher mat55=pat55.matcher(temp);
    					temp=mat55.replaceAll(" ");
    					temp=temp.trim();
    						// differentiating reactants and products
    					if(flag_product == 1) {
    						reaction.addProduct(temp);
    					}
    
    					if(flag_reactant == 1) {
    						reaction.addReactant(temp);
    					}
    				}
    
    				matcher = p_reaction_end.matcher(line);
    				if(matcher.find()) {
    					reactions.add(reaction);
    					reaction = new Reaction();
    				}
    			}
    		}
    		catch(Exception e) {
    			System.out.println("ERROR!");
    		}
    	}
    
    
    	public static void main (String []args)	{
    		new Test();
    	}
    }
    class TextFilter extends FileFilter {
    
    				  public boolean accept(File f) {
    				    if (f.isDirectory())
    				      return true;
    				    String s = f.getName();
    				    int i = s.lastIndexOf('.');
    
    				    if (i > 0 && i < s.length() - 1)
    				      if (s.substring(i + 1).toLowerCase().equals("xml"))
    				        return true;
    
    				    return false;
    			}
    			public String getDescription() {
    			    return "Accepts xml files only.";
      }
    		}

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Click out event
    By rulych in forum AWT / Swing
    Replies: 3
    Last Post: 02-17-2011, 04:43 PM
  2. add tabbed pane to the frame on a button click
    By Mahaveer in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 09:19 AM
  3. Swing - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:03 PM
  4. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM
  5. How to perform some event to button click
    By eva in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •