Center text vertically inside label - java

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();
}

Related

SWT Application: Are Draggable Tabs Possible?

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...

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

Scrolling & Selecting of JList in undecorated frame doesn't work properly

I have this strange behavior, look at the following code (or try it out yourself):
public class JListProblem
{
public static void main (String[] args)
{
JFrame frame = new JFrame("JList Problem");
frame.setSize( 300, 500);
JScrollPane sp = new JScrollPane();
DefaultListModel dlm = new DefaultListModel();
for ( int i = 0; i < 10000; i++ )
{
dlm.addElement( i);
}
JList list = new JList(dlm );
sp.setViewportView( list );
frame.add( sp );
frame.setUndecorated( true );
frame.setBackground( new Color( 0.0f, 0.0f, 0.0f, 0.0f ) );
frame.setVisible( true );
}
}
Here's my problem:
When you try to scroll, it does not scroll "smoothly" (sorry, I don't know the correct word for this).
Try selecting an entry after scrolling: After you clicked, another entry is selected.
How can I correct this behavior?
When you decrease the amount of entries (change the value of maximum i to 1000 for example), everything is working fine.

What layout on Java I need?

I have a JPanel and for example, if I click on the button "INSERT", I can add a JButton and a JLabel. My problem is I need to insert the JLabel under the JButton. The JLabel text must centred respect the JButton text. After that, I want a space around 10 pixels to use again my "INSERT" button and add horizontally a new pair on JButton and JLabel with the same orientation.
Thanks!
PD: Please, complement your question with an attempt.
Here is a quick example that shows a dynamic (which is what I assume you wanted) setup to allow insertion of an undefined number of panels:
public class AwesomeAnswer {
public static void main(String[] args) {
// please not that this is only an example and not a
// Swing thread safe way of starting a JFrame
JFrame frame = new JFrame();
JPanel content = (JPanel)frame.getContentPane();
// create our top panel that will hold all of the inserted panels
JPanel page = new JPanel();
page.setLayout( new BoxLayout( page, BoxLayout.Y_AXIS ) );
// add our page to the frame content pane
content.add( page );
// add two button/label panels
page.add( insert( "This is an awesome answer", "Accept" ) );
page.add( insert( "Say thank you", "Thank" ) );
frame.pack();
frame.setVisible( true );
}
public static final JPanel insert( String labelText, String buttonText ) {
// create the label and the button
JLabel lbl = new JLabel( labelText );
JButton btn = new JButton( buttonText );
// create the panel that will hold the label and the button
JPanel wrapPanel = new JPanel( new GridBagLayout() );
wrapPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// tell the grid bag how to behave
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 0;
gbc.gridheight = 2;
// make the button centered
JPanel buttonPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
buttonPanel.add( btn );
// make the label centered
JPanel labelPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
labelPanel.add( lbl );
// add our button and label to the grid bag with our constraints
wrapPanel.add( buttonPanel, gbc );
wrapPanel.add( labelPanel, gbc );
return wrapPanel;
}
}
I think that you have something like that
rootPane
+-----panelButton
| +------JButton
|
+-----panelPanels
+-----panel
+---JButton
+---JLabel
The SpringLayout can help you
SpringUtilities.makeGrid(panel,
2, 1, //rows, cols
0, 0, //initialX, initialY
5, 5);//xPad, yPad

Setting number of rows to be displayed for Multi line text in swt

I am using following for TextArea
ToolBar bar = new ToolBar(box,SWT.NONE);
ToolItem item = new ToolItem(bar, SWT.SEPARATOR);
Text text = new Text(bar, SWT.BORDER | SWT.MULTI);
item.setWidth(width);
item.setControl(text);
GridData data = new GridData();
data.verticalAlignment = SWT.CENTER;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
text.setLayoutData(data);
I want to display a multi line text box, currently its accepting multi line text but showing only a single line at a time.
Any idea how to set the number of rows to be displayed ?
Thanks.
You can also use Text.getLineHeight() to determine the height of a single line of text, you don't need a GC for that:
final Text text = new Text(container, SWT.BORDER | SWT.WRAP);
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
gridData.heightHint = 5 * text.getLineHeight();
text.setLayoutData(gridData);
You can set the height in pixels:
/* Set the height to 75 pixels */
data.heightHint = 75;
However, you can also set the height in terms of the number of character rows, but you have to do some trickery to measure the character height. You'll need to build a graphics context (GC) to measure the text extent.
For example:
GC gc = new GC(text);
try
{
gc.setFont(text.getFont());
FontMetrics fm = gc.getFontMetrics();
/* Set the height to 5 rows of characters */
data.heightHint = 5 * fm.getHeight();
}
finally
{
gc.dispose();
}

Categories

Resources