Results 1 to 9 of 9

Thread: Improve my GUI!

  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Question Improve my GUI!

    Hello. I would really appreciate some help which is not available in the API!

    I have written a program which is a text analyser. It gets some text (either from keyboard or a file) and displays various statistics about the text. At the moment the program effectively displays like a tool bar (occupying only the top left hand part of the screen) and any boxes to collect information, select files and display information all "pop up" outside of the tool bar. I created the input/output boxes by using JOptionPane boxes (I included a JTextArea where the text is too large to fit on one pane where appropriate).

    What I want to do is to make an application so that all of the input/output is in the same frame as the toolbar. (Therefore it will look like an ordinary program!)

    I would be grateful for any guidance as to how I should go about in order to achieve this.

    Many thanks

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

    Default

    What part are you stuck on? Is it laying out components in a Container? If so, check out the Sun Java tutorial of this very name. Is it getting different parts of the app to talk to each other? Have you tried to implement a GUI on a smaller scale first before the larger one? Your question is somewhat general and information on what you've done already somewhat vague, so please remember that the more specific and informative your question likely the more specific and helpful the reply. Best of luck!

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    I created the input/output boxes by using JOptionPane boxes (I included a JTextArea where the text is too large to fit on one pane where appropriate).

    i think you need a guide to layout managers or perhaps an guide to swing components like scrollbars and so on.

    please post your code.

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    Since my first post I have read the tutorial on layouts and am now successfully using NetBeans. (I love it!) This has produced a nice layout for me.

    As before I am writing a text analyser. What I have designed is a JMenu with some JButtons (that do the same thing).

    However, I want to have is two bigger boxes below. The first an "input" box which the user writes in some text. The second a display box which when a JButton is pressed shows statistics about the text in the first input box.

    I would be very grateful if anyone could tell me what types of java "boxes" I should be using in order to achieve this?

    Many thanks

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    I would be very grateful if anyone could tell me what types of java "boxes" I should be using in order to achieve this?

    if you would read my links above with the layout managers then you would know how. but for you i have a small example. the first box of code is for the gui and the second for the listeners.

    Java Code:
    import java.awt.*;
    import java.util.StringTokenizer;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class TextAnalyzerDemo extends Frame
    {
    	private JLabel analyzerLabel;
    	private TextArea textArea;
    	private Button enter, clear, exit;
    	StringTokenizer st;
    
    
    	public TextAnalyzerDemo (String title)
    	{
    		super(title);
    
    		analyzerLabel = new JLabel();
    		textArea = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
    		textArea.setEditable(true);
    		analyzerLabel.setBackground(Color.GRAY);
    
    		enter = new Button("Analyze this");
    		clear = new Button("Clear");
    		exit = new Button("Exit");
    
    		//layout the GUI
    		this.add(textArea, BorderLayout.CENTER);
    		Panel south = new Panel(new BorderLayout());
    		south.add(analyzerLabel, BorderLayout.CENTER);
    
    		Panel southEast = new Panel(new BorderLayout());
    		southEast.add(enter, BorderLayout.WEST);
    		southEast.add(clear, BorderLayout.CENTER);
    		southEast.add(exit, BorderLayout.EAST);
    		south.add(southEast, BorderLayout.EAST);
    
    		this.add(south, BorderLayout.SOUTH);
    
    		//setup the event handling
    		MyListener listener = new MyListener(analyzerLabel, textArea);
    		enter.addActionListener(listener);
    		clear.addActionListener(listener);
    		exit.addActionListener(listener);
    	}
    
    	public TextArea getTextArea()
    	{
    		return textArea;
    	}
    
    	public static void main(String [] args)
    	{
    		TextAnalyzerDemo f = new TextAnalyzerDemo ("Text Analyzer");
    		f.setSize(400,200);
    		f.setVisible(true);
    		f.getTextArea().requestFocus();
    	}
    }

    Java Code:
    package myjava.examples.org;
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.StringTokenizer;
    
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class MyListener implements ActionListener {
    	private JLabel destination;
    	private TextArea source;
    	
    	
    	public MyListener (JLabel d, TextArea s) 
    	{
    		source = s;
    		destination = d;
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		Object component = e.getSource();
    		String action = e.getActionCommand();
    		
    		if (component instanceof TextField || action.equals("Analyze this"))
    		{
    			StringTokenizer st;
    			st = new StringTokenizer (source.getText(), " ");
    			destination.setText("Number of tokens: " + st.countTokens());
    
    			String text = source.getText();
    			//	add other analyzing code here
    		}
    		else if (action.equals("Clear"))
    		{
    			source.setText("");
    			source.requestFocus();
    			destination.setText("");
    		}
    		else if (action.equals("Exit"))
    		{
    			System.exit(0);
    		}
    	}
    }

    start the application, enter some text and then press the button "Analyze this" and the number of tokens you entered in the textarea are prompted in the jlabel. now you can add more analyzing code and integrate the panels in your app.

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

    Default

    All they asked for was what components their boxes should be...:)

    And a bit of critiquing:

    That's not how you're supposed to start up a Swing program.
    And a single listener to handle all events is not generally good practice.
    It's a bit like having a single servlet doing everything and filtering the different tasks based on some flag or other.

  7. #7
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Unhappy

    Thank you for your posts.
    How do I compile the code?

    Also what type of box should I use to display the text? I need to somehow export text into the box, or create some kind of popup frame.

    I am very upset at the moment from having had a program that worked I get the following error message:
    Exception in thread "AWT-EventQueue-0" java.lang.VerifyError: (class: analysis, method: <init> signature: ()V) Constructor must call super() or this()

    Please could someone suggest what is wrong!?

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

    Default

    What's the constructor of the analysis class look like?

  9. #9
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    Thank you for the email. I abandoned what I had done and started again! I am now at the point I was before. (This time I backed my work up!)

    Can someone please tell me how I can get a some kind of java information box to display inside of an existing JFrame? At the moment I am using pop up windows (i.e. JOptionPane) that launch, but are outside of the pane. What I want to do is to get them or something else inside an existing JFrame.

    This also applies to JFileChooser (for loading and saving files). So instead of the files window launching outside of the JFrame it goes inside the Frame.

    Any help is appreciated!

Similar Threads

  1. How to improve this code
    By Raymond in forum Advanced Java
    Replies: 9
    Last Post: 03-19-2010, 08:16 AM
  2. How to improve Jslider
    By JohnST in forum New To Java
    Replies: 3
    Last Post: 01-12-2010, 01:04 AM
  3. How to Improve performance of query?
    By SE123 in forum Advanced Java
    Replies: 0
    Last Post: 10-29-2009, 09:13 AM
  4. how to improve my security?
    By anthrax in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-13-2009, 09:08 AM
  5. how to improve the performance of JWS?
    By dinesh kaushik in forum Java Applets
    Replies: 0
    Last Post: 11-21-2007, 08:46 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
  •