🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #883

Questionable JTabbedPane

#883
From: user
Date: Tue, 02 Nov 2004 06:29
145 lines
6783 bytes
Hi everyone, will you please spend some time with my problem and give 
some clue to the solution to me? I am going to include 1)program 
description, and 2)java source file below (instead of 'attaching' them).
I described my problem or, I believe, JTabbedPane's problem in 1).

Your hint, clue, suggestion, or even question is greatly appreciated.

PS I tested my program in JBuilderX (Foundation), NetScape7, and MSIE7 
and the behavior of my program is the same.

Brief program description:
A. Purpose:
This tiny program  was written to demonstate the following illogical
and weird behavior of JTabbedPane.

a1)When addTab(tabHdr,tabbedPane) is called, followed by
    drawString(TEXT_1),both called in MouseListener,TEXT_1 is not shown.
a2)The 2nd and the following calls to drawString(TEXT_N) called in the
    MouseListener,show the specified text normally.
a3)When setText(TEXT) is used,instead of drawString(),however,the
    specified text is normally shown, at the first call or otherwise.
a4)If addTab()is called OUTSIDE the MouseListener,however, TEXT_1 is
    normally shown.(This can be proved by reactivating the commented-out
    addTab()in 'init()' method.)

Additionally, the program shows the following:

a5)addTab() doesn't show the tab until either setText() or drawString()
    is called.(ie showing the tab is deferred until something is output
    to the selected component for the tab.)
a6)When both setText(), and drawString() are called in that order, the
    text from drawString() is posted first(against their calling order).

B. How the program runs:
b1)After a button and JTabbedPane are shown, it asks which text-output
    method is used (ie either drawString(), setText(), or both. )

b2)Then it asks delay times (in seconds) between
    - addTab() and the first text-output method call(ie drawString() or
      setText()) and
    - 2 consecutive text-output methods (ie setText() > setText(), or
      drawString() > drawString(), or setText() > drawString()).

b3)Then it waits for the mouse click. At the first click, it adds a
    JTextPane component to the tab (via addTab()) and output a line of
    text with the specified text-output method or methods.

b4)For the 2nd and the following clicks, it simply outputs a line of
    text, again, with the specified text-output method.

C. How to run the program:
  The program is an applet. After the source is compiled, the .java file
(or files) must be placed at "classes" subfolder of a folder where the
  following .htm file (of name "TabPaneTest.htm") is put.

  Both folders must have 'web-sharing' property so that the htm file is
  run from your web browser.

  Then type "file:///(folder)/TabPaneTest.htm" in the browser's web
  address where '(folder)' is the folder name the htm file is put (such
  as "c:/test").

<html><title>Tabbed Pane Test</title><body>
<applet codebase="classes" code="TabPaneTest.class" name="TabPane"
   width=400 height=250 vspace=0 hspace=0>
</applet></body></html>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.AttributedString;
import java.text.AttributedCharacterIterator;

public class TabPaneTest extends JApplet
{int         n=0;
  float       dt1,dt2;
  boolean     useDrw=false,useSet=false;
  JPanel      mainPnl=new JPanel();
  JButton     btn=new JButton("JButton");
  JTextPane   txp=new JTextPane();
  JTabbedPane tbp=new JTabbedPane();
  public TabPaneTest(){};
  public void init() {
   Container cpane = getContentPane();
   tbp.setPreferredSize(new Dimension(220,190));
   btn.addMouseListener(new MouseAdapter() {
     public void mouseClicked(MouseEvent e) {++n; showText(n);}});
   ChangeListener chglst = new ChangeListener() {
     public void stateChanged(ChangeEvent e) {System.out.println("state 
changed");}};
   tbp.addChangeListener(chglst);
   mainPnl.add(btn);
   mainPnl.add(tbp);
   cpane.add(mainPnl);
//tbp.addTab("p1",txp);
}
  public void start() {
   askMethodToUse();
   dt1=askDelayTime("addTab() and the 1st drawString/setText?");
   dt2=askDelayTime("consecutive text output?");
  }
  public void showText(int n) {
   String msg1="from setText - clicks=";
   String msg2="from drawString - clicks=";
   if (tbp.getTabCount()==0){tbp.addTab("p1",txp); waitAbit("addTab 
done",dt1);}
   if (useSet){txp.setText(msg1+n);   waitAbit("setText done",dt2);}
   if (useDrw){drawStringWay(msg2,n); waitAbit("drawString done",dt2);}
}
public void waitAbit(String msg,float sec) {
   try {Thread.sleep((int)sec*1000);}
   catch (InterruptedException ev) {} //(nothing'll interrupt)
   System.out.println(msg);
}
public void drawStringWay(String msg,int n) {
   Graphics g = txp.getGraphics();
   AttributedString as=new AttributedString(msg+n);
   AttributedCharacterIterator aci=as.getIterator();
   g.drawString(aci,0,20+20*n);
   g.dispose();
}
public void askMethodToUse()
{String ans;
  String opts[]={"use drawString() only","use setText() only",
                 "use both drawString() and setText()"};
  ans=(String)JOptionPane.showInputDialog(mainPnl,"Select an option below.",
      "Output from",JOptionPane.QUESTION_MESSAGE,null,opts,opts[0]);
  if (ans.equals(opts[0])) {useDrw=true;  useSet=false;}
  if (ans.equals(opts[1])) {useDrw=false; useSet=true;}
  if (ans.equals(opts[2])) {useDrw=true;  useSet=true;}
}
public float askDelayTime(String prmpt)
{Float  dt;
  String ans;
  StringBuffer sb=new StringBuffer("wait time (in sec) between ");
  sb.append(prmpt);
  JLabel lbl=new JLabel(sb.toString());
  String opts[]={"0.0","1.0","1.5","2.5","4.0"};
  ans=(String)JOptionPane.showInputDialog(mainPnl,lbl,
      "Time delay",JOptionPane.QUESTION_MESSAGE,null,opts,opts[0]);
  dt=Float.valueOf(ans);
  return dt.floatValue();
}
}
Technical Details
Message-ID: <41871c3a@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,662c888bffac8e46,start
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Tue, 02 Nov 2004 01:29:50 -0500
From:  user@domain.invalid
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing
Subject: Questionable JTabbedPane
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.192.157.16
Message-ID: <41871c3a@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1099373626 69.192.157.16 (1 Nov 2004 21:33:46 -0700)
Lines: 147
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail