How to show only desired numbers on a slider? - java

I have a slider and it's max value is 60. How to show only 10, 25 and 60 as a label ?
Thanks in Advance!

With the setLabelTable method. Copy-paste from the tutorial:
//Create the slider
JSlider framesPerSecond = new JSlider(JSlider.VERTICAL,
FPS_MIN, FPS_MAX, FPS_INIT);
...
//Create the label table
Hashtable labelTable = new Hashtable();
labelTable.put( new Integer( 0 ), new JLabel("Stop") );
labelTable.put( new Integer( FPS_MAX/10 ), new JLabel("Slow") );
labelTable.put( new Integer( FPS_MAX ), new JLabel("Fast") );
framesPerSecond.setLabelTable( labelTable );
framesPerSecond.setPaintLabels(true);

Related

panel setBackground is messing up colors of JLabels within

Here's my code in problem.
The problem is that if I use "white" to set the Background of panel, the colors of icon in the "pic" JLabel become very light.
If I use "black" instead, the colors of pic JLabel are visible.
It doesn't matter what colors I use in the pic JLabel. They all get lightened as soon as the panel is set to white.
Is there any other way I can set background color of the panel without affecting the colors of the JLabel within?
Color black = new Color( 20, 20, 20, 255 );
Color white = new Color( 255, 255, 255, 255 );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 1200, 500 );
frame.setVisible(true);
frame.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
frame.setLocationRelativeTo( null );
frame.setResizable( false );
JPanel panel= new JPanel();
frame.getContentPane().add( panel );
panel.setLayout( null );
panel.getAccessibleContext().setAccessibleName("panel");
panel.getAccessibleContext().setAccessibleDescription(" ");
// this is the line that causes problem
panel.setBackground( black );
JLabel pic = new JLabel( new ImageIcon( showBaseImage() ) );
panel.add( pic );
pic.setSize( 1200, 500 );
pic.setLocation( 1, 1);
pic.setBackground( black );
public BufferedImage showBaseImage(){
BufferedImage c = new BufferedImage( 1200, 500, BufferedImage.TYPE_INT_ARGB );
Graphics2D gg= c.createGraphics();
gg.setPaint( new Color( 125, 0, 125, 255 ));
gg.fillRect( 0,0, c.getWidth(), c.getHeight() );
gg.setPaint( new Color( 255, 255, 225, 255 ));
imgFont = new Font( "Arial", Font.BOLD, 45 );
gg.setFont( imgFont );
gg.drawString( "Write something", 20, 20 );
gg.dispose();
return c;
}
You may use label.setOpaque(true) to allow your label to be opaque.
Its implementation comes from JComponent which is false by default.
Note that labels are not opaque by default. If you need to paint the label's background, it is recommended that you turn its opacity property to "true". The following code snippet shows how to do this.
label.setOpaque(true);
https://docs.oracle.com/javase/tutorial/uiswing/components/label.html

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.

JavaFx 2.2 editable ComboBox left arrow key

Editable JavaFx ComboBoxes works well but the left-arrow key is interpreted as Shift-TAB.
As you can see the focus is set to the ComboBox and the insertion point is in the middle of its field. Pressing the left arrow key move the focus to the first control, the TextField on the left of the image when pressing right arrow key move the insertion one character right as expected like in any TextField.
How can I catch events to reproduce the behavior of a TextField in an editable ComboBox?
I've tried to catch key events via ComboBox.setOnKeyPressed() and event.consume() but without success.
Here is a minimal program to reproduce this unexpected behavior:
#Override
public void start( Stage stage ) {
stage.setTitle( "Editable ComboBox and left-arrow key" );
ComboBox<String> cmbBx = new ComboBox<>();
cmbBx.getItems().addAll( "A", "B", "C", "D", "E" );
cmbBx.setMinWidth( 150 );
cmbBx.setEditable( true );
cmbBx.setOnKeyPressed( new EventHandler<KeyEvent>(){
#Override public void handle( KeyEvent event ) {
System.err.println( event );
event.consume(); }}); // Consuming left arrow key is inoperant
GridPane grid = new GridPane();
grid.setVgap( 4 );
grid.setHgap( 4 );
grid.setPadding( new Insets( 4, 4, 4, 4 ));
grid.add( new Label( "TextField:" ), 0, 0 );
grid.add( new TextField() , 1, 0 );
grid.add( new Label( "ComboBox:" ) , 2, 0 );
grid.add( cmbBx , 3, 0 );
stage.setScene( new Scene( grid ));
stage.show();
}
The answer is around key bindings like shown in this SO post "key bindings in javafx".
This code catch the LEFT event:
cmbBx.addEventFilter( KeyEvent.ANY, new EventHandler< KeyEvent >() {
#Override public void handle(KeyEvent event ) {
if( event.getCode() == KeyCode.LEFT ) {
event.consume(); }}});
And that's all, but I'm surprised because the left arrow key move the insertion point as expected, only the undesired behavior is removed. Why?

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.

TitledBorder border color and width using UIManager

To change all TitledBorder fonts, I am using UIManager:
UIManager.put("TitledBorder.font", new Font("Tahoma", Font.BOLD, 11));
But what to put to TitledBorder.border property to change only the color of the border (or maybe even it's width)?
Cheers
Just as using UIManager to change all TitledBorder font at once, to change TitledBorder borders use this function:
UIManager.put("TitledBorder.border", new LineBorder(new Color(200,200,200), 1));
It will change (set) the border property to the border object passed in a second parameter.
All border types (even the factory class) description can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html
This sample passes LineBorder object which takes color and width in a constructor just as you asked.
Well, you can always specify any property in TitledBorder itself.
Here is a fully customized example of Swing TitledBorder:
public static void main ( String[] args )
{
LineBorder border = new LineBorder ( Color.RED, 3, true );
TitledBorder tborder = new TitledBorder ( border, "Titled border", TitledBorder.CENTER,
TitledBorder.DEFAULT_POSITION, new Font ( "Arial", Font.BOLD, 14 ), Color.BLUE );
JFrame frame = new JFrame ();
JLabel label = new JLabel ( "Some content label" );
label.setBorder ( BorderFactory
.createCompoundBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ),
BorderFactory.createCompoundBorder ( tborder,
BorderFactory.createEmptyBorder ( 15, 15, 15, 15 ) ) ) );
frame.add ( label );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setVisible ( true );
}

Categories