Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting text from swiing components  RSS feed

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting some text from a layer of swing components that are several layers deep. I'll enclose the current code. Is there a way to genericly get the text without knowing what makes up the layers of components? This isn't the exact
code but it shows how it gets down to the text.
==================================================
JInternalFrame currentcomponent
Container qad = currentcomponent.getContentPane();
if(qad == null)
{
return;
}
Component compList[] = qad.getComponents();
if(compList [0] == null)
{
return;
}
JPanel jp = (JPanel)compList [0];
if(jp == null)
{
return;
}
compList = jp.getComponents();
if(compList[0] == null)
{
return;
}
JScrollPane sp = (JScrollPane)compList [0];
if(sp == null)
{
return;
}
compList = sp.getComponents();
if(compList[0] == null)
{
return;
}
JViewport vp = (JViewport)compList [0];
if(vp == null)
{
return;
}
compList = vp.getComponents();
if(compList[0] == null)
{
return;
}
JTextPane tp = (JTextPane)compList [0];
if(tp == null)
{
return;
}
Document doc = tp.getDocument();
String lineOfText = tp.getText();
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry,
As far as I know, all the swing text components (including "JTextPane" -- from your example), extend the JTextComponent class.
Therefore, I suggest you simply test whether the particular component is an instance of "JTextComponent". Something like this (note: untested)

Hope it helps.
Good Luck,
Avi.
[ June 09, 2003: Message edited by: Avi Abrami ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!