I am creating a Jtree with a rootNode and than create another thread that update the root node asynchronously.
It works fantastic if i run this Jtree independently in some JPanel, it was even working at some place in the project, but i was asked to have this Jtree in some new swing Component.
In new Swing Panel, it doesnot populate fully, it only populate the nodes that were inserted (at start for few milliseconds) before the Jtree was rendered on the Screen. Once the Jtree is rendered it doesnt get updated.
Now the interesting part i also made a mouse listener on the node so that i can create a new node by right click create node function, and with that new node is created and is added on the Jtree root node.
Important thing to add i was using newThread(){void run}).start() method to create a thread to add node on the Jtree, becuase i never felt the need of SwingUtilities.invokeLater method before. but now if i used SwingUtilities.invokeLater method than the main window also doesnot open it just halts during the startup, i just checked that SwingUtilities.invokeLater also works fine with the old component and ofcourse works fine independently.
And i do call model.nodeStructureChanged(changedNode); after adding the node thats why it was working fine before.
Kindly asist,
code is difficult to extract and Jtree code was working fine before, may be some component block the containing widgets to refresh itself async?
EDIT
Update to include some code, i am using Temp class as provided by Nick:-
public BasicGraphEditor(String appTitle, mxGraphComponent component)
{
// Stores and updates the frame title
this.appTitle = appTitle;
// Stores a reference to the graph and creates the command history
graphComponent = component;
final mxGraph graph = graphComponent.getGraph();
undoManager = createUndoManager();
// Do not change the scale and translation after files have been loaded
graph.setResetViewOnRootChange(false);
// Updates the modified flag if the graph model changes
graph.getModel().addListener(mxEvent.CHANGE, changeTracker);
// Adds the command history to the model and view
graph.getModel().addListener(mxEvent.UNDO, undoHandler);
graph.getView().addListener(mxEvent.UNDO, undoHandler);
// Keeps the selection in sync with the command history
mxIEventListener undoHandler = new mxIEventListener()
{
#Override
public void invoke(Object source, mxEventObject evt)
{
List<mxUndoableChange> changes = ((mxUndoableEdit) evt
.getProperty("edit")).getChanges();
graph.setSelectionCells(graph
.getSelectionCellsForChanges(changes));
}
};
undoManager.addListener(mxEvent.UNDO, undoHandler);
undoManager.addListener(mxEvent.REDO, undoHandler);
// Creates the graph outline component
graphOutline = new mxGraphOutline(graphComponent);
// Creates the library pane that contains the tabs with the palettes
libraryPane = new JTabbedPane();
/////////////////////////////////////////////////
// Only change i have done here: start
////////////////////////////////////////////////
Temp tempExplorer = new Temp();
libraryPane.add("new Explorere", tempExplorer);
/////////////////////////////////////////////////
// Only change i have done here: End
////////////////////////////////////////////////
// Creates the inner split pane that contains the library with the
// palettes and the graph outline on the left side of the window
JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
libraryPane, graphOutline);
inner.setDividerLocation(320);
inner.setResizeWeight(1);
inner.setDividerSize(6);
inner.setBorder(null);
// Creates the outer split pane that contains the inner split pane and
// the graph component on the right side of the window
JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner,
graphComponent);
outer.setOneTouchExpandable(true);
outer.setDividerLocation(200);
outer.setDividerSize(6);
outer.setBorder(null);
// Creates the status bar
statusBar = createStatusBar();
// Display some useful information about repaint events
installRepaintListener();
// Puts everything together
setLayout(new BorderLayout());
add(outer, BorderLayout.CENTER);
add(statusBar, BorderLayout.SOUTH);
installToolBar();
// Installs rubberband selection and handling for some special
// keystrokes such as F2, Control-C, -V, X, A etc.
installHandlers();
installListeners();
updateTitle();
}
The above class is from Jgraph library as https://github.com/jgraph/jgraphx
And i am just adding the jtree component like as above, no other changes.
please help.
Swing isn't thread safe unless explicitly stated. In the JavaDocs for JTree, it explicitly says this isn't thread safe. If you update it in a thread outside the EDT, there's no guarentee anything will work. So if you want to update a JTree from a different Thread, you need to use SwingUtilities.invokeLater(Runnable run); in order to put the request on the EDT.
I'd recommend having a data structure to store the info of the JTree, and only using the JTree for User Interaction (not data storage).
EDIT
Here's an example of using SwingUtilities.invokeLater() to update a JTree while in the component model. Without you posting any code, this is the best I have to work with. Please try to use this to recreate your problem (add pieces of your code to this example until you have narrowed down what the problem is).
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Temp extends JPanel{
JTree tree = new JTree();
public Temp(){
JScrollPane jsp = new JScrollPane(tree);
// Creates the library pane that contains the tabs with the palettes
JTabbedPane libraryPane = new JTabbedPane();
libraryPane.add("new Explorere", jsp);
// Creates the inner split pane that contains the library with the
// palettes and the graph outline on the left side of the window
JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
libraryPane, new JPanel());
inner.setDividerLocation(320);
inner.setResizeWeight(1);
inner.setDividerSize(6);
inner.setBorder(null);
// Creates the outer split pane that contains the inner split pane and
// the graph component on the right side of the window
JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner,
new JPanel());
outer.setOneTouchExpandable(true);
outer.setDividerLocation(200);
outer.setDividerSize(6);
outer.setBorder(null);
// Puts everything together
setLayout(new BorderLayout());
add(outer, BorderLayout.CENTER);
}
public static void main(String[] args) {
final Temp temp = new Temp();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(temp);
frame.pack();
frame.setVisible(true);
}});
Thread updater = new Thread(temp.new CustomThread());
updater.start();
}
public class CustomThread implements Runnable{
#Override
public void run() {
for(int i = 0; i < 1000; i++){
updateTree("New Item "+ i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void updateTree(final String nodeToAdd){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeToAdd);
model.insertNodeInto(child, root,root.getChildCount());
tree.scrollPathToVisible(new TreePath(child.getPath()));
}});
}
}
}
Related
I'm working on an RCP application that's in a transition from a Swing version. So we have a lot of UI components that still need to live in the Swing world during this transition. I'm able to properly place the existing Swing components in AWT-SWT bridge frames.
I've wrapped these Swing components in a JScrollable pane before adding them to the bridge so that I don't have to resize the containing-part when the size of the Swing UI elements change. The code where I place an old Swing component in a part looks like this:
#PostConstruct
public void postConstruct(final Composite parent) {
/* Create embedding composite */
final Frame bridgeFrame;
final Composite embed;
embed = new Composite(parent, SWT.EMBEDDED);
embed.setLayout(new FillLayout());
bridgeFrame = SWT_AWT.new_Frame(embed);
bridgeFrame.setLayout(new BorderLayout());
bridgeFrame.add(
new JScrollPane(getTestPanel()),
BorderLayout.CENTER);
}
My Swing component has a behavior where when the user clicks a button, things that were hidden in the component are made visible, or new UI elements are added to the Swing component. For example:
private JPanel getTestPanel() {
final JPanel output;
final JButton eastBttn, westBttn;
output = new JPanel();
eastBttn = new JButton("East Button");
westBttn = new JButton("West Button");
output.setLayout(new BorderLayout());
output.add(eastBttn, BorderLayout.EAST);
output.add(westBttn, BorderLayout.WEST);
eastBttn.addActionListener(evt -> {
System.out.println("East Button Clicked");
output.add(new JLabel("East Button Clicked"), BorderLayout.CENTER);
});
return output;
}
My problem is, when the elements in the Swing-component change, the parent bridge-frame doesn't properly get rendered.
When the parts are first created, my application looks like this:
After I click on the EastButton it's supposed to add a text label in the center of that bridge frame. However, nothing changes in the application view.
But, when I even begin to resize the containing part-sash a little, the part containing the bridge-frame updates correctly:
What can I do to make the bridge-frame update containing part update automatically?
To test whether this was a repainting issue on the bridge-frame, I had a menu item which would trigger a repaint / revalidate / pack of the bridge-frame, but that didn't solve the issue. I suspect it has something to do with the renderer of the containing part, but have no idea how to go about addressing it.
The same a problem exists in a pure Swing solution:
public static void main(String[] args) {
JFrame bridgeFrame = new JFrame("Test");
bridgeFrame.setSize(400, 400);
bridgeFrame.setLayout(new BorderLayout());
bridgeFrame.add(new JScrollPane(getTestPanel()), BorderLayout.CENTER);
bridgeFrame.setVisible(true);
}
You need to add an output.doLayout() in your event handler.
I eventually got around the problem by attaching a custom ControlListener / ComponentListener to the part containing the bridge. If any changes within the workings of the bridge-frame caused it to resize to beyond the parent, I'd have the listener resize it to fit within the parent thus forcing the scroll-pane to take over.
Here's my listener:
public class BridgeComponetAdapter
extends ComponentAdapter
implements ControlListener {
private final Composite parent;
private final Frame bridgeFrame;
private Point parentSize;
public BridgeComponetAdapter(
final Composite parent,
final Frame bridgeFrame) {
this.parent = parent;
this.bridgeFrame = bridgeFrame;
bridgeFrame.addComponentListener(this);
parent.addControlListener(this);
}
#Override
public void componentResized(final ComponentEvent e) {
System.out.println(e);
if (e.getSource() != bridgeFrame)
return;
final Dimension currentBridgeSize;
currentBridgeSize = bridgeFrame.getSize();
if (currentBridgeSize.getWidth() > parentSize.x
|| currentBridgeSize.getHeight() > parentSize.y) {
bridgeFrame.setSize(parentSize.x, parentSize.y);
}
}
#Override
public void controlMoved(final ControlEvent e) {}
#Override
public void controlResized(final ControlEvent e) {
System.out.println(e);
if (e.getSource() == parent)
parentSize = parent.getSize();
}
}
It's not an elegant solution; I'm still open to other ideas to solve the problem.
I have a JPanel holding a JButton and JScrollPane (in turn holding a JTable) and am currently running into two issues which I believe are related:
The JButton listener's actionPerformed() method is not invoked upon click. The only way in which I can get it to be invoked is by calling doClick() on the JButton. The JButton color changes upon hover but no click animation is shown when the mouse is pressed.
Secondly, if a cell is clicked within the JTable, the cell located 2 rows down in the same column registers the click instead. This offset does not occur when clicking in the column headers (i.e. to adjust cell widths), only when within the cell area.
Left-hand panel. Click position circled
public class InventoryPanel extends JPanel {
// Parent Business object reference for communication and JFrame
private Business parent;
private AddItemPanel addItemPanel;
// Inventory table items
private DefaultTableModel inventoryModel;
private JTable inventoryTable;
private JScrollPane inventoryScrollPane;
private JLabel updateLbl;
private JButton addItemBtn;
// Columns for inventory table
private static final String[] INVENTORY_COLUMNS = {"Item","Stock","Restocking Level","Edit"};
public InventoryPanel(Business parent) {
this.parent = parent;
initGUI();
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
//doStuff
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace(new PrintStream(System.out));
}
}
}
}).start();
}
// INITIALISES GUI
public void initGUI() {
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JLabel titleLabel = new JLabel("<html><B>Inventory</B></html>");
this.add(titleLabel);
// Create empty inventory table
inventoryModel = new DefaultTableModel(new Object[3][4],INVENTORY_COLUMNS);
inventoryTable = new JTable(inventoryModel);
inventoryScrollPane = new JScrollPane(inventoryTable);
// Create button to allow items to be added
addItemBtn = new JButton("Add item");
addItemBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("ADD ITEM PRESSED");
}
});
updateLbl = new JLabel("Loading inventory...");
this.add(addItemBtn);
this.add(inventoryScrollPane);
this.add(updateLbl);
}
I've tried removing the table from the panel to see if that solves the JButton issue and visa-versa, but no luck. I've also tried changing the project JDK but no luck there either.
There are other JPanels adjacent to the troublesome one in a JFrame which work perfectly fine. Any ideas?
Edit: I can create a working instance of the InventoryPanel alone in a frame in another project, as mentioned in the comments. However the exact same code (no calls being made to other objects/methods) in the current project now produces ClassCastExceptions. After some googling this seems to be due to non-EDT threads updating the GUI.
However there is no use of the Business class, and all GUI operations are performed using the SwingUtilities.invokeLater() method like so:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("test");
frame.add(new InventoryPanel());
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
Note: the no-argument constructor InventoryPanel() just calls initGUI().
Thanks for the help so far...still very confused by this.
I've been having trouble trying to get the table to scroll; it just won't. I've looked at other stack answers and tried them, but they aren't working; and I'm not sure if I have something conflicting with those solutions.
tableModel = new TableModel(); //Custom Table Model
table = new JTable();
table.setBorder(null);
table.setFillsViewportHeight(true);
table.setModel(tableModel);
JScrollPane tblScrollPane = new JScrollPane(table);
tblScrollPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
tblScrollPane.setBounds(245, 17, 560, 425);
frmServerAdministration.getContentPane().add(tblScrollPane);
EDIT: More info
So a window opens, which is the server program. Client programs connect to the server and when they do, a method is triggered in my table model class which adds a new row into the table. (I can see the row) Then at the end of that method it calls another but nothing changes in the ScrollPane. Do I need to do some kind of repainting? -
Server.updateTableScroll();
public static void updateTableScroll() {
System.out.println("Here"); //NOTE: I do see this printed out
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
EDIT 2: Thoughts
So in Eclipse I use Window Builder to make the GUI, and the following for loop will display the table with the scrollbar! But when I run the same addClient() method at another point, then the scroll bar won't appear.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server window = new Server();
window.frmServerAdministration.setVisible(true);
for(int i = 0; i < 20; i++){
tableModel.addClient(i, String.valueOf(i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Instead of setBounds(), override getPreferredScrollableViewportSize(), as suggested here, and pack() the enclosing top-level container. Also, the API authors "recommend that you put the component in a JPanel and set the border on the JPanel."
Addendum: As a JTable listens to its TableModel, verify that the correct event is fired from the model. If you extend AbstractTableModel, one of the fireTableXxx() methods will be appropriate.
All I needed to do was call this after I added data (I'm very new to table models :P )
this.fireTableStructureChanged();
Ok, I'm trying to add the World Wind globe from NASA to a GUI window created by NetBeans GUI builder. My sample code instantiates its own window, and the GUI builder would like me not to edit the areas necessary to slip this in :) I'd write my own but this is part of a NetBeans platform app and contains code and annotations Im not prepared to handle yet. I am not sure how to accomplish this. Here is the sample code I would like in the window:
public class WorldWindTest {
public static void main(String[] args) {
//create a WorldWind main object
WorldWindowGLCanvas worldWindCanvas = new WorldWindowGLCanvas();
worldWindCanvas.setModel(new BasicModel());
Position myPoint = Position.fromDegrees(31.12, -88.64, 35000);
//build Java swing interface
JFrame frame = new JFrame("World Wind");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(worldWindCanvas);
frame.setSize(800,600);
frame.setVisible(true);
//create some "Position" to build a polyline
LinkedList<Position> list = new LinkedList<Position>();
// list.add(Position.fromDegrees(i,0.0,i*20000));
}
list.add(Position.fromDegrees(30.12, -85.64, 35000));
list.add(Position.fromDegrees(31.12, -88.64, 35000));
//create "Polyline" with list of "Position" and set color / thickness
Polyline polyline = new Polyline(list);
polyline.setColor(Color.RED);
polyline.setLineWidth(3.0);
//create a layer and add Polyline
RenderableLayer layer = new RenderableLayer();
layer.addRenderable(polyline);
//add layer to WorldWind
worldWindCanvas.getModel().getLayers().add(layer);
}
}
To amplify on my comment, I was thinking that you could create a class, say called SetUpWorldWindowGLCanvas, and in it, initialize and set up your WorldWindowGLCanvas object, and then give it a public getter method that would allow you to obtain the set up WorldWindowGLCanvas object. i.e.,
public class SetUpWorldWindowGLCanvas {
WorldWindowGLCanvas worldWindCanvas = new WorldWindowGLCanvas();
public SetUpWorldWindowGLCanvas() {
worldWindCanvas.setModel(new BasicModel());
Position myPoint = Position.fromDegrees(31.12, -88.64, 35000);
// ... etc
}
public WorldWindowGLCanvas getWwGlCanvas() {
return worldWindCanvas;
}
}
And then place this BorderLayout.CENTER in a JPanel that was created in your GUI builder and that uses BorderLayout as its layout manager.
Instead of using the GUI editor for your entire application, limit it's use just the few containers that will most benefit from it, e.g. difficult layouts. Then your WorldWindowGLCanvas can be added normally to your top-level container. In this example, WorldWindowGLCanvas would appear alongside NewJPanel:
JFrame f = new JFrame();
f.setLayout)new GridLayout(1, 0);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(worldWindCanvas);
f.add(new NewJPanel());
f.pack();
f.setVisible(true);
I am using netBeans editor to create desktop application . and i want to add Component without using drag and drop way. I am trying code like this for adding JList to JPanel but nothing showed
JList jl = new JList();
Vector<String> v= new Vector<String>();
v.add("one");
v.add("Two");
v.add("Three");
jl.setListData(v);
JScrollPane js = new JScrollPane(jl);
js.setLocation(50, 50);
js.setSize(100, 100);
js.setVisible(true);
jPanel1.add(js);
I want to add Component without using drag and drop way.
Here's a simple JList example that doesn't use the NetBeans' GUI editor. See How to Use Lists for more.
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class JListTest {
private static final Random random = new Random();
public static final void main(String args[]) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
final DefaultListModel dlm = new DefaultListModel();
for (int i = 0; i < 1000; i++) {
dlm.addElement("Z" + (random.nextInt(9000) + 1000));
}
final JList list = new JList(dlm);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(list), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
The scroll list doesn't appear, or the data items in the list? Also, you're setting the position manually. Seriously, don't do that -- use a layout manager, many of which are available and you can easily use in the Netbeans GUI editor Mattise.
If the main window is under the control of a layout manager and then you add something to it that specifies its position and size, all mayhem will break loose. Namely, the layout manager will overwrite this, possibly with the result of size becoming 0, 0.
What you need to do is create a JPanel in your layout manager to hold the position of the new component and make sure it has a known field name you can reference and use to add to. Make sure that Panel also has FlowLayout or something in the properties.
you may want to call repaint() when you dynamically create GUI elements.