Setting the component using setTabComponent - java

if I set a JButton as the tabComponent using
tabbedPane.setTabComponent(new JButton("test"));
The button covers a part of the tab while I can still see background of the tab.
But my requirement is to set this Button component on the tab completely as if the button should cover the whole tab as if there should be no access to that tab.

You might try using this method:
UIManager.put ( "TabbedPane.tabInsets", new Insets ( 0, 0, 0, 0 ) );
to remove tab insets and this one:
UIManager.put ( "TabbedPane.selectedTabPadInsets", new Insets ( 0, 0, 0, 0 ) );
to remove selected tab additional insets.
There is also a few other options that are usually taken from UI defaults:
highlight = UIManager.getColor("TabbedPane.light");
lightHighlight = UIManager.getColor("TabbedPane.highlight");
shadow = UIManager.getColor("TabbedPane.shadow");
darkShadow = UIManager.getColor("TabbedPane.darkShadow");
focus = UIManager.getColor("TabbedPane.focus");
selectedColor = UIManager.getColor("TabbedPane.selected");
textIconGap = UIManager.getInt("TabbedPane.textIconGap");
tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
selectedTabPadInsets = UIManager.getInsets("TabbedPane.selectedTabPadInsets");
tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets");
tabsOverlapBorder = UIManager.getBoolean("TabbedPane.tabsOverlapBorder");
contentBorderInsets = UIManager.getInsets("TabbedPane.contentBorderInsets");
tabRunOverlay = UIManager.getInt("TabbedPane.tabRunOverlay");
tabsOpaque = UIManager.getBoolean("TabbedPane.tabsOpaque");
contentOpaque = UIManager.getBoolean("TabbedPane.contentOpaque");
opaque = UIManager.get("TabbedPane.opaque");
You might want to modify some of them aswell to achieve what you need...

Related

obtain Windows 10 Display settings values in Java

When user change Display settings scaling (Windows 10, right click on desktop, select Display settings and scale to 150%), suddenly all values reported by
GraphicsDevice device = MouseInfo.getPointerInfo().getDevice();
Rectangle bounds = device.getDefaultConfiguration().getBounds();
or
Toolkit.getDefaultToolkit().getScreenSize();
or
GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
become invalid. Is there a way how to get the actual values ?
Found solution that seems to respect the Display settings.
javafx.geometry.Rectangle2D r = new javafx.geometry.Rectangle2D(0, 0, 1, 1);
ObservableList<Screen> screens = Screen.getScreensForRectangle(r);
if (screens != null && screens.size() > 0) {
javafx.geometry.Rectangle2D rc = screens.get(0).getBounds();
}

java awt color chooser does't show in proper way in JavaFx

#FXML
Private void handleItemBackAction(ActionEvent eve)
{
java.awt.Color color=JColorChooser.showDialog(null,"Select a color",java.awt.Color.CYAN);
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
hex="#"+hex;
Text.setText(hex);
ShortcutButton.setStyle("-fx-background-color: " + hex + ";");
}
When I run this window and click on button at first time color chooser goes behind my actual pane.
When I click on button second time while running it shows at top of all other pane which is correct and so on it works properly.
Then why color chooser not shows in front first time on button click?
The first argument to JColorChooser.showDialog is the parent component of the dialog. You told that method to show the dialog with no parent, so it doesn't know about your other windows.
Instead of using JColorChooser.showDialog, you'll need to embed a JColorChooser instance inside a JavaFX dialog or window:
JColorChooser colorChooser = new JColorChooser(java.awt.Color.CYAN);
SwingNode colorChooserNode = new SwingNode();
colorChooserNode.setContent(colorChooser);
Alert dialog = new Alert(Alert.AlertType.NONE);
// Guarantees dialog will be above (and will block input to) mainStage.
dialog.initOwner(mainStage);
dialog.setTitle("Select a color");
dialog.getDialogPane().setContent(colorChooserNode);
dialog.getDialogPane().getButtonTypes().setAll(
ButtonType.OK, ButtonType.CANCEL);
Optional<ButtonType> response = dialog.showAndWait();
if (response.filter(r -> r == ButtonType.OK).isPresent()) {
int rgb = colorChooser.getColor().getRGB();
String hex = String.format("#%06x", rgb & 0xffffff);
Text.setText(hex);
ShortcutButton.setBackground(new Background(
new BackgroundFill(Color.valueOf(hex), null, null)));
} else {
System.out.println("User canceled");
}
Of course, you're probably better off using ColorPicker in your main window, so you don't have to create an explicit dialog at all:
final ColorPicker colorPicker = new ColorPicker(Color.CYAN);
colorPicker.setOnAction(e -> {
Color color = colorPicker.getValue();
String hex = String.format("#%02x02x02x",
(int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255));
Text.setText(hex);
ShortcutButton.setBackground(
new Background(new BackgroundFill(color, null, null)));
});
myLayoutPane.getChildren().add(colorPicker);
As an aside, Java variable names should always start with a lowercase letter, to make them easy to distinguish from class names. Consider changing Text to text, and ShortcutButton to shortcutButton.

