The Artima Developer Community
Sponsored Link

Java Answers Forum
GUI help needed

8 replies on 1 page. Most recent reply: Apr 6, 2002 8:44 AM by Elliott Wood

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Elliott Wood

Posts: 15
Nickname: tooess
Registered: Mar, 2002

GUI help needed Posted: Apr 2, 2002 2:30 PM
Reply to this message Reply
Advertisement
I'm creating an interface which at the moment uses a text area to display some results in what I would like to be two columns under the headings of ISBN number and Book title. At the moment I'm using the setText() method to add a string of text with all of the ISBN numbers and Titles to the text area. e.g.

String result = "problems with java \t\t\t\t 1-1212-2121 \n";

result += "god damn it \t\t\t\t 1-6545-4565 \n";

textArea display.setText(result);

The thing is that the text area obviously doesn't display the ISBN numbers and titles in nice neat columns, which is what I want and I can't find a way of doing it. If anyone can understand my jibberings and has an idea of a solution it would be greatly appreciated, thanx.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: GUI help needed Posted: Apr 2, 2002 11:09 PM
Reply to this message Reply
Sounds like you should be using a JTable, not JTextArea.

Elliott Wood

Posts: 15
Nickname: tooess
Registered: Mar, 2002

Re: GUI help needed Posted: Apr 3, 2002 3:07 AM
Reply to this message Reply
Thanks for your suggestion, I'll give it a try

steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: GUI help needed Posted: Apr 4, 2002 9:30 PM
Reply to this message Reply
While you could use a pair of TextFields (Labels etc)
or JTable, you could format any text into columns by knowing the length of each string, (assuming fixed char size) or by using the awt class FontMetrics
public Rectangle2D getStringBounds(String str, Graphics context)
keep adding " " to the key string until it ends where the value string column should begin. or prepending " " char's until you're where you want to be.
It's great to have choices.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: GUI help needed Posted: Apr 5, 2002 12:34 AM
Reply to this message Reply
I would strongly disagree with the approach of stuffing space characters into the TextArea in order to align columns.

First of all, you are not likely to get good alignment unless you use a fixed-width font. You may get things almost aligned, but it will probably look a bit cheesy and be very much dependent upon the font.

Second, you are polluting your data. If you want to allow editing, it will be a big mess to maintain on many fronts. Imagine someone typing new characters into an item in the first column; you have to keep adjusting the number of padding space characters. What happens when they type in enough characters to make the current row's first column wider than the current widest column in some other row? Now you have to go and re-pad all the rows. This will be visually jarring, at best, and the data quickly becomes an unmanageable amorphous blob of characters (kind of like a chess tournament!). Additionally, if you copy and paste this data to some other window that has a different font, the results rarely be satisfying.

With your data in a table format, you maintain the data's integrity, allow for easy editing and copying. On top of that, it probably will look much nicer and more professional. You will also learn how to use a nifty new component instead of trying to shoehorn everything you do into simple text components.

Since it is great to have choices, another choice that is better than using a TextArea or TextField with space padding is simply drawing the text on the Pane -- then you can have perfect alignment. However, editing and copying will be a lot of work, so this is really only good if you don't want it to be edited or copied.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: GUI help needed Posted: Apr 5, 2002 3:01 AM
Reply to this message Reply
I would suggest you to use the JTextArea with HTML rendering.... Sounds odd, but it work fine especially you the "client" needs to be deployed as thin client later on the project !

Rgds,

Thomas,

Elliott Wood

Posts: 15
Nickname: tooess
Registered: Mar, 2002

Re: GUI help needed Posted: Apr 5, 2002 12:38 PM
Reply to this message Reply
Thanks for all of your suggestions i'm going to give them a try, i'll let you know which one worked best for me.

Thomas, is there any chance you could post a small e.g. of HTML rendering as i'm not quite sure I understand how to perform it.

again thanks, Elliott.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

GUI help needed - 3 solutions Posted: Apr 6, 2002 6:29 AM
Reply to this message Reply
import java.io.Reader;
import java.io.FileReader;
import java.io.IOException;
 
