expand and collapse icons - java

As you can see from the images below the expand and collapse icons are gray, as is the row selection highlight. This causes you to not see the expand or collapse icon (Note: Not the folder icon) when the row is highlighted, I want to have a white expand or collapse icon for the row that is selected. How can that be done?
Something else that would also be cool is, to have the expand and collapse icons completely hidden until the JTree gains focus. like windows 7's tree.

Google says -according to this post: http://www.exampledepot.com/egs/javax.swing.tree/DefIcons.html - :
// Retrieve the three icons
Icon leafIcon = new ImageIcon("leaf.gif");
Icon openIcon = new ImageIcon("open.gif");
Icon closedIcon = new ImageIcon("closed.gif");
// Create tree
JTree tree = new JTree();
// Update only one tree instance
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
renderer.setLeafIcon(leafIcon);
renderer.setClosedIcon(closedIcon);
renderer.setOpenIcon(openIcon);
// Remove the icons
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
// Change defaults so that all new tree components will have new icons
UIManager.put("Tree.leafIcon", leafIcon);
UIManager.put("Tree.openIcon", openIcon);
UIManager.put("Tree.closedIcon", closedIcon);
// Create tree with new icons
tree = new JTree();
// Update row height based on new icons;
Certainly, I'm not sure if you can modify only the color of the images on-the-go. But you can always create new icons, right?

You can try this. You should note however to get this to work, I had to override setUI on the tree to only allow my TreeUI.
private class IconTreeUI extends BasicTreeUI {
private Icon collapseIcon = null;
private Icon expandIcon = null;
#Override
public Icon getCollapsedIcon() {
if (collapseIcon == null) {
collapseIcon = new ImageIcon(yourCollapseImageHere);
}
return collapseIcon;
}
#Override
public Icon getExpandedIcon() {
if (expandIcon == null) {
expandIcon = new ImageIcon(yourExpandImageHere);
}
return expandIcon;
}}

Related

Changing colour of all buttons in netbeans

I've got 4 Buttons on my Form and a menu bar with an option to change their colours. In my code I change colour of each one individually, like this:
jButton1.setBackground(Color.cyan);
jButton2.setBackground(Color.cyan);
jButton3.setBackground(Color.cyan);
jButton4.setBackground(Color.cyan);
Which isn't a problem right now but might become one if I add more of them. So is there a way to change the colour of all present buttons at once?
You can try to create an array of jbuttons such as:
JButton[] buttonsArr = new JButton[4];
and then you can loop on the items and set color of text for all of them.
Such as:
for(int i = 0;i < 4;i++){
buttonsArr[i] = new JButton(String.valueOf(i));
// Or you can add the color such as
buttonsArr[i].setBackground(Color.cyan);
}
Another solution is to declare a Color Variable and use it as a global variable or as Enum such as:
Color globalColor = new Color(187, 157, 177);
jButton1.setBackground(globalColor);
jButton2.setBackground(globalColor);
jButton3.setBackground(globalColor);
jButton4.setBackground(globalColor);
And whenever you need to change it you can change it easily by changing it is value.
Check those links for more help:
Link_1 & Link_2
Assume you have this interface
and you want to change color of all buttons to any other color you want.
My recommendation is to create a recursive method similar to DFS algorithm that can scan all components in your interface and find all buttons even if each button is a child for another component.
Here is an example:-
private List<Component> getAllButtons(Component[] components) {
List<Component> buttons = new LinkedList();
for (Component component: components) {
if (component instanceof JButton) {
buttons.add(component);
} else {
JComponent jComponent = ((JComponent) component);
buttons.addAll(getAllButtons(jComponent.getComponents()));
}
}
return buttons;
}
This method needs to pass the parents components of the interface and it will scan each component either it's a button or not, if it's a button! it will be added to the list, if not! it will get the children's of this component and scan each one recursively until get all buttons.
Now, to call this method, you should add listeners to Button Color items. For me, i prefer to create a one action listener for all these items.
private void actionPerformed(java.awt.event.ActionEvent evt) {
String colorName = ((JMenuItem) evt.getSource()).getText();
Color color = null;
if (colorName.equals("red")) {
color = Color.RED;
} else if (colorName.equals("green")) {
color = Color.GREEN;
} else if (colorName.equals("blue")) {
color = Color.BLUE;
} else if (colorName.equals("cyan")) {
color = Color.CYAN;
}
List<Component> buttons = getAllButtons(this.getComponents());
for (Component component : buttons) {
component.setBackground(color);
}
}

