Java Group layout, vertical layout issues - java

I am trying to make a two column, 3 row layout. Something along the lines of:
----------------------
| Username |Textbox| |
| Email |Textbox| |
----------------------
Yet even when I'm quite sure the groups managed correctly, it still ends up on a single row like so:
I have the vertical groups separated just fine
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel)
.addComponent(emailLabel))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernames)
.addComponent(email))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(usernameLabel)
.addComponent(usernames))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(emailLabel)
.addComponent(email))
);
Any ideas?

You need to set the layout for the container - see mark [1] in the third line below.
To me it looks like you missed that and the container uses FlowLayout.
JFrame frame = new JFrame("GroupLayout Test");
GroupLayout gl_contentPanel = new GroupLayout(frame.getContentPane());
frame.setLayout(gl_contentPanel); // [1]
JLabel usernameLabel = new JLabel("User name");
JLabel emailLabel = new JLabel("Email");
JTextField usernames = new JTextField("usernames");
JTextField email = new JTextField("email");
// your snippet
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel)
.addComponent(emailLabel))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernames)
.addComponent(email))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(usernameLabel)
.addComponent(usernames))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(emailLabel)
.addComponent(email))
);
// end of your snippet
frame.pack();
frame.setVisible(true);

For reference, there's a working example of a two column, three row layout here, illustrated below, that may help guide you.

Related

Create empty table with specific height and redraw on button click in SWT