import java.awt.BorderLayout;
 
import javax.swing.JFrame;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
 
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
 
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
 
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
 
/**
  * This class just demonstrate how to render an HTML document in a 
  * JFrame.
  * Three different cases are proposed.
  *
  * @see http://manning.com/sbe/files/uts2/Chapter27html/Chapter27.htm
  * @see http://www.ibiblio.org/javafaq/slides/sd2000east/webclient/25.html
  * @see http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/
  *
  * See respective authors for Copyright issues.
  * This document is provided "as is".
  * @see javax.swing.text.html.HTMLDocument
  *
  * @version 0.15 - final
  * @author <a href="mailto:tsmets@altern.org">Thomas SMETS</a>
  * 
  */
public class HTMLRendering
  extends JFrame
{
  Document d = null;  
  JEditorPane t = null;
  Category log = null;
  
  HTMLRendering (String aName)
  {    
    log = Category.getInstance(HTMLRendering.class);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    setTitle( aName );
    
    t = new JEditorPane ();
    t.setEditable(false);    
      
    JScrollPane p = new JScrollPane (t);
    p.getViewport().add (t);
    p.setBounds (0, 0, 150,150);
    p.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    p.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    getContentPane().add(p, BorderLayout.CENTER);
    setSize(350, 750);
    setVisible(true);      
    log.info ("HTMLRendering created");
  }
  
 
  
  public void initializeURL (String anURL)
  {
    log.info ("HTMLRendering::initializeURL ()");
    try 
    {
      log.info("Loading the URL");
      t.setPage(anURL);
      log.info("Loaded");
    }
    catch (Exception ex) 
    {
      log.warn("Exception " + ex.getClass().getName(), ex);
    }
    return;     
  }  
  
  public void initializeFile (String aFileName)
  {
    log.info ("HTMLRendering::initializeFile");
    try 
    {
      FileReader fr = new FileReader( aFileName );      
      t.read(fr, aFileName);
    } catch (IOException ioe)
    {
      log.warn("Exception thrown " + ioe.getClass(), ioe);      
    }
  }
  
  public void initializeHTMLFile (String aFileName)
  {
    log.info ("HTMLRendering::initializeHTMLFile");
    try 
    {
      t.setEditorKit(new HTMLEditorKit());
      FileReader fr = new FileReader( aFileName );      
      t.read(fr, aFileName);
    } catch (IOException ioe)
    {
      log.warn("Exception thrown " + ioe.getClass(), ioe);      
    }
  }  
  
  public static void main (String[] args)
  {
    BasicConfigurator.resetConfiguration();
    BasicConfigurator.configure();
    
    HTMLRendering html = new HTMLRendering ("Example - HTML file");
    html.initializeHTMLFile ("SimpleHTML.html");
    
    html = new HTMLRendering ("Example - HTML from URL");
    html.initializeURL ("http://tsmets.lautre.net");
    
    html = new HTMLRendering ("Example - HTML Viewed as pure text");
    html.initializeFile ("SimpleHTML.html");
  }
}
 




Copy - Paste what is next in a file named SimpleHTML.html

<html>
<head>
<title>Simple HTML</title>
</head>

<body>
Here is some HTML code...<br>
Here <b>is</b> <i>some</i> <u>more</u> with formatting
</body>
</html>




I guess with a screen big enough you should be able to view the three on ontop of each other.
Up to you to move them around to have them on one screen next to each other.

You need the latest version of Log4J (http://jakarta.apache.org/log4J) in you classpath for the example to work properly (1.2Beta).

Elliott Wood

Posts: 15
Nickname: tooess
Registered: Mar, 2002

Re: GUI help needed Posted: Apr 6, 2002 8:44 AM
Reply to this message Reply
Thanks for the example, i'll give it a try

Flat View: This topic has 8 replies on 1 page
Topic: Java application Previous Topic   Next Topic Topic: JTABLE

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use