JavaFX - Notify when user drags mouse from one Node to another?

I have the following basic GUI for demonstration:
I'm trying to achieve the following functionality but I've exhausted all avenues that I've attempted.
User can left click on any of the ImageView's and it will create an
arrow that follows the user's cursor around until the user let's go of
the mouse button. (arrow start x,y is where he clicked and end x,y is
where his mouse currently is) If the user clicked on the Red
ImageView and dragged it over the Blue ImageView and then let go,
the program would print User just clicked from R to B
If the user clicked on the Red ImageView and let go of the mouse but
was not over a different ImageView, the program would print User
just clicked from R but did not target a different ImageView.
Under all circumstances, the arrow will appear when the user clicks on
the ImageView and will disappear the second he lets go of the mouse.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import java.util.HashMap;
public class Test extends Application
{
public static int HEIGHT = 500, WIDTH = 600;
#Override
public void start(Stage primaryStage) throws Exception
{
ImageView blue = new ImageView(new Image("blue.png")),
red = new ImageView(new Image("red.png")),
dark = new ImageView(new Image("dark.png"));
// Final array as to bypass the `final` requirement of event handler inner classes.
final ImageView[] hoveredOver = new ImageView[1];
final Line[] linePtr = new Line[1];
linePtr[0] = new Line();
linePtr[0].setStrokeWidth(10);
HashMap<ImageView, Character> lookup = new HashMap<ImageView, Character>(3)
{{
put(blue, 'B');
put(red, 'R');
put(dark, 'D');
}};
for (ImageView i : new ImageView[] { blue, red, dark })
{
i.setFitWidth(150);
i.setFitHeight(150);
// Set the anchor points of the click and display the arrow.
i.setOnMousePressed(e -> {
linePtr[0].setStartX(e.getX());
linePtr[0].setStartY(e.getY());
linePtr[0].setVisible(true);
});
// Move the arrow as the mouse moves.
i.setOnMouseDragged(e -> {
linePtr[0].setEndX(e.getX());
linePtr[0].setEndY(e.getY());
});
i.setOnMouseReleased(e -> {
// Not null means that the user WAS actually just now hovering over an imageview.
if (hoveredOver[0] != null)
System.out.printf("The user clicked from %c to %c!\n", lookup.get(i), lookup.get(hoveredOver[0]));
// Null means the user is not over an ImageView.
else
System.out.printf("The user initially clicked %c but did not drag to another Imageview.\n", lookup.get(i));
linePtr[0].setVisible(false);
});
// If the user enters ANY of the ImageViews,
// Set a variable so that the drag release listener
// can know about it!
i.setOnMouseDragOver(e -> hoveredOver[0] = i);
i.setOnMouseDragExited(e -> hoveredOver[0] = null);
}
blue.setX(400);
blue.setY(250);
red.setY(300);
red.setX(50);
/*
In this example I'm using a Pane but in my real program
I might be using a VBOX HBOX etc where I cannot freely move stuff around as I'd like.
This makes things extremely difficult and without using a 'Pane'
I don't know how this can even be done. Suggestions?
*/
Pane pneRoot = new Pane(blue, red, dark, linePtr[0]);
Scene scene = new Scene(pneRoot, WIDTH, HEIGHT);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
This was my best attempt and it's not even close. It moves a line (not an arrow, and ideally I want my arrow to curve as it moves much like this example image from a popular video game) but does not suit my needs. It cannot detect however when I let go while 'dragging over' an ImageView.
Is there a better way to do this? I feel like I can't simply the code I have down any further but there MUST be another way.
Java is an object-oriented language. The basic idea is that you create classes to represent the data you are modeling and then create objects from those classes. If you are tying things together with arbitrary maps to look things up, and arrays kicking around for no apparent reason, you are starting in the wrong place.
JavaFX has a system of observable properties. These wrap objects in a mutable way and can be observed so you can respond to changes.
Make sure you read and understand the documentation on MouseEvents and MouseDragEvents. There are three different modes for handling dragging. For events (mouse drag events) to be sent to nodes other than the one on which the drag was initiated during a mouse drag, you need to be in full "press-drag-release gesture" mode. You can activate this mode by calling startFullDrag() on the node when responding to a dragDetected event.
I would start with something like
public class NamedDragAwareImageView {
private final ObjectProperty<NamedDragAwareImageView> source ;
private final ObjectProperty<NamedDragAwareImageView> destination ;
private final String name ;
private final ImageView imageView ;
public NamedDragAwareImageView(ObjectProperty<NamedDragAwareImageView> source,
ObjectProperty<NamedDragAwareImageView> destination,
String name, String resource) {
this.source = source ;
this.destination = destination ;
this.name = name ;
this.imageView = new ImageView(new Image(resource));
imageView.setOnDragDetected(e -> {
source.set(this);
destination.set(null);
imageView.startFullDrag();
});
imageView.setOnMouseDragReleased(e -> {
if (source.get() != null && source.get() != this) {
destination.set(this);
}
});
// other image view config...
}
public ImageView getView() {
return imageView ;
}
public String getName() {
return name ;
}
}
Then you can do things like:
// observable properties to represent start and end nodes for drag:
ObjectProperty<NamedDragAwareImageView> source = new SimpleObjectProperty<>();
ObjectProperty<NamedDragAwareImageView> destination = new SimpleObjectProperty<>();
Pane root = new Pane();
// create your named image views, referencing the source and destination
// and add their image views to root, e.g.
NamedDragAwareImageView red = new NamedDragAwareImageView(source, destination, "Red", "red.png");
root.getChildren().add(red.getView());
// recommend using SVG paths (i.e. javafx.scene.shape.Path) for the arrow
// easy to draw programmatically, easy to manipulate elements etc:
Path arrowHead = new Path();
MoveTo arrowHeadStart = new MoveTo();
arrowHead.getElements().add(arrowHeadStart);
arrowHead.getElements().addAll(/* draw an arrow head with relative path elements... */);
arrowHead.setVisible(false);
// avoid arrowHead interfering with dragging:
arrowHead.setMouseTransparent(true);
// this will contain a MoveTo and a bunch of LineTo to follow the mouse:
Path arrowLine = new Path();
arrowLine.setMouseTransparent(true);
root.getChildren().addAll(arrowHead, arrowLine);
// change listener for source. source is set when drag starts:
source.addListener((obs, oldSource, newSource) -> {
if (newSource == null) return ;
arrowHeadStart.setX(/* x coord based on newSource */);
arrowHeadStart.setY(/* similarly */);
arrowHead.setVisible(true);
});
// change listener for destination. destination is only set
// when drag complete:
destination.addListener((obs, oldDestination, newDestination) -> {
if (newDestination != null) {
System.out.println("User dragged from "+source.get().getName()+
" to "+destination.get().getName());
}
});
root.setOnMouseDragOver(e -> {
if (source.get()==null && destination.get()!=null) {
// update arrowStart position
// add line element to arrowLine
}
});
root.setOnMouseReleased(e -> {
// clear arrow:
arrowHead.setVisible(false);
arrowLine.getElements().clear();
});

JFace Drag&Drop items highlight

I'm having problems understanding how a TreeViewer's item is highlighted while a user is dragging an item.
Here's what happens: I start dragging the bottom item within the Treeviewer, and the items next to it highlight accordingly. The problem is, I can't get the highlighted item from the DragOver event. But if i drop the item from this position, the event in Drop method will have the "item" field holding the highlighted item. The tree's selection isn't changed when the highlight occurs
What i want to do: I want to change the image of the pointer according to the highlighted item. The problem is I don't know how to understand which one is highlighted. Another mistery to me is that in the Drop method the highlighted item will be the target of the drop (the secont Field from the top, in this case). I do not want to use SWT.FULL_SELECTION
Here's the image:
Source snippets (what i want is the functionality of DragOver in cases when I'm not directly hovering over an item)
final DropTarget valuesTarget = new DropTarget(tree, DND.DROP_MOVE);
valuesTarget.addDropListener(new DropTargetAdapter()
#Override
public void dragOver(DropTargetEvent event)
{
if (transfer.isSupportedType(event.currentDataType))
{
final DropTarget target = (DropTarget)event.widget;
final Tree tree = (Tree)target.getControl();
final Point relativeDropPoint = getRelativeDropPoint(event);
final TreeItem targetItem = tree.getItem(relativeDropPoint);
if (targetItem != null)
{
event.feedback =
DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
if (event.item.getData() instanceof NotAcceptableClass)
{
event.detail = DND.DROP_NONE;
}
}
}
}
private Point getRelativeDropPoint(DropTargetEvent event)
{
final Tree tree = (Tree)((DropTarget)event.widget).getControl();
final Point tableLocation = tree.toDisplay(0, 0);
return new Point(event.x - tableLocation.x, event.y
- tableLocation.y);
}
Take the TreeItem directly from DropTargetEvent.item.
If you would be using JFace TreeViewer with associated content and label providers then you could use ViewerDropAdapter, which would take care of resolving the item.

JColorChooser: Save/restore recent colors in Swatches panel

I am using a JColorchooser at various places in an application. There can be multiple instances of the panel that can invoke a JColorChooser.
The "Swatches" panel in the chooser has an area of "recent" colors, which only persists within each instance of JColorChooser. I would like to (a) have the same "recent" colors in all my choosers in my application, and (b) to save the colors to disk so that these colors survive close and restart of the application.
(At least (a) could be solved by using the same single chooser instance all over the whole app, but that apears cumbersome because I would need to be very careful with attached changelisteners, and adding/removing the chooser panel to/from various dialogs.)
I did not find any method that lets me set (restore) these "recent" colors in the chooser panel. So to me, it appears that the only ways of achieving this would be:
serialize and save / restore the whole chooser (chooser panel?)
or
create my own chooser panel from scratch
Is this correct, or am I missing something?
BTW: I would also like to detect a double click in the chooser, but it seems hard to find the right place to attach my mouse listener to. Do I really need to dig into the internal structure of the chooser panel to do this? (No, it does not work to detect a second click on the same color, because the change listener only fires if a different color is clicked.)
As you noticed, there is no public api to access the recent colors in the DefaultSwatchChooserPanel, even the panel itself isn't accessible.
As you'll need some logic/bean which holds and resets the recent colors anyway (plus the extended mouse interaction), rolling your own is the way to go. For some guidance, have a look at the implementation of the swatch panel (cough ... c&p what you need and modify what you don't). Basically, something like
// a bean that keeps track of the colors
public static class ColorTracker extends AbstractBean {
private List<Color> colors = new ArrayList<>();
public void addColor(Color color) {
List<Color> old = getColors();
colors.add(0, color);
firePropertyChange("colors", old, getColors());
}
public void setColors(List<Color> colors) {
List<Color> old = getColors();
this.colors = new ArrayList<>(colors);
firePropertyChange("colors", old, getColors());
}
public List<Color> getColors() {
return new ArrayList<>(colors);
}
}
// a custom SwatchChooserPanel which takes and listens to the tracker changes
public class MySwatchChooserPanel ... {
ColorTracker tracker;
public void setColorTracker(....) {
// uninstall old tracker
....
// install new tracker
this.tracker = tracker;
if (tracker != null)
tracker.addPropertyChangeListener(.... );
updateRecentSwatchPanel()
}
/**
* A method updating the recent colors in the swatchPanel
* This is called whenever necessary, specifically after building the panel,
* on changes of the tracker, from the mouseListener
*/
protected void updateRecentSwatchPanel() {
if (recentSwatchPanel == null) return;
recentSwatchPanel.setMostRecentColors(tracker != null ? tracker.getColors() : null);
}
// the mouseListener which updates the tracker and triggers the doubleClickAction
// if available
class MainSwatchListener extends MouseAdapter implements Serializable {
#Override
public void mousePressed(MouseEvent e) {
if (!isEnabled())
return;
if (e.getClickCount() == 2) {
handleDoubleClick(e);
return;
}
Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
setSelectedColor(color);
if (tracker != null) {
tracker.addColor(color);
} else {
recentSwatchPanel.setMostRecentColor(color);
}
}
/**
* #param e
*/
private void handleDoubleClick(MouseEvent e) {
if (action != null) {
action.actionPerformed(null);
}
}
}
}
// client code can install the custom panel on a JFileChooser, passing in a tracker
private JColorChooser createChooser(ColorTracker tracker) {
JColorChooser chooser = new JColorChooser();
List<AbstractColorChooserPanel> choosers =
new ArrayList<>(Arrays.asList(chooser.getChooserPanels()));
choosers.remove(0);
MySwatchChooserPanel swatch = new MySwatchChooserPanel();
swatch.setColorTracker(tracker);
swatch.setAction(doubleClickAction);
choosers.add(0, swatch);
chooser.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0]));
return chooser;
}
As to doubleClick handling: enhance the swatchChooser to take an action and invoke that action from the mouseListener as appropriate.
You can use the JColorChooser.createDialog method - one of the parameters is a JColorChooser. Use a static instance of the JColorChooser and make it the Dialog modal - that way, only one color chooser is displayed at a time.
The createDialog method also takes ActionListeners as parameters for the OK and Cancel button. Thus, don't really have to manage listeners. Of course, this doesn't persist the recent colors across invocations of the app, just persists recent colors in the current app.
Here's a workaround using reflection - it will work provided the underlying implementation doesn't change. Assuming you have a JColorChooser, add your recent colors to it like this:
final JColorChooser chooser = new JColorChooser(Color.white);
for (AbstractColorChooserPanel p : chooser.getChooserPanels()) {
if (p.getClass().getSimpleName().equals("DefaultSwatchChooserPanel")) {
Field recentPanelField = p.getClass().getDeclaredField("recentSwatchPanel");
recentPanelField.setAccessible(true);
Object recentPanel = recentPanelField.get(p);
Method recentColorMethod = recentPanel.getClass().getMethod("setMostRecentColor", Color.class);
recentColorMethod.setAccessible(true);
recentColorMethod.invoke(recentPanel, Color.BLACK);
recentColorMethod.invoke(recentPanel, Color.RED);
//add more colors as desired
break;
}
}

