Results 1 to 5 of 5
  1. #1
    dime111 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default disable button in a fuction

    Hello
    Im very new java in fact not a java programmer but im modifying a source code which i need
    my problem is as follow
    i have two java file each consist of their own classes and functions
    i have a case in which when application start i disable one of the button then if user click on setting tab the second java files is called i want to reenable button in the first java class file again
    sorry if im not very clear

    part of code
    first file which create ui and buttons and i disable jb1 button here
    Java Code:
    	public static void main(final String args[]) {
    		// copy certificate file from jar.
    		final ProxyUIStarter proxyUIStarter = new ProxyUIStarter();
    		final InputStream is = proxyUIStarter.getClass().getResourceAsStream(
    				"/ProxyCertificate.ser");
    		proxyUIStarter.copyFile(proxyUIStarter, is);
    		
    		
    		
    		final String currentDir = new File("").getAbsolutePath();
    		final File file = new File(currentDir + File.separator
    				+ "ProxyConfig.dat");
    		if (!file.exists())
    		{
    			ProxyUIStarter.showSettings();
    		}
    		else
    		{
    			final Settings getvalues = new Settings();
    			getvalues.updateField();
    			authorize tc = new authorize();
    			tc.authorizeing();
    		}
    		
    		final String cerfile = currentDir + File.separator
    				+ "ProxyCertificate.ser";
    
    		try {
    			final PrintStream stdout = new PrintStream(new FileOutputStream(
    					ProxyConstants.LOG_FILE));
    			System.setOut(stdout);
    		} catch (final FileNotFoundException e) {
    			final String currentDir1 = new File("").getAbsolutePath();
    			JOptionPane.showMessageDialog(null,
    					"Could not access LOG file in  : " + currentDir1
    							+ File.separator + "ProxyLog.txt", "Message",
    					JOptionPane.ERROR_MESSAGE);
    		}
    		uiuser();
    	}
    		public static void uiuser ()
    		{
    		final JFrame jd = new JFrame();
    		final Toolkit toolkit = Toolkit.getDefaultToolkit();
    		final Dimension screenSize = toolkit.getScreenSize();
    		final int x = (screenSize.width - jd.getWidth()) / 2;
    		final int y = (screenSize.height - jd.getHeight()) / 2;
    
    		final JPanel panel = new JPanel(new GridLayout(5, 1));
    		jd.addWindowListener(new MyWindowListener());
    
    		jd.setTitle("Coded by @amir");
    		jd.setLocation(100, 100);
    		jd.setSize(300, 200);
    		final JButton jb1 = new JButton("شروع");
    		final JButton jb2 = new JButton("تنظیمات");
    		final JButton jb3 = new JButton("توقف");
    		final JButton jb4 = new JButton("درباره");
    		final JButton jb5 = new JButton();
    		if(ProxyConstants.sessionid.equals("0"))
    		{
    			jb1.setEnabled(false);
    		}
    		else
    		{
    			JFrame frame;
    			frame = new JFrame();
    		    frame.setSize(200, 100);
        		JOptionPane.showMessageDialog(frame,"hey", "پیام سیستم", JOptionPane.PLAIN_MESSAGE);
    			jb1.setEnabled(true);
    		}
    		jb1.addActionListener(new ButtonListener(jb1, jd, jb5, jb2));
    		jb2.addActionListener(new ButtonListener(jb2, jd, jb5, jb2));
    		jb3.addActionListener(new ButtonListener(jb3, jd, jb5, jb2));
    		jb4.addActionListener(new ButtonListener(jb4, jd, jb5, jb2));
    
    		panel.add(jb1);
    		panel.add(jb2);
    		panel.add(jb3);
    		panel.add(jb4);
    		panel.add(jb5);
    		jd.add(panel);
    		
    		jd.show();
    		
    	}
    
    	public static void showSettings() {
    
    		final Toolkit toolkit = Toolkit.getDefaultToolkit();
    		final Dimension screenSize = toolkit.getScreenSize();
    		final Settings frame = new Settings();
    		final int x = (screenSize.width - frame.getWidth()) / 2;
    		final int y = (screenSize.height - frame.getHeight()) / 2;
    		frame.setLocation(100, 100);
    		frame.setSize(500, 500);
    
    		frame.show();
    
    	}
    }
    
    class ButtonListener implements ActionListener {
    	JButton jb;
    
    	JButton jb5;
    
    	JButton jb2;
    
    	JFrame jf;
    
    	ButtonListener(final JButton jb, JFrame jf, JButton jb5, JButton jb2) {
    		this.jb = jb;
    		this.jf = jf;
    		this.jb5 = jb5;
    		this.jb2 = jb2;
    		
    	}

    second file which i need to re enable my jb1 button
    Java Code:
    	if(!ProxyConstants.sessionid.equals("0"))
    			{
    				ProxyUIStarter ui = new ProxyUIStarter();
    				ui.jb1;
    				
    				String files = currentDir + File.separator+ "bv.jar";
    				System.exit(0);
    				Runtime.getRuntime().exec(files);
    			}
    Regards

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

    Default

    Interesting code here:

    Java Code:
    				System.exit(0);
    				Runtime.getRuntime().exec(files);
    What is this supposed to accomplish? Is the runtime exec ever called? I'd be surprised if it is.

  3. #3
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    You need to expose the jb1 Button so the other class can enable it. In your first class, provide a public "get" method that returns a reference to your button. Then in the second class, call that get method to get the reference. Then call setEnabled(true) on that reference.

  4. #4
    dime111 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    thanks FlipPoker@gmail.com
    can you give me an example or show me on my source how to do this
    cause im very beginner in java

    @Fubarable i was moving a code up and down thats why its there
    sure it wont work
    regards

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

    Default

    Read up in most any tutorial on getter and setter methods (also called accessor and mutator methods respectively), and you'll see many examples of how to create these critters. You will probably want to study quite a bit in order to be able to handle the depth of coding problems that you are and will be faced with.

    Luck

Similar Threads

  1. Disable Radio button
    By AJG in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 11:09 AM
  2. Please help : JSP enable/disable button
    By kalyana in forum New To Java
    Replies: 3
    Last Post: 03-24-2011, 05:06 PM
  3. Disable Maximize button in JFrame?
    By Manish87 in forum AWT / Swing
    Replies: 0
    Last Post: 02-01-2011, 03:39 PM
  4. disable a java.awt.Button but not greying it out
    By alinaqvi90 in forum AWT / Swing
    Replies: 4
    Last Post: 10-18-2010, 01:08 PM
  5. Disable the WebBrowser Back button
    By Deepa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-26-2008, 09:10 PM

Posting Permissions

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