Thread View: borland.public.jbuilder.java.swing
3 messages
3 total messages
Started by user
Tue, 02 Nov 2004 06:29
Questionable JTabbedPane
Author: user
Date: Tue, 02 Nov 2004 06:29
Date: Tue, 02 Nov 2004 06:29
145 lines
6783 bytes
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();
}
}
Re: Questionable JTabbedPane
Author: Paul Furbacher [TeamB]
Date: Tue, 02 Nov 2004 16:55
Date: Tue, 02 Nov 2004 16:55
96 lines
3749 bytes
3749 bytes
user@domain.invalid wrote:
> 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.
> [... lots of verbiage omitted ...]
First, you don't have to run your code as
an applet to see this. Just create
a JFrame, add the tabbed panel, and do
your stuff.
Second, it's highly unlikely that you would
draw a string into a text pane. How it is
being drawn is probably complex enough to
cause this phenomenon. I'd draw into
a panel, not a text pane, which you add
to the tabbed pane. Change
your code to add a JPanel which has
a BorderLayout. Then add the textpane to
the JPanel: add it using a BorderLayout.EAST.
Set the preferred size of the textpane
to something like 100, 190.
if ( tbp.getTabCount() == 0 ) {
JPanel p = new JPanel(new BorderLayout());
p.add(txp, BorderLayout.EAST);
txp.setPreferredSize(new Dimension(100, 190));
tbp.addTab("tab 1", p);
}
Third, change the draw string method so that it draws into
the *panel*, not the textpane.
tbp.getSelectedComponent().getGraphics();
...
Fourth, put the contents of the draw string method
into a nested class which implements Runnable
and call an instance of that runnable
using
...
if ( useDrw ) {
SwingUtilities.invokeLater(
new UpdateRunner(n, msg2));
}
...
public class UpdateRunner implements Runnable {
int n;
String msg;
UpdateRunner(int number, String message) {
n = number;
msg = message;
}
public void run() {
// put the contents of your drawStringWay()
// method here.
}
}
It may be an interesting phenomenon, but
the important question is: how does this
affect you in the real world.
Finally, we do all of our replying in
public -- that is, we don't reply to
requests for responses to a questioner's
email address. Please visit the newsgroup
guidelines Web page to check on this and
other matters.
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://www.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Re: Questionable JTabbedPane
Author: Paul Furbacher [TeamB]
Date: Wed, 03 Nov 2004 04:14
Date: Wed, 03 Nov 2004 04:14
57 lines
3121 bytes
3121 bytes
user@domain.invalid wrote:
> I am going to learn about invokeLater.
Go to java.sun.com and look for the tutorial
site: start here and look at the articles
on threads.
http://java.sun.com/products/jfc/tsc/articles/
> PS The reason why I put my email address is that readers may prefer to
> respond to my question personally, not openly. Plus I never expected
> that there would be some help from Borland pros. We would pay 1000s$
> annually for the technical support from pros of software company, right?
> So, I didn't think pros in your company would even read those questions
> from JBuilder newsgroup.
Please, please, please read the newsgroup guidelines!
These are not official Borland support venues. TeamB
members do *not* work for Borland. We are working
programmers like all the rest of the people who participate
in these newsgroups, but as the newsgroup guidelines
point out, we are *all* volunteers here.
Because this is peer-to-peer support, we do
not take discussion to email. We conduct them
in public, in these newsgroups. That achieves
two things (at least): 1) lurkers read, learn,
chime in with corrections; and 2) we build a
valuable archive of answers which are searchable
using Google or Tamaracka. Many of the answers
I come up with are based on information from
threads in the archives. This second point
is the most valuable aspect of these newsgroups:
that they are archived and searchable. I don't
think most participants, especially new ones,
realize how beneficial this is, and how *different*
it is from the "old days" when it was very
difficult to find good quality technical
information.
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://www.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads