Results 1 to 8 of 8
- 01-19-2010, 05:11 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Insert and retrieve embedded Buttons from JTextPanel
Hi
I have a JTextPane and want insert JButtons and free-text into it during runtime. While editing, JButtons may also be removed again.
I inserted the Buttons into the JTextPane with .insertComponent(), which works fine.
If I add them with .add() they don't show, even after revalidate and repaint.
Now here comes my Problem:
When I finish editing I need to know which JButtons are remaining in the JTextPane...
with .getComponentCount() I can get the number of JButtons (which are the only Components I add) in the JTextPane, but I am not able to retrieve the Button-objects.
with .getComponents() I get an array of objects back, but I can't cast them into JButtons. (they are of the type ComponentView$Invalidator ?... never heard of it)
Can anybody help? It would be very appreciated.
PS: I have to admit this is the first time I have been working with JTextPane, and I found the documentation a bit overwhelming and confusing. But I was hoping there would be a simple solution.:confused:
- 01-19-2010, 07:33 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You could just use the index position of each component:
Java Code:package peterme; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextPane; import javax.swing.SwingUtilities; class TextPaneFun { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initialize(); } }); } public static void initialize() { JFrame frame = new JFrame(""); frame.setSize(400, 400); JTextPane pane = new JTextPane(); JButton remove = new JButton(); JButton removeAll = new JButton(); removeAll.setAction(new RemoveButtons("Remove us all", pane)); remove.setAction(new RemoveButtons("Remove ME", pane)); pane.insertComponent(remove); pane.insertComponent(removeAll); frame.add(pane); frame.setVisible(true); } } class RemoveButtons extends AbstractAction { private static final long serialVersionUID = 6442325023529096622L; private JTextPane pane; public RemoveButtons(String text, JTextPane pane) { super(text); this.pane = pane; } @Override public void actionPerformed(ActionEvent e) { JButton source = (JButton) e.getSource(); if (source.getText().equalsIgnoreCase("Remove Me")) { pane.remove(0); } else { int componentCount = pane.getComponentCount(); for (int i = 0; i < componentCount; i++) pane.remove(0); } pane.repaint(); } }
- 01-19-2010, 11:06 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
but how do I retrieve the Buttons?
Hi
I have tried your code, but it didn't address my problem.
I really should have added some code at the first time to explain the problem better, so here is one:
This code will create a JTextPane with some text and a few JButtons in it. The Buttons are filled in by .insertComponent() and by .add() to demonstrate the difference ( .add() doesn't show ).Java Code:[FONT=Courier New][SIZE=3]import java.awt.BorderLayout;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]import java.awt.Component;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]import java.awt.event.ActionEvent;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]import java.awt.event.ActionListener;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]import javax.swing.JButton;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]import javax.swing.JFrame;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]import javax.swing.JTextPane;[/SIZE][/FONT] [FONT=Courier New][SIZE=3]public class TPE2 extends JFrame {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] private JTextPane pane = new JTextPane();[/SIZE][/FONT] [FONT=Courier New][SIZE=3] public TPE2() {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] // filling in pane with some text and Buttons[/SIZE][/FONT] [FONT=Courier New][SIZE=3] // this would be done by the user at runtime[/SIZE][/FONT] [FONT=Courier New][SIZE=3] // Buttons are filled in by insertComponent() + add() to demonstrate the difference[/SIZE][/FONT] [FONT=Courier New][SIZE=3] for (int i = 0; i < 3; i++) {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] pane.replaceSelection(" text ");[/SIZE][/FONT] [FONT=Courier New][SIZE=3] pane.insertComponent(new JButton("InsertedButton")); // this workes fine[/SIZE][/FONT] [FONT=Courier New][SIZE=3] pane.replaceSelection(" text ");[/SIZE][/FONT] [FONT=Courier New][SIZE=3] pane.add(new JButton("AddedButton")); // this will not show[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3] // button to print[/SIZE][/FONT] [FONT=Courier New][SIZE=3] JButton printButton = new JButton("PrintOut Components in TextPane");[/SIZE][/FONT] [FONT=Courier New][SIZE=3] printButton.addActionListener(new ActionListener() {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] public void actionPerformed(ActionEvent event) {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] printOut();[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3] });[/SIZE][/FONT] [FONT=Courier New][SIZE=3] // layout[/SIZE][/FONT] [FONT=Courier New][SIZE=3] getContentPane().add(pane, BorderLayout.CENTER);[/SIZE][/FONT] [FONT=Courier New][SIZE=3] getContentPane().add(printButton, BorderLayout.SOUTH);[/SIZE][/FONT] [FONT=Courier New][SIZE=3] setSize(600, 300);[/SIZE][/FONT] [FONT=Courier New][SIZE=3] setVisible(true);[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3] public void printOut() {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] System.out.println();[/SIZE][/FONT] [FONT=Courier New][SIZE=3] System.out.println(pane.getComponentCount());[/SIZE][/FONT] [FONT=Courier New][SIZE=3] for (Component com : pane.getComponents()) {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] try {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] JButton jb = (JButton) com;[/SIZE][/FONT] [FONT=Courier New][SIZE=3] System.out.print("invisible Component of added Button: ");[/SIZE][/FONT] [FONT=Courier New][SIZE=3] System.out.println(jb.toString());[/SIZE][/FONT] [FONT=Courier New][SIZE=3] } catch (ClassCastException cce) {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] System.out.print("visible Component of inserted Button: ");[/SIZE][/FONT] [FONT=Courier New][SIZE=3] System.out.println(com.toString());[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3] public static void main(String[] args) {[/SIZE][/FONT] [FONT=Courier New][SIZE=3] TPE2 tpe2 = new TPE2();[/SIZE][/FONT] [FONT=Courier New][SIZE=3] tpe2.printOut();[/SIZE][/FONT] [FONT=Courier New][SIZE=3] }[/SIZE][/FONT] [FONT=Courier New][SIZE=3]}[/SIZE][/FONT]
At the end there will be a printout of all the Components of the JTextPane, demonstrating that the inserted ones can't be casted into JButtons, so I wouldn't know which Buttons they were.
If Buttons are deleted during editing of the JTextPane, and a printOut is done again, it shows that only the inserted ones were deleted, but not the ones which were added but are invisible (unsurprisingly).
My problem is that I need to be able to find out exactly which Buttons are present and visible in the JTextPane at any moment.:confused:
Thanks a lot for helping!:)
-
One problem you have is a layout problem. JTextPane isn't really built to hold added buttons, but if you really want to add them, you have to either specify their size and location (a la null layout), or give your text pane a better layout via setLayout.
You understand from the API that add and insertComponent are for two different purposes, right?
- 01-20-2010, 01:43 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
how to check TextPane for inserted Buttons?
Thanks for clarifying the issue for me. I do appreciate.
I think I understand the difference of add and insertComponent now, and will not talk about the .add -method anymore.
What I need to use is insertComponent, because I want to insert Buttons in a Text and edit their location within the text. The use of the Buttons is similar to hyperlinks in a webpage.
However, at the end of editing I need to find out which Buttons are there in the TextPane.
What I would like to get is an Array of JButtons that are present in the JTextPane. (Keeping track of what was added is not helpful as Buttons can be deleted on editing. Or is there a Listener that keeps track of Components that are deleted??)
I tried the .getComponents() -method, but it doesn't seem to work for Buttons that have been inserted.
Is there any trick? Or can you give me a hint how to proceed?:confused:
Cheers
PS: maybe it was presumptuous of me to ask this question in the Advanced forum. I admit I am only a Java-amateur, and JTextPane is pretty new for me.:D
-
Look up the API for ComponentView. It appears to be a holder or wrapper class that holds the other widgets that are being displayed on your textpane. Your buttons are present within the ComponentViews. To see this is so, you need to use a similar program to your previous, but make it recursive so you can see what holds what. For e.g.,
Java Code:import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextPane; public class TPE2 extends JFrame { private JTextPane pane = new JTextPane(); public TPE2() { //!! pane.setLayout(new GridLayout(1, 0)); //!! // filling in pane with some text and Buttons for (int i = 0; i < 3; i++) { pane.replaceSelection(" text "); pane.insertComponent(new JButton("InsertedButton")); // this workes fine pane.replaceSelection(" text "); } // button to print JButton printButton = new JButton("PrintOut Components in TextPane"); printButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { printOut(); } }); // layout getContentPane().add(pane, BorderLayout.CENTER); getContentPane().add(printButton, BorderLayout.SOUTH); setSize(600, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void printOut() { System.out.println(); showComponents(pane, 0); } public void showComponents(Container c, int tabCount) { Component[] comps = c.getComponents(); for (Component comp : comps) { for (int i = 0; i < tabCount; i++) { System.out.print("\t"); } System.out.println(comp.toString()); if (comp instanceof Container && ((Container)comp).getComponents().length > 0) { showComponents((Container)comp, tabCount + 1); } } } public static void main(String[] args) { TPE2 tpe2 = new TPE2(); tpe2.printOut(); } }
- 01-20-2010, 09:10 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 8
- Rep Power
- 0
Thank You!
This is great! My problem is 100% solved!
Thank you very much Fubarable!!!!
I have been trying on this one for quite a while on my own before I consulted you guys, and I am pretty certain that I wouldn't have solved it alone.
:):):)
One last question just out of interest:
The recursive method has always recursed exactly 1 time (by trying several scenarios). So I wrote the same method but not recursive, which works fine too so far.
Can you think af any scenario where it wouldn't work?
Just for playing around, in case anybody else is interested, I have a similar code, slightly more complex, with 2 printOut methods: the 1st is recursive, the 2nd normal:
Java Code:import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextPane; public class ComponentsInJTextPad extends JFrame { private int number; private JTextPane pane = new JTextPane(); public static void main(String[] args) { new ComponentsInJTextPad(); } public ComponentsInJTextPad() { // button to insert some text JButton textButton = new JButton("Insert Text"); textButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pane.replaceSelection(" text "); } }); // button to insert an icon final ImageIcon icon = new ImageIcon(getClass().getResource("Any Picture.gif")); JButton iconButton = new JButton(icon); iconButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pane.insertIcon(icon); pane.replaceSelection(" "); } }); JButton buttonButton = new JButton("Insert Button"); buttonButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { pane.insertComponent(makeButton()); pane.replaceSelection(" "); } }); // button to print JButton printButton = new JButton("print"); printButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println(); System.out.println(); printOut(pane, 0); System.out.println(); System.out.println(); printOut2(); } }); // layout JPanel buttons = new JPanel(); buttons.add(textButton); buttons.add(iconButton); buttons.add(buttonButton); buttons.add(printButton); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(pane, BorderLayout.CENTER); getContentPane().add(buttons, BorderLayout.SOUTH); setSize(700, 200); setVisible(true); } public JButton makeButton() { // button to insert a button JButton thisButton = new JButton("A:InsertButton" + number); number++; thisButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { pane.insertComponent(new JButton("B:Button" + number)); number++; pane.replaceSelection(" "); } }); return thisButton; } public void printOut(Container comp0, int recurs) { Component[] comps1 = comp0.getComponents(); if (comps1.length == 0) { System.out.println("\tnull"); } for (Component comp1 : comps1) { for (int i = 0; i < recurs; i++) { System.out.print("-\t"); } if (comp1 instanceof JButton) { System.out.println(((JButton) comp1).getText()); } else { System.out.println(comp1.toString()); assert comp1 instanceof Container; printOut((Container) comp1, recurs + 1); } } } public void printOut2() { Component[] comps1 = pane.getComponents(); if (comps1.length == 0) { System.out.println("\tnull"); } for (Component comp1 : comps1) { System.out.println(comp1.toString()); assert comp1 instanceof Container; Component[] comps2 = ((Container) comp1).getComponents(); if (comps1.length == 0) { System.out.println("\tnull"); } for (Component comp2 : comps2) { assert comp2 instanceof JButton; System.out.println(((JButton) comp2).getText()); } } } }
-
You're quite welcome!
Sure. What if you want to add something more complex, say a JButton with a JLabel all on a JPanel.One last question just out of interest:
The recursive method has always recursed exactly 1 time (by trying several scenarios). So I wrote the same method but not recursive, which works fine too so far.
Can you think af any scenario where it wouldn't work?
Similar Threads
-
Embedded Java Middleware in STB Technologies
By neel_vk in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 03:46 PM -
how to insert/retrieve jpg img format from MSAccess???
By vrk in forum JDBCReplies: 3Last Post: 04-30-2009, 09:28 AM -
i need a IDE for embedded java
By santhosh_el in forum AWT / SwingReplies: 2Last Post: 04-29-2009, 08:23 AM -
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM -
Extracting embedded files
By kasturi in forum New To JavaReplies: 0Last Post: 02-07-2008, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks