SWT Application: Are Draggable Tabs Possible? - java

I am building a Java application using SWT. One of the requirements of the application is that it have multiple windows. Rather than having "forever independent" windows, I thought it would be cool to implement a feature like in most browsers where you have a single, tabular window, where each tab can be dragged out to create a separate window. After a little research using the Google, it seems possible to accomplish this using JavaFX, but is it possible (and relatively easy) to achieve the same functionality in SWT? Thanks in advance.

While my take might be a little late, here it is nonetheless. Below snippet is a rough POC that allows to drag an item from the CTabFolder and when the item is dropped outside of the bounds of the folder, a shell is opened to show items' content.
public static void main( String[] args ) {
Display display = new Display();
final Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
final CTabFolder folder = new CTabFolder( shell, SWT.BORDER );
for( int i = 0; i < 4; i++ ) {
CTabItem item = new CTabItem( folder, SWT.CLOSE );
item.setText( "Item " + i );
Text text = new Text( folder, SWT.MULTI );
text.setText( "Content for Item " + i );
item.setControl( text );
}
Listener dragListener = new Listener() {
private CTabItem dragItem;
public void handleEvent( Event event ) {
Point mouseLocation = new Point( event.x, event.y );
switch( event.type ) {
case SWT.DragDetect: {
CTabItem item = folder.getItem( mouseLocation );
if( dragItem == null && item != null ) {
dragItem = item;
folder.setCapture( true );
}
break;
}
case SWT.MouseUp: {
if( dragItem != null && !folder.getBounds().contains( mouseLocation ) ) {
popOut( dragItem, folder.toDisplay( mouseLocation ) );
dragItem.dispose();
dragItem = null;
}
break;
}
}
}
};
folder.addListener( SWT.DragDetect, dragListener );
folder.addListener( SWT.MouseUp, dragListener );
shell.pack();
shell.open();
while( !shell.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
private static void popOut( CTabItem tabItem, Point location ) {
Control control = tabItem.getControl();
tabItem.setControl( null );
Shell itemShell = new Shell( tabItem.getParent().getShell(), SWT.DIALOG_TRIM | SWT.RESIZE );
itemShell.setLayout( new FillLayout() );
control.setParent( itemShell );
control.setVisible( true ); // control is hidden by tabItem.setControl( null ), make visible again
itemShell.pack();
itemShell.setLocation( location );
itemShell.open();
}
While this example uses a CTabFolder it should also be possible to use the (native) TabFolder instead.
What is certainly missing is visual feedback while dragging an item and a means to abort a drag operation (e.g. Esc key), and probably some more things...

Related

How to set text on a particular frame on a video in android java?

I'm making a video editing app where I am trying to add text on a video to a particular frame. I tried to solve this by taking the exact frame and making a bitmap and adding text on it through canvas, but it didn't work.
Here is my code:
private void extractVideoFrame ( String path , Context context , int time ) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever ( );
try {
retriever.setDataSource ( path );
} catch ( Exception e ) {
System.out.println ( "Exception= " + e );
}
String duration = retriever.extractMetadata ( MediaMetadataRetriever.METADATA_KEY_DURATION );
videoFramesBitmapList = retriever.getFrameAtTime ( time );
pickFrame.setImageBitmap ( videoFramesBitmapList );
Toast.makeText ( context , "current" + time , Toast.LENGTH_SHORT ).show ( ); FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
}
All I want to do is to add text on a frame from a video, I also don't wanna use ffmpeg, since I have do to through java.

Using JTree to list Objects

Currently I am trying to learn Swing. I want to make a JTree with my ArrayList which has Object instances in it. Every object has a name and I want to able to change that name with right click and then an appearing change button. But I don't really understand the Mouselistener. I Googled and got a mouse listener but I don't know how to add the setName method.
Lager is my example Object. 400 is the capacity.
private DefaultMutableTreeNode createTree() {
Lager s = new Lager("Name",400);
DefaultMutableTreeNode root = new DefaultMutableTreeNode(s);
DefaultMutableTreeNode vegetableNode = new DefaultMutableTreeNode("Vegetables");
DefaultMutableTreeNode fruitNode = new DefaultMutableTreeNode("Fruits");
// add the child nodes to the root node
root.add(vegetableNode);
root.add(fruitNode);
return root;
}
Here is the mouse listener I copied.
DefaultMutableTreeNode root = createTree();
JTree tree = new JTree(root);
tree.addMouseListener ( new MouseAdapter ()
{
public void mousePressed ( MouseEvent e )
{
if ( SwingUtilities.isRightMouseButton ( e ) )
{
TreePath path = tree.getPathForLocation ( e.getX (), e.getY () );
Rectangle pathBounds = tree.getUI ().getPathBounds ( tree, path );
if ( pathBounds != null && pathBounds.contains ( e.getX (), e.getY () ) )
{
JPopupMenu menu = new JPopupMenu ();
menu.add ( new JMenuItem ( "Test" ) );
System.out.println(tree.getLastSelectedPathComponent());
menu.show ( tree, pathBounds.x, pathBounds.y + pathBounds.height ); // pathBounds.y/x sind die Positionen des Lagers auf den ich Rechtsklicke
}
}
}
} );
this.add(tree);

Center text vertically inside label

Is it possible to center the text inside a label vertically? I'm using:
message1 = new Label(shell, SWT.VERTICAL | SWT.CENTER);
This centers the text horizontally, but not vertically.
CLabel label = new CLabel(shell, SWT.CENTER);
Maybe you should try using org.eclipse.swt.custom.CLabel (JavaDoc).
The code above exactly centers the text in the label vertically.
The text within a label is always aligned at the top. The VERTICAL style only applies when SEPARATOR is set. In this case it displays a single vertical or horizontal line and the text is ignored.
But you can center the label itself within the parent. For example, this snippet centers a label within the containing shell by using a GridLayout:
public static void main( String[] args ) {
Display display = new Display();
Shell parent = new Shell( display );
Label label = new Label( parent, SWT.NONE );
label.setText( "some text" );
label.setBackground( display.getSystemColor( SWT.COLOR_GREEN ) );
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true ) );
parent.open();
while( !parent.isDisposed() ) {
if( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}

Display selected Item in JComboBox when item is clicked

I currently have 2 JComboBoxes. When an item in the first JComboBox is selected, relevant items are displayed in JComboBox2. However when I try to select the items in the second JComboBox it keeps returning to default. However it is registering the value of the item that is selected, just not displaying the selected item in the JComboBox.
Here is a snippet of my code:
mainComboBox = new JComboBox( treeItems);
mainComboBox.addActionListener( this );
getContentPane().add( mainComboBox, BorderLayout.WEST );
// Create sub combo box with multiple models
subComboBox = new JComboBox();
subComboBox.addActionListener(this);
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
getContentPane().add( subComboBox, BorderLayout.EAST );
String [] chromalveolataItems = chromalveolataTreeSet.toArray(new String[chromalveolataTreeSet.size()]);
subItems.put(treeItems[1], chromalveolataItems);
String [] mycetozoaItems = mycetozoaTreeSet.toArray(new String[mycetozoaTreeSet.size()]);
subItems.put(treeItems[2], mycetozoaItems);
String [] metazoaItems = metazoaTreeSet.toArray(new String[metazoaTreeSet.size()]);
subItems.put(treeItems[3], metazoaItems);
String [] viridiplantaeItems = viridiplantaeTreeSet.toArray(new String[viridiplantaeTreeSet.size()]);
subItems.put(treeItems[4], viridiplantaeItems);
String [] virusesItems = virusesTreeSet.toArray(new String[virusesTreeSet.size()]);
subItems.put(treeItems[5], virusesItems);
.
#Override
public void actionPerformed(ActionEvent e)
{
String item = (String)mainComboBox.getSelectedItem();
Object o = subItems.get( item );
String organismComboItem = (String)subComboBox.getSelectedItem();
if(treeArray.contains(organismComboItem)){
//System.out.println(treeArray.indexOf(organismComboItem));
String selectedId = idArray.get(treeArray.indexOf(organismComboItem));
System.out.println(selectedId);
}
if (o == null)
{
subComboBox.setModel( new DefaultComboBoxModel() );
}
else
{
subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
}
}
There is probably a relatively simple solution to this however I am new to Java so please forgive me.
Any help whatsoever would be much appreciated! :)
Looks to me like you based your code on the example found in this posting: Binding comboboxes in swing (or one like it).
When you use examples don't add extra code.
subComboBox.addActionListener(this);
The example does not do this. By adding the same listener to the subComboBox you are telling it to reset itself every time you select an item from it. Only the mainComboBox needs the ActionListener.