I want to create an empty SWT table with 2-3 empty rows. Also I want to fill this table with data upon button clicking.
From what I found on the web, it is not possible to create a table with empty rows. If I want empty rows, I would need to add some dummy data which I don't want to (a specific hight of 2-3 rows is good was well). Is there any other way?
Preferably I would have a table with header + 3 rows height which keeps that height and adds scrollbars as necessarily. How can I achieve this?
My layout for the table is set up like this:
Group tableGroup = new Group(parent, SWT.SHADOW_OUT);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginWidth = 5;
gridLayout.marginHeight = 5;
tableGroup.setLayout(gridLayout);
tableGroup.setText("Test");
Composite tableComp = new Composite(tableGroup, SWT.NONE);
TableViewerBuilder tableViewerBuilder = new TableViewerBuilder(tableComp, SWT.BORDER | SWT.NO_SCROLL | SWT.V_SCROLL);
tableViewer = tableViewerBuilder.getTableViewer();
GridData gridData = new GridData();
gridData.heightHint = tableViewer.getTable().getHeaderHeight()
+ (5 * tableViewer.getTable().getItemHeight());
tableGroup.setLayoutData(gridData);
Note: TableViewerBuilder just creates a new TableViewer and sets TableColumnLayout to table.getParent()
The size and location of widgets in SWT is controlled by layout managers. Therefore, you need to instruct the layout manager of the table to give it the desired height.
If the parent of the table uses a GridLayout, you can use hints to influence the size computation of the layout manager.
parent.setLayout( new GridLayout( 1, false ) );
...
Table table = new Table( parent, SWT.NONE );
GridData gridData = new GridData();
gridData.heightHint = table.getHeaderHeight() + ( 3 * table.getItemHeight() );
table.setLayoutData( gridData );
The above example tells the grid layout to reserve a height of 3 times the item height, plus room for the header row, for the table.
Note that the getItemHeight() may change after you add items, in which case you would need to adjust the height hint and re-layout.
The accepted answer did help me much to achieve the result. Yet here I provide the final solution which worked for me:
Group tableGroup = new Group(parent, SWT.SHADOW_OUT);
FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
fillLayout.marginWidth = 5;
fillLayout.marginHeight = 5;
tableGroup.setLayout(fillLayout);
data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
tableGroup.setText("Test");
Composite tableComp = new Composite(tableGroup, SWT.NONE);
TableViewerBuilder tableViewerBuilder = new TableViewerBuilder(tableComp,
SWT.BORDER | SWT.NO_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
tableViewer = tableViewerBuilder.getTableViewer();
tableViewer.setContentProvider(OpenFigiResponseContentProvider.getInstance());
data.heightHint = tableViewer.getTable().getHeaderHeight()
+ (5 * tableViewer.getTable().getItemHeight());
tableGroup.setLayoutData(data);
tableViewer.getTable().setLayoutData(data);
I get a table which completely fill the Group

putting two label beside each other in gwt

Code:
RootPanel footer = RootPanel.get("footer");
footer.addStyleName(StyleNameGen.createName("mainWidgetFooterStyle"));
footer.add(versionLabel);
footer.add(expirationMessage);
and it puts the versionLabel and thenexpirationMessage `below it.
What to do if I want to put the versionLabelbesides the expirationMessage?
You have to add them both to another container first like a HorizontalLayoutContainer or an HBoxLayoutContainer.
HorizontalLayoutContainer labelContainer = new HorizontalLayoutContainer()
labelContainer.add(versionLabel, new HorizontalLayoutData(-1, -1, new Margins(5)));
labelContainer.add(expirationMessage, new HorizontalLayoutData(-1, -1, new Margins(5)));
footer.add(labelContainer);
If I properly understand you need to place 2 label one over another.
For this you can try:
FlowLayoutContainer somePanel = new FlowLayoutContainer();
mixCont.add(somePanel);
Label versionLabel = new Label("12345678");
Label expirationMessage = new Label("9999999");
versionLabel.getElement().getStyle().setFloat(Style.Float.LEFT);
expirationMessage.getElement().getStyle().setPosition(Position.ABSOLUTE);
expirationMessage.getElement().getStyle().setLeft(20, Unit.PX);
somePanel.add(versionLabel);
somePanel.add(expirationMessage);
Actually next code should solve your problem. Second row helps you in positioning your overlapping message:
expirationMessage.getElement().getStyle().setPosition(Position.ABSOLUTE);
expirationMessage.getElement().getStyle().setLeft(20, Unit.PX);

What is the proper way to add custom Controls to the Eclipse toolbar (using WorkbenchWindowControlContribution)?

I'm developing a set of Eclipse plugins, one of which is responsible for adding a toolbar to the Eclipse workspace.
While adding new commands (and the corresponding buttons) can be done in plugin.xml, I also need a text box and a label, which requires the addition of a Control to the plugin.xml, plus an implementation in Java that extends org.eclipse.ui.menus.WorkbenchWindowControlContribution. In practice, this comes down to overriding createControl(Composite parent) in the subclass.
This part is clear to me. The problem is that I'm not sure what type of Control object I should return.
I have tried the following:
Create a ToolBarManager, add an SWT Label and an STW Text to it (both wrapped in separate ControlContribution objects), and return the toolbar obtained by ToolBarManager.createControl(parent):
#Override
protected Control createControl(Composite parent)
{
ToolBarManager manager = new ToolBarManager(SWT.FLAT | SWT.HORIZONTAL);
LabelContributionItem labelItem = new LabelContributionItem("myLabelId");
manager.add(labelItem);
TextContributionItem textItem = new TextContributionItem("myTextId");
manager.add(textItem);
ToolBar toolbar = manager.createControl(parent);
return toolbar;
}
However, the label is not positioned correctly:
Use a GridLayout as the control to return (code adapted from this answer):
#Override
protected Control createControl(Composite parent)
{
Composite composite = new Composite(parent, SWT.SINGLE);
GridLayout compositeLayout = new GridLayout(2, false);
compositeLayout.marginTop = -1;
compositeLayout.marginBottom = 0;
compositeLayout.marginLeft = 5;
compositeLayout.marginWidth = 0;
composite.setLayout(compositeLayout);
Label myLabel = new Label(composite, SWT.BORDER | SWT.SINGLE);
myLabel.setText("myLabel");
Text myText = new Text(composite, SWT.BORDER | SWT.SINGLE);
myText.setText("myText");
return composite;
}
The result is an incorrectly sized and aligned text box, plus a border around the label (rightmost text box added for comparison):
I also tried some other combinations and layouts, but cannot get this to work properly. Furthermore, I'd like to add a ControlDecoration to the text box, like this:
For the ControlDecoration's mouseover text to work properly, there needs to be margin space to the left of the text box (source):
Clients using ControlDecoration should typically ensure that enough margin space is reserved for a decoration
Adding this space has also proved troublesome, except when using the GridLayout's marginLeft parameter (but GridLayout gave the alignment problems described above).
I had the same problem with labels. Use CLabel if you want to add text to the toolbar.
My advice would be to first create a standalone Composite that contains the desired controls and decorations independantly of the workbench contribution.
There you can test the layout in a simple Shell until it looks like how it should.
As far as I understood your question, this is how the controls should be layed out:
Composite composite = new Composite( shell, SWT.NONE );
composite.setLayout( new GridLayout( 2, false ) );
composite.setBackground( composite.getDisplay().getSystemColor( SWT.COLOR_GREEN ) );
Label label = new Label( composite, SWT.NONE );
label.setText( "MyLabel" );
Text text = new Text( composite, SWT.BORDER );
text.setText( "my text" );
ControlDecoration decoration = new ControlDecoration( text, SWT.TOP | SWT.LEFT );
FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
FieldDecoration fieldDecoration = registry.getFieldDecoration( DEC_CONTENT_PROPOSAL );
decoration.setImage( fieldDecoration.getImage() );
label.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
GridData gridData = new GridData( SWT.BEGINNING, SWT.CENTER, false, false );
gridData.horizontalIndent = 10;
text.setLayoutData( gridData );
Then you can attempt to contribute the controls to the workbench. The extension should be specified as described in this post: Contributed control to the status bar not visible
createControl() should return the above mentioned composite and look like this
protected Control createControl( Composite parent ) {
Composite composite = new Composite( shell, SWT.NONE );
// ... create label, text, decoration as above
return composite;
}
You need to create a tool item of type SWT.SEPARATOR and then attach the label to that. The following code demonstrates this:
public static ToolItem createToolBarLabel(ToolBar toolBar, String text, int width) {
ToolItem labelItem = new ToolItem(toolBar, SWT.SEPARATOR);
CLabel label = new CLabel(toolBar, SWT.NONE);
label.setText(text);
labelItem.setWidth(width);
labelItem.setControl(label);
return labelItem;
}

Scrollbar is not showing in ScrolledForm

I am using ScrolledForm and using its property setAlwaysShowScrollBars(true) but still I am not getting scroll bar always.
Please count the reason: I want scroll bar to show always. I have used its reFlow(true) also by thinking problem in layout but it still not work.
formToolkit = new FormToolkit(parent.getDisplay());
form = formToolkit.createScrolledForm(parent);
form.setText("scrolled from");
form.setAlwaysShowScrollBars(true);
form.setFont(fontTahoma8Normal);
GridLayout layout = new GridLayout();
form.getBody().setLayout(layout);
GridData data = new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_BEGINNING
| GridData.HORIZONTAL_ALIGN_BEGINNING);
form.getBody().setLayoutData(data);
return form;

How do I easily edit the style of the selected text in a JTextPane?

How do I easily edit the style of the selected text in a JTextPane? There doesn't seem to be many resources on this. Even if you can direct me to a good resource on this, I'd greatly appreciate it.
Also, how do I get the current style of the selected text? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart()); but it doesn't seem to be working.
Here's a code snippet to insert a formatted "Hello World!" string in a JEditorPane:
Document doc = yourEditorPane.getDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);
Font font = new Font("Arial", Font.BOLD, 18);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "Hello World!", style);
Take a look at the following code in this pastebin:
http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3
The example is from here:
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm
It looks like you can change the style using the following in an action listener:
final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);
doc.setCharacterAttributes(0, 10, boldStyle, true);
It sets the style of the text between the given offset and length to a specific style.
See the full pastebin for more details. That should fix your problem though.
The easiest way to manipulate text panels is using editor kits and their associated actions. You can find a demo of this in the JDK samples (under jdk\demo\jfc\Stylepad).
Sample code that installs a StyledEditorKit and uses a FontSizeAction to manipulate the text:
public static void main(String[] args) {
// create a rich text pane
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// install the editor kit
StyledEditorKit editorKit = new StyledEditorKit();
textPane.setEditorKit(editorKit);
// build the menu
JMenu fontMenu = new JMenu("Font Size");
for (int i = 48; i >= 8; i -= 10) {
JMenuItem menuItem = new JMenuItem("" + i);
// add an action
menuItem
.addActionListener(new StyledEditorKit.FontSizeAction(
"myaction-" + i, i));
fontMenu.add(menuItem);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(fontMenu);
// show in a frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setJMenuBar(menuBar);
frame.setContentPane(scrollPane);
frame.setVisible(true);
}
(Tip: if you want to use a FontFamilyAction, have a look at GraphicsEnvironment.getAvailableFontFamilyNames() and logical font family names.)
I'd recommend taking a look at Sun's Java Tutorial about editor panes.
Ok, wow. Hard question. So I have not found a way to get the style of a given character. You can, however, get the MutableAttributeSet for a given character and then test to see if the style is in that attribute set.
Style s; //your style
Element run = styledDocument.getCharacterElement(
textPane.getSelectionStart() );
MutableAttributeSet curAttr =
( MutableAttributeSet )run.getAttributes();
boolean containsIt = curAttr.containsAttributes( s );
One problem with getting the Style for a range of characters is that there may be more than one style applied to that range (example: you may select text where some is bold and some is not).
To update the selected text you can:
Style s; //your style
JTextPane textPane; //your textpane
textPane.setCharacterAttributes( s, false );
Oh, and it appears that the function getLogicalStyle doesn't work because it's returning the default style (or maybe just the style) for the paragraph that contains p, rather than the the style of the character at p.

Categories

Resources