Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: GUI data handling

  1. #1
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default GUI data handling

    Hi!
    Ive been studying Java Swing all day today, have understood the basic components, layouts and event-handlers, but i cant find any information on data handling, passing a value if a button was pressed etc. to a text box etc. Could someone share a tutorial please? Ive been googling, but i couldnt find anything sueful.. Thanks!

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

    Default Re: GUI data handling

    You may be making the common mistake of looking for too specific a tutorial, and you'll likely not find a tutorial that covers exactly that. Instead, you'll find tutorials that cover the steps involved in doing this including



    Then it's up to you to put this together so that your program does what you want.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: GUI data handling

    The button tutorial.
    The ActionListener code in there shows a button changing the state of other buttons on a click.
    The concept behind that and changing the text in a JTextField is the same.

    ETA: Ooh...mere seconds!
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: GUI data handling

    Thanks about the links ;) But im having an issue i dont really understand: "Cannot refer to a non-final variable name inside an inner class defined in a different method" I cant make the variable final, i need to edit it[bolded]. The code is below, i just want to check if there is something written in each field. As far as i have understood, i cant do this, becaue the garbage colector may have deleted the value right? How to work around this issue? One of those links had a solution, but i couldnt understand it completely :\

    Java Code:
    public class Task2 extends JFrame{
    	
    	Task2()
    	{
    		Start();
    	}
    	
    	public void Start()
    	{	
    		JPanel panel=new JPanel(new GridLayout(3,2));
    		add(panel, BorderLayout.CENTER);
    		JTextField name=new JTextField(20);
    		name.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				
    			}
    		});
    		panel.add(name);
    		
    		JTextField surname=new JTextField(20);
    		panel.add(surname);
    		
    		JTextField mail=new JTextField(30);
    		panel.add(mail);
    		
    		JButton save=new JButton("Save");
    		add(save,BorderLayout.SOUTH);
    		
    		save.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				[B]if(name.getText()!=NULL)[/B]
    			}
    		});
    		pack();
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args) 
    	{
    	Task2 task=new Task2();
    	task.setVisible(true);
    
    	}
    
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: GUI data handling

    You need to either have the 'name' field as an attribute of your 'Task2' class, or declare it as final.
    The reasons are slightly complicated, but essentially the compiler needs to guarantee that that variable is available (and hasn't changed) when the actionPerformed is run, which could be some time after that listener was created.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: GUI data handling

    Thats exactly what i did, added them to the class and even implemented the Listener:

    Java Code:
    public class Task2 extends JFrame implements ActionListener{
    	
    	private JTextField name;
    	private JTextField surname;
    	private JTextField mail;
    Java Code:
    public void actionPerformed(ActionEvent e)
    	{
    		String text=name.getText();
    	}
    If i press the button, it should get the text now, but the compiler rather throws a null exception at me. Why? As far as i understand it should work..

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: GUI data handling

    No, don't implement the listener.
    You had it the preferred way before, which is an anonymous listener.

    What does your code look like, because I suspect you are not initialising the text field.
    You might think you are, but I expect you have another one with the same name.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: GUI data handling

    Here is the complete code, still aint working. I started form scrath and now everything is working, althout its basicly the same as this one, cant find why this one ains working oO

    Java Code:
    package tasks;
    
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class Task2 extends JFrame implements ActionListener{
    	
    	private JTextField name;
    	private JTextField surname;
    	private JTextField mail;
    	
    	
    	public void Start()
    	{	
    		JPanel panel=new JPanel(new GridLayout(3,2));
    		add(panel, BorderLayout.CENTER);
    		JTextField name=new JTextField(20);
    		panel.add(name);
    		
    		JTextField surname=new JTextField(20);
    		panel.add(surname);
    		
    		JTextField mail=new JTextField(30);
    		panel.add(mail);
    		
    		JButton save=new JButton("Save");
    		save.addActionListener(this);
    		add(save,BorderLayout.SOUTH);
    		
    		pack();
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		String text=name.getText();
    	}
    	
    	Task2()
    	{
    		Start();
    	}
    	public static void main(String[] args) 
    	{
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                	Task2 task=new Task2();
                	task.setVisible(true);
                }
            });
    	
    
    	}
    
    }
    Below is the working code. There is but one last thing i need to add, a Dialog window, which pops up if the information aint valid. I have to use the JDialog right, should i create it as another class? Im a bit confused on how to pass it its parent window..

    Java Code:
    package examples;
    
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class MainFrame extends JFrame implements ActionListener {
    
    	private JTextField name;
    	private JTextField surname;
    	private JTextField mail;
    	private JButton button;
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if(name.getText().equals("") || surname.getText().equals("") || !mail.getText().contains("@") )
    		{
    			
    		}
    		else
    		{
    			name.setText("");
    			surname.setText("");
    			mail.setText("");
    		}
    	}
    	
    	public void CreateUI()
    	{
    		setTitle("Personel Manager");
    		JPanel panel=new JPanel(new GridLayout(3,1));
    		add(panel, BorderLayout.CENTER);
    		JPanel info=new JPanel(new GridLayout(3,1));
    		add(info, BorderLayout.WEST);
    		
    		name=new JTextField(20);
    		JTextField nameLabel=new JTextField("Name");
    		nameLabel.setEditable(false);
    		nameLabel.setBorder(new EmptyBorder(3,3,3,3));
    		
    		surname=new JTextField(20);
    		JTextField surnameLabel=new JTextField("Surname");
    		surnameLabel.setEditable(false);
    		surnameLabel.setBorder(new EmptyBorder(3,3,3,3));
    		
    		mail=new JTextField(20);
    		JTextField mailLabel=new JTextField("Mail");
    		mailLabel.setEditable(false);
    		mailLabel.setBorder(new EmptyBorder(3,3,3,3));
    		
    		panel.add(name);
    		panel.add(surname);
    		panel.add(mail);
    		
    		info.add(nameLabel);
    		info.add(surnameLabel);
    		info.add(mailLabel);
    		
    		button=new JButton("Save");
    		button.addActionListener(this);
    		JPanel forSend=new JPanel();
    		add(forSend, BorderLayout.EAST);
    		forSend.add(button, BorderLayout.EAST);
    		pack();
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    	
    	MainFrame()
    	{
    		CreateUI();
    	}
    	
    	public static void main(String[] args) 
    	{
    		MainFrame main=new MainFrame();
    		main.setVisible(true);
    
    	}
    
    }
    Last edited by MustSeeMelons; 12-05-2012 at 03:11 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: GUI data handling

    Java Code:
    JTextField name=new JTextField(20);
    That's not the same variable as the one defined at the top of the class.
    You've defined a local one, so the 'name' attribute is still null.
    Get rid of the 'JTextField' bit at the beginning of that line (and the same for the other variables you have as attributes).

    I would still put the ActionListener as an anonymous class rather than implement it.
    If you have it listening on more than one thing then you'll complicate your code...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 11
    Last Post: 09-21-2012, 09:33 AM
  2. Asynchronous data handling in Servlet
    By blessedmind in forum Java Servlet
    Replies: 4
    Last Post: 04-20-2012, 10:47 AM
  3. Best practices in handling big data streams
    By rsantana in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2011, 03:23 PM
  4. IO handling
    By sonalim in forum Advanced Java
    Replies: 2
    Last Post: 10-08-2010, 04:00 AM
  5. Replies: 0
    Last Post: 10-31-2008, 02:13 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
  •