Icon from GWT tree menu item to the right.

Is it possible to align the icon for a tree menu item to the right instead of left?
Like this :
item1 >
item2 >
item3 >
Where the ">" is an image. I am using standard GWT. Tried with both CellTree and normal Tree.
/Andreas
Edit: Just realised this is a Google Web Toolkit question. I know nothing about Google Web Toolkit - but if they let you use normal CSS you can style any list in the way I describe below:
You can't do it with the standard list-style property, but you can definitely do it with background-image:
li {
list-style: none;
background-image: url('arrow.gif');
background-position: right center;
}
You can put any Widgets as treeItem into the Tree. So if you want to put an item inside the tree which has the icon left just create a horizonal panel, put your text and the icon inside and then put the horizontal panel into the tree... (I know this sounds a little bit complex but it is actually really simple and you can do alot of other cool stuf with it)
Tree t = new Tree();
HorizontalPanel hc = new HorizontalPanel();
hc.add(new Label("some text"));
hc.add(new Image("http://tueffel.net/images/icons/icon13.gif"));
TreeItem ti = new TreeItem(hc);
HorizontalPanel hc2 = new HorizontalPanel();
hc2.add(new Label("some text"));
hc2.add(new Image("http://tueffel.net/images/icons/icon13.gif"));
ti.addItem(new TreeItem(hc2));
t.addItem(ti);
RootPanel.get().add(t);
I have a CellTree with my own TreeModel which return a NodeInfo with custom renderer.
something like this:
viewModel = new MyTreeModel( dirs.getRootItems(), filter );
treeDrives = new CellTree( viewModel, null, treeResources );
public class MyTreeModel {
#Override
public <T> NodeInfo<?> getNodeInfo( final T value ) {
return new DefaultNodeInfo<Item>( new ListDataProvider<Item>( dirs ), new DirectoryCell(), selectionModel, null );
}
}
private final class DirectoryCell extends AbstractCell<Item> {
#Override
public void render( com.google.gwt.cell.client.Cell.Context context, Item value, SafeHtmlBuilder sb ) {
// my own html renderer ...
sb.appendHtmlConstant(value.toString);
sb.append"<img src=\"" + value.getImgUrl() + "\" //>");
}
new TreeItem(new HTML("Item Text <img src='path/to/image.png'>"));

Categories