I create a JTree and model for it out in a class separate to the GUI class. The data for the JTree is extracted from a file.
Now in the GUI class the user can add files from the file system to an AWT list. After the user clicks on a file in the list I want the JTree to update. The variable name for the JTree is schemaTree.
I have the following code for the when an item in the list is selected:
private void schemaListItemStateChanged(java.awt.event.ItemEvent evt) {
int selection = schemaList.getSelectedIndex();
File selectedFile = schemas.get(selection);
long fileSize = selectedFile.length();
fileInfoLabel.setText("Size: " + fileSize + " bytes");
schemaParser = new XSDParser(selectedFile.getAbsolutePath());
TreeModel model = schemaParser.generateTreeModel();
schemaTree.setModel(model);
}
I've updated the code to correspond to the accepted answer. The JTree now updates correctly based on which file I select in the list.
From the Component.add API docs.
Note: If a component has been added to
a container that has been displayed,
validate must be called on that
container to display the new
component. If multiple components are
being added, you can improve
efficiency by calling validate only
once, after all the components have
been added.
You have called repaint and validate on a component that is not displayed, which will not be effective. You need to call those methods on the mainPanel after the add. Also revalidate tends to be better than validate as it effectively coalesces.
I not sure that I'm understanding your question, but I'll try...
The right thing to do should be, IMHO:
get the file
create a new TreeModel from your file
give the model to the JTree
In pseudocode, it would look like that:
File newContent = getSelectedByUser(...);
TreeModel newModel = new MyFileBasedTreeModel(newContent);
//this next part must be done in the EventDispatcherThread
myTree.setModel(newModel);
then the JTree would be updated, without any call to repaint, etc.
Hope it helps
Related
I added an UndoManager to a JTextPane in my application, but I can't get it work:
UndoManager undoManager = new UndoManager();
textpane.getDocument().addUndoableEditListener(undoManager);
When I manually type into the text pane, then try to undo the changes, nothing ever happens undoManager.canUndo() always returns false.
I also tried another way of adding the manager as follows:
textpane.getDocument().addUndoableEditListener(new UndoableEditListener()
{
#Override
public void undoableEditHappened( UndoableEditEvent e )
{
System.out.println("UndoableEditEvent");
undoMgr.addEdit(e.getEdit());
}
});
With the above code I can see in the output window that the undoableEditHappened( UndoableEditEvent e ) is called once at the start (most likely by a read call which loads the test file). When I make changes (via keyword) or insertText(...) calls, there are no further listener calls.
I found some similar questions here in StackOverflow, but the solutions were always alongs the lines that they had custom input methods for the JTextPane, I don't ... not that I know of.
What might I have overlooked?
I found out why the UndoableEditListener wasn't triggering.
I was calling JTextPane.read(Reader reader, Object object) after I had setup the Document listeners - What I didn't know was that calling the read(...) method creates and adds a new Document model to the JTextPane, which basically removed anything I had previously done to the old Document.
Solution
Work with the Document model after calling JTextPane.read(...)
This is probably a simple question, I'm not very used to Java programming. But I need to create a dialog with a CheckboxTree (a variant of JTree with checkboxes, see http://www.javaworld.com/javaworld/jw-09-2007/jw-09-checkboxtree.html)
Please note: I have created the JDialog in the graphical environment of NetBeans, so it has generated code for adding buttons etc. So I need to know how to add this tree after the creation of the main parts, so to speak... Maybe that's the problem, because if I do something like this:
JPanel panel = new JPanel();
this.setContentPane(panel);
Then I actually see the tree showing up in the dialog, but all the buttons and all are gone...
I have been able to add it to a JFrame and an optionspane, but I want it in a custom JDialog. Could anyone please explain to me in very simple terms what I need to do?
Here are my feeble attempts so far:
Constructor for the JDialog:
public MetadataUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
Container panel = getContentPane();
panel.add(getTree());
panel.repaint();
this.pack();
}
getTree method that creates the checkboxtree:
private static CheckboxTree getTree() {
DefaultMutableTreeNode root, child;
root = new DefaultMutableTreeNode("root");
child = new DefaultMutableTreeNode("Colors");
root.add(child);
child.add(new DefaultMutableTreeNode("Cyan"));
child.add(new DefaultMutableTreeNode("Magenta"));
child.add(new DefaultMutableTreeNode("Yellow"));
child.add(new DefaultMutableTreeNode("Black"));
CheckboxTree checkboxTree = new CheckboxTree(root);
checkboxTree.setVisible(true);
return checkboxTree;
}
This repainting and all that is the last attempt based on something I found Googling, but it made no difference whatsoever, so I'm guessing I'm way off.
The simplest way to add the tree and make it visible would be appreciated. It seems to work exactly as a JTree with regards to adding it, but I cannot make it work. So even if no one has experience with this particular checkboxtree plugin, the same (simplest) code for using a JTree in a JDialog would probably do!
EDIT:
In response to Andrew, here is the same thing (my best attempt) with a regular JTree:
private static JTree getTree() {
DefaultMutableTreeNode root, child;
root = new DefaultMutableTreeNode("root");
child = new DefaultMutableTreeNode("Colors");
root.add(child);
child.add(new DefaultMutableTreeNode("Cyan"));
child.add(new DefaultMutableTreeNode("Magenta"));
child.add(new DefaultMutableTreeNode("Yellow"));
child.add(new DefaultMutableTreeNode("Black"));
JTree tree = new JTree(root);
tree.setVisible(true);
return tree;
}
EDIT 2:
In response to Maxim, I'm confused. Things that are obvious to you are probably lost on me. Borrowing some stuff from your code this is the best I could come up with (doesn't work):
public MetadataUI(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
JScrollPane s = new JScrollPane();
s.getViewport().add(getTree());
getContentPane().add(s, BorderLayout.CENTER);
setVisible(true);
}
If you indeed created your JDialog with Netbeans GUI (it's a JDialog form) and the component you are trying to integrate into what you already have provides a zero argument constructor, try the following:
open your existing JDialog in Design mode
In the Navigator view right-click [JDialog] or whatever your top-level node is (should be a sibling of "Other Components") and select Add From Palette > Beans > Choose Bean
enter a fully qualified name for the class which represents your JTree component (ex. com.example.jtree.SomeJTreeComponent) and confirm. If the component is trully a JTree it will probably get added without any problems.
(optional) At this point the tree may or may not be enclosed within a JScrollPane. If it is not, you can manually achieve this by repeating parts of step 2. on your newly added component and choosing Enclose in this time around.
You will need to program other stuff by hand. I suggest you read a JTree Tutorial or refer to the documentation of your 3rd party component to help you through it.
You might also want to read more about the tool you are using to build your GUI.
I'm binding the text value of a label to my backend model using the following code (some details omitted):
Model model = entry.getModel();
Control nameControl = formToolkit.createLabel(labelPanel, null);
IObservableValue modelValue = BeanProperties.value(model.getClass(), Model.PROPERTY_NAME).observe(model);
IObservableValue widgetValue = SWTObservables.observeText(nameControl);
DataBindingContext context = new DataBindingContext();
UpdateValueStrategy widgetToModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER);
UpdateValueStrategy modelToWidget = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
context.bindValue(widgetValue, modelValue, widgetToModel, modelToWidget);
When the model is changed, the text of the label updates appropriately. However the label is not resized. So when the model changes to a longer name, the new name is cut off. When it changes to a shorter name, the label takes up more space than it should.
I thought about adding a property change listener (either for the model name or the label text) that would call layout on the label's parent whenever the name is changed, but wouldn't that defeat the purpose of data binding?
Well you could bind the size of your control with
WidgetProperties.size().observe(nameControl);
to the text of the control (the observable you already have) and provide a IConverter in your UpdateStrategy that converts from text to size (so every time the text changes you can calculate the text and then set the size of the control).
Make sure that the size of the label is recognized by the parents layout-manager
I'm new to Java programming and am facing a (most likely) easy problem that I don't seem to be able to get across nor understand.
I have three different java files, one where I create an interface (SimulatorGui.java), other where I am creating a panel to use on the jTabbedPanel created in the interface (CollisionPanel.java - CollisionPanel class) and a third one, where I run a code that will create the output needed (Collision.java - Colision class).
In the Collision.java main method, I am doing the following:
public static void main (String[] args) {
//<editor-fold defaultstate="collapsed" desc="Simulation start procedures">
Tally statC = new Tally ("Statistics on collisions");
Collision col = new Collision (100, 50);
col.simulateRuns (100, new MRG32k3a(), statC);
//</editor-fold>
new SimulatorGUI().setVisible(true);
CollisionPanel update = new CollisionPanel();
update.updatepanel();
The first block, will create the desired output. I then want to send that output to the updatepanel! I am not passing any arguments to the method as I am still trying to debug this. updatepanel method is created in the file CollisionPanel as following:
public void updatepanel(){
System.out.println ("debug");
jTextArea1.setText("update\n");
}
What happens then is that when I run the Collision.java file it will output the "debug" text but won't set the text to the jTextArea1 (append doesn't work aswell). I then created a button to try and do so and in that case it works. In CollisionPanel.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
updatepanel();
}
This does the trick! I've searched and tried different things but can't seem to understand why this won't work.
Thanks in advance for your help, hope I've made the problem clear!
Okay I think I've eventually got the problem, and it is because of the IDE, you see in your main method you initiate a new CollisionPanel, which is wrong, netbeans has already added and initiated that panel in the SimulatorGUI, so now what you need to do is add a get method in the SimulatorGUI to get the initiated panel, then call the update method on that panel.
So add this to SimulatorGUI:
public CollisionPanel getCollisionPanel1() {
return collisionPanel1;
}
replace your old updatePanel() method with:
void updatepanel(String str) {
System.out.println ("debug");
jTextArea1.setText(str);
// jTextArea1.revalidate();
jLabel1.setText("test");
}
after that change your main too look like this:
SimulatorGUI simulatorGUI = new SimulatorGUI();
simulatorGUI.setVisible(true);
CollisionPanel cp=simulatorGUI.getCollisionPanel1();
cp.updatepanel("Hi");
and dont forget to remove the old updatePanel() method call from your CollisionPanel constructor, because now you can simply call cp.updatePanel("text here"); in your SimulatorGUI class instead of calling it only in the constructor.
I hope this is easy to grasp, if you're unsure let me know
Where do you add your CollisionPanel to the main GUI? I fear that this is your problem, and that you need to do this for your code to work. In fact where do any of your three classes get a reference to the others? For different classes to work in a program, there must be some communication between them. Understand that if you create a CollisionPanel object inside the GUI, and create another CollisionPanel object inside of the main method, calling a method on one object will have no effect on the other since they are two completely distinct entities.
For example, this code:
new SimulatorGUI().setVisible(true);
CollisionPanel update = new CollisionPanel();
update.updatepanel();
It appears that you are in fact calling updatePanel() on a CollisionPanel, but it's not on any CollisionPanel that is visualized in your GUI.
Consider giving SimulatorGUI a method that allows one to pass the CollisionPanel into it so that it can use it. This may in fact be a constructor parameter:
CollisionPanel update = new CollisionPanel();
SimulatorGUI simulatorGUI = new SimulatorGUI(update);
update.updatePanel();
Meaning SimulatorGUI's constructor would have to look something like:
public SimulatorGUI(CollisionPanel update) {
this.update = update;
// add update to GUI somewhere
}
There are three different levels when developping a GUI:
The view: the graphical component
The Model: the code that you run
The controller: checks if are update on the model in order to refresh the view.
So When you first start your program, the view will have the value assigned in the code; for instance say you created your JTextArea with the initial value type here. The view will show the JTextArea with the text type here.
When a change is made to the model, the view is not aware of it, it is job of the controller to check for update on the model and then refresh the view.
So this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
updatepanel();
}
Will generate an event that say a property has been modify. So the controller will update the view.
Other than that, the change will not appear on the view.
Hope this helps..
I am creating a web page with several tabs. To implement that I am using wicket AjaxTabbedPanel and several AbstractTab. In each tab I have tables with data and I am using a javascript script to make the tables sortable.
public TabbedPage() {
List<ITab> tabs = new ArrayList<ITab>();
tabs.add(new AbstractTab(new Model<String>("first tab")) {
public Panel getPanel(String panelId) {
return new TablePanel(panelId);
}
});
tabs.add(new AbstractTab(new Model<String>("second tab")) {
public Panel getPanel(String panelId) {
return new TablePanel(panelId);
}
});
add(new AjaxTabbedPanel("tabs", tabs));
}
When I load the page the table in the tab selected by default is sortable. However, as soon as I click any of the links to jump to other tabs (including the one of the tab already selected), none of the tables in any of the tabs allows me sort them (including the one that was previously working - the table in the default tab). If I refresh the page I can sort the table (of the tab selected in the moment of the refresh), but as soon as I click in any of links to switch tabs, the tables stop having the sortable capability again. Any ideas of why is this happening?
EDIT:
I just found that if I replace the AjaxTabbedPanel by TabbedPanel I don't have this problem. Although I'm still not sure why is that. Can anyone enlighten me?
add(new TabbedPanel("tabs", tabs));
Sorting the table by JavaScript is most likely a function called with a specific DOM-Id and seems to be executed 'onLoad'. it then accesses the currently displayed table and does it's work.
Changing the content of your Panel by Ajax doesn't trigger 'onLoad' so the function isn't executed again. TabbedPanel reloads the page and therefore executed your script.
Selecting a previous sortable table with AjaxTabbedPanel doesn't work because of the dynamically generated DOM-Ids.
Your solution is to add a AjaxCallDecorator to the links from AjaxTabbedPanel or to include the script or at least the function call to your tabbed panels.
At least this it what comes to mind without seeing any sources...
EDIT:
You might want to look at The Wicket Wiki. There's a description on how to call js after clicking an AjaxLink. That's exactly what should solve your problem.
Summary: Just add
link.add(new AttributeAppender("onclick", new Model("myTableSortingScript();"), ";"));
to the links generated by AjaxTabbedPanel.
In Wicket 6.0 you can run JavaScript on a component basis: Just override renderHead(IHeaderResponse response) for your component:
#Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
response.render(new OnLoadHeaderItem("initalizeMe(\"" + getMarkupId() + "\");"));
}
initializeMe(mycomponentId) is executed every time the component is loaded by the AjaxTabbedPanel. This also works with the standard TabbedPanel.
No real idea since I'm not sure what the code is doing but I had a similar problem with my Panel Manager. Basically if you dynamically load HTML into a panel (a div or another element) using "innerhtml" script in the content will not be executed.
To get around this I scan the loaded content for "script" tags and append them using the DOM methods - this does run the script and makes it available. My "load" method is:
// Load Content
Panel.load = function(Content) {
// "null" the old (to clear it)
Panel.innerHTML = null;
// Load the content
Panel.innerHTML = Content;
// Load Scripts
var AllScripts = Panel.getElementsByTagName("script");
var AllScriptsCnt = AllScripts.length;
for (var Cnt = 0; Cnt < AllScriptsCnt; Cnt++){
var CurScript = document.createElement('script');
CurScript.type = "text/javascript";
CurScript.text = AllScripts[Cnt].text;
Panel.appendChild(CurScript);
};
};
Again, not sure if this is the issue, but it sounds pretty much on target from my experience.