swt add widgets towards right in formLayout

Here is my code:
Composite outer = new Composite(parent, SWT.BORDER);
outer.setBackground(new Color(null, 207, 255, 206)); // Green
FormLayout formLayout = new FormLayout();
formLayout.marginHeight = 5;
formLayout.marginWidth = 5;
formLayout.spacing = 5;
outer.setLayout(formLayout);
//TOP
Composite Top = new Composite(outer, SWT.BORDER);
Top.setLayout(new GridLayout());
Top.setBackground(new Color(null, 232, 223, 255)); // Blue
FormData fData = new FormData();
fData.top = new FormAttachment(0);
fData.left = new FormAttachment(0);
fData.right = new FormAttachment(100); // Locks on 10% of the view
fData.bottom = new FormAttachment(20);
Top.setLayoutData(fData);
//BOTTOM
Composite Bottom = new Composite(outer, SWT.BORDER);
Bottom.setLayout(fillLayout);
Bottom.setBackground(new Color(null, 255, 235, 223)); // Orange
fData = new FormData();
fData.top = new FormAttachment(20);
fData.left = new FormAttachment(0);
fData.right = new FormAttachment(100);
fData.bottom = new FormAttachment(100);
Bottom.setLayoutData(fData);
I just wanted to add widgets for example label images to the right of the "TOP" composite layout. Since i am new to swt, am facing difficulty to align all the label to right of it. How could i achieve this ?
If want to place another width to the right of top you first need to advise top to not occupy 100% of the available space, for example only half of the space:
FormData formData = new FormData();
formData.right = new FormAttachment( 50 );
Or you can leave formData.right unspecified (i.e. null) so that the widget will use its preferred width.
Once there is room for another widget, you can right-attach one like so:
Composite right = new Composite( outer, SWT.BORDER );
right.setBackground( display.getSystemColor( SWT.COLOR_YELLOW ) );
FormData rightFormData = new FormData();
rightFormData.top = new FormAttachment( top, 0, SWT.TOP );
rightFormData.left = new FormAttachment( top );
rightFormData.bottom = new FormAttachment( top, 0, SWT.BOTTOM );
right.setLayoutData( rightFormData );
The result will look like this:
To learn more about FormLayout and other layouts in SWT I recommend the
Understanding Layouts in SWT article. Though the article may seem outdated, Layouts in SWT haven't changed since then thus the contents of the article are still valid.
Once you are fluent with the FormLayout and look for a less verbose way to specify the positioning you might want to try this FormLayout helper.

How I can add size to text in SWT

I create a label with default text of 'data preview'.
When I tried to change the text ( dnyamic ) to diffrerent text it show me the text only in the size of 'data preview'.
How i can control on the size of the label ?
final Composite previewHeader = new Composite(this.previewPanel, SWT.NULL);
final GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;
previewHeader.setLayout(layout);
final GridData headerData = new GridData();
headerData.horizontalAlignment = GridData.FILL;
headerData.grabExcessHorizontalSpace = true;
previewHeader.setLayoutData(headerData);
this.previewLabel = new RichTextLabel(previewHeader, SWT.LEFT);
this.previewLabel.setText("<b>Data Preview:</b>"); //$NON-NLS-1$
final GridData labeldata = new GridData();
labeldata.grabExcessHorizontalSpace = true;
labeldata.verticalAlignment = GridData.VERTICAL_ALIGN_END;
labeldata.verticalSpan = 0;
labeldata.verticalIndent = 0;
this.previewLabel.setLayoutData(labeldata);
You need to call layout() on the parent of the text. Here is the javadoc of Composite#layout():
If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children. If the receiver does not have a layout, do nothing.
This solves the problem because of the following reason:
The size of each child is determined every time the parent has to lay out. When you first add your label, the parent will initiate the size computation for the child. When you later on change the text, this is not done automatically, so you have to tell the parent to lay out again.

GXT - Truncated text in a Window

I've a Window which contains two ContentPanel (horizontally), one with a Image into it and the other one with some text. The problem is that my text is truncated : it's going out of the Window...
Window win = new Window();
win.setLayout( new FillLayout() );
win.setMinWidth( 250 );
win.setHeight( 120 );
ContentPanel content = new ContentPanel( new RowLayout( Orientation.HORIZONTAL ) );
ContentPanel iconePanel = new ContentPanel( new FillLayout() );
iconePanel.add(myImage);
content.add( iconePanel, new RowData( 48, 1 ) );
Text textPanel = new Text();
textPanel.setText( msg );
content.add( textPanel, new RowData( -1, 1 ) );
win.add( content );
win.show();
How can I keep the text in the panel?
Its necessary that you should have a good understanding of different panels in the GXT,There is another trick by adjusting the padding size,Its better you use a firebug and adjust the Layout using Firebug,it will help a lot for adjusting the window.

Categories