Results 1 to 5 of 5
  1. #1
    eevaa is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default Java user interface problem

    Hey everyone!

    I've got trouble changing the text for the text field in my application. More
    precisely, I'd like to add lines every now and then without the other lines
    vanishing, similar to the DOS console.

    This is GUI.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class GUI extends JFrame {
    
    	public void createAndShowGUI() {		
    
            //Create and set up the window.
            JFrame myframe = new JFrame("TopLevelDemo");
            myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myframe.setPreferredSize(new Dimension(800,600));
            myframe.setLayout(new BorderLayout());
                    
            //Create an output text Field
            JTextField outputField = new JTextField(10);
            outputField.addActionListener(null);
            outputField.setPreferredSize(new Dimension(800,525));
            outputField.setEditable(false);
            outputField.ad
    
            myframe.getContentPane().add(outputField, BorderLayout.NORTH);
            
            //Display the window.
            myframe.pack();
            myframe.setVisible(true);
        }
    }
    This is how I'm trying to change the text in the field (even though that's not what I actually want to do, see above).
    Java Code:
    public class General{
    	public static void main(String[] args) {
    		 final GUI iface = new GUI(); //I had to declare that final, does anyone know why btw?
    		 iface.createAndShowGUI();
    		 iface.myframe.outputField.setText("Test"); //<- Does not work.
    	}
    }
    Any help would be very much appreciated!

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    To display multiple lines of simple text in a Swing GUI, you'll want to use a JTextArea, not a JTextField. Also, look at the API and the tutorials regarding this component and in particular, check out the append method.

    Que tengas suerte!

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I don't understand what you want.

    Do you want something like this:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    
    public class AddLines {
    	public static void main(String[] args) {
    		AddFrame frame = new AddFrame();
    		frame.showGUI();
    	}
    }
    
    class AddFrame extends JFrame {
    	JPanel panel;
    	JTextField textField;
    	JTextArea textArea;
    	JButton button;
    	JLabel textLabel;
    	
    	public AddFrame() {
    		panel = new JPanel();
    		textField = new JTextField(20);
    		button = new JButton("Change text");
    		textLabel = new JLabel("Text is not changed");
    		
    		button.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textLabel.setText(textField.getText());
    			}
    		});
    		
    		panel.add(textField, BorderLayout.NORTH);
    		panel.add(button, BorderLayout.SOUTH);
    		panel.add(textLabel, BorderLayout.CENTER);
    		
    		getContentPane().add(panel);
    	}
    	
    	public void showGUI() {
    		setTitle("Add Frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(200,200);
    		setLocation(200,200);
    		pack();
    		setVisible(true);
    	}
    }
    If you want something else, please explain it better.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Or you maybe want something like this:

    Java Code:
    import java.util.Vector;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    
    public class AddLines {
    	public static void main(String[] args) {
    		AddFrame frame = new AddFrame();
    		frame.showGUI();
    	}
    }
    
    class AddFrame extends JFrame {
    	JPanel panel;
    	JTextField textField;
    	JTextArea textArea;
    	JButton button;
    	Vector<String> allTextFields;
    	
    	public AddFrame() {
    		panel = new JPanel();
    		panel.setLayout(new BorderLayout());
    		textField = new JTextField(20);
    		button = new JButton("Add text to console");
    		textArea = new JTextArea(6,20);
    		allTextFields = new Vector<String>();
    		
    		button.addActionListener(new ActionListener() {
    
    			public void actionPerformed(ActionEvent e) {
    				allTextFields.add(textField.getText().toString());
    					String text="";
    					for(int i = 0; i < allTextFields.size(); i++ )
    						text += allTextFields.get(i) + "\n";
    						textArea.setText(text);
    						textField.setText("");
    			}
    			
    		});
    		
    		panel.add(textField, BorderLayout.NORTH);
    		panel.add(textArea, BorderLayout.SOUTH);
    		panel.add(button, BorderLayout.WEST);
    		
    		getContentPane().add(panel);
    	}
    	
    	public void showGUI() {
    		setTitle("Add to console");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(200,200);
    		setLocation(200,200);
    		pack();
    		setVisible(true);
    	}
    }

  5. #5
    eevaa is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. simple user interface designer
    By v1nsai in forum New To Java
    Replies: 3
    Last Post: 08-17-2009, 02:53 AM
  2. User Interface
    By swikar.java in forum Advanced Java
    Replies: 16
    Last Post: 12-09-2008, 03:37 PM
  3. J2ME User Interface Developer
    By mobileapps in forum Jobs Offered
    Replies: 0
    Last Post: 10-03-2008, 01:43 PM
  4. user interface development using JSP
    By pradeep1_mca@yahoo.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-02-2008, 02:48 PM
  5. Help user interface
    By carl in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:58 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
  •