How to make unequal GridLayout rows

I am wanting to make each cell in a row a different length..
Here is a picture to help.
So the Policy cell and the text to the right is fine. However, Section 1 and Section 2 I want to be 50/50.. Not 20/80 or whatever it is now. I have started using the WindowsBuilder tool instead of doing this by hand. Is this possible to do?
To lay out controls in the requested manner with a GridLayout in SWT you will have to group the controls of each row into a composite of their own like so:
shell.setLayout( new RowLayout( SWT.VERTICAL ) );
Composite composite1 = new Composite( shell, SWT.NONE );
composite1.setLayout( new GridLayout( 2, false ) );
createLabel( composite1, "2020" );
createLabel( composite1, "808080808080" );
Composite composite2 = new Composite( shell, SWT.NONE );
composite2.setLayout( new GridLayout( 2, false ) );
createLabel( composite2, "50505050" );
createLabel( composite2, "50505050" );
private static Label createLabel( Composite parent, String text ) {
Label label = new Label( parent, SWT.NONE );
label.setText( text );
return label;
}
However, to me a FormLayout seems more suitable to solve the given problem:
shell.setLayout( new FormLayout() );
FormData leftFormData = new FormData();
leftFormData.top = new FormAttachment( 0 );
leftFormData.left = new FormAttachment( 0 );
leftFormData.right = new FormAttachment( 20 );
Label leftLabel = createLabel( shell, "2020", leftFormData );
FormData rightFormData = new FormData();
rightFormData.top = new FormAttachment( 0 );
rightFormData.left = new FormAttachment( leftLabel );
rightFormData.right = new FormAttachment( 100 );
createLabel( shell, "808080808080", rightFormData );
private static Label createLabel( Composite parent, String text, Object layoutData ) {
Label label = new Label( parent, SWT.NONE );
label.setText( text );
label.setLayoutData( layoutData );
return label;
}
If you find the formData and formAttachment code too verbose, you may have a look at Slim Down SWT FormLayout Usage
And for an in-depth discussion of SWT layouts I recommend the Understanding Layouts in SWT article.
EDIT: Missed that you are using SWT GridLayout instead of the Swing one. My solution works with Swing so keep that in mind.
You may want to use the GridBagLayout. It's more complicated than the GridLayout but offers greater flexibility
See some documentation on how to use it here

Categories

Resources