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.
Related
Hi everyone reading this.
I am currently developing a program, that should run well displayed in different resolutions and thus has a scaling factor.
There is ne component, that I cannot find a solution for scaling for:
The JXDatePicker and its JXMonthView.
What I tried:
final JXDatePicker datePicker = new JXDatePicker(new Date());
final JXMonthView monthView = datePicker.getMonthView();
monthView.setBounds(hauptFrame.getSize().width / 2 - (100*ViewerDefaults.SZ_INCR), (165*ViewerDefaults.SZ_INCR), (200*ViewerDefaults.SZ_INCR), (200*ViewerDefaults.SZ_INCR));
monthView.setSize(monthView.getWidth() * ViewerDefaults.SZ_INCR, monthView.getHeight() * ViewerDefaults.SZ_INCR);
monthView.setPreferredColumnCount(1);
monthView.setPreferredRowCount(1);
monthView.setTraversable(true);
monthView.setZoomable(true);
datePicker.setSize(datePicker.getWidth() * 2, datePicker.getHeight() * 2);
ViewerDefaults.SZ_INCR is the scaling factor and would replace the 2 in the last line, if working.
I added the setPreferredColumnCount(1) and setPreferredRowCount(1) as I tried setting the size of the JXMonthview before, but I ended up having 4 months displayed next to each other, whereas I just wanted a single, larger displayed month.
Can you help and show me a way to enlarge the display of only on month?
EDIT:
I translated everything into using Layout Managers. The problematic behaviour remains: The month displayed is not scaled up, but simply displayed in the center. To give some more insight to the code I put the whole method here as follows(size is set earlier by Toolkit.getDefaultToolkit().getScreenSize();):
private static JPanel licBorrowPanel = null;
licBorrowPanel = new JPanel();
licBorrowPanel.setBounds(0, 20, size.width, size.height-20);
licBorrowPanel.setVisible(false);
licBorrowPanel.setBackground(Color.WHITE);
public static void licenceBorrowDialog() {
licBorrowPanel.setSize(new Dimension(hauptFrame.getSize().width, hauptFrame.getSize().height));
licBorrowPanel.setBackground(Color.WHITE);
licBorrowPanel.setLayout(new BorderLayout());
licBorrowPanel.setVisible(true);
//Create upper part of licBorrowPanel
JPanel licBorrowTop = new JPanel();
licBorrowTop.setLayout(new BoxLayout(licBorrowTop, BoxLayout.Y_AXIS));
// Label Titel
JLabel labelTitel = new JLabel("License borrwing");
labelTitel.setHorizontalAlignment(JLabel.CENTER);
labelTitel.setFont(ViewerDefaults.DEF_LABEL_FONT);
licBorrowTop.add(labelTitel);
// Header Calendar
JLabel calendarTitle = new JLabel("Please select a date", SwingConstants.LEFT);
calendarTitle.setHorizontalAlignment(JLabel.CENTER);
calendarTitle.setFont(ViewerDefaults.DEF_ROW_FONT);
licBorrowTop.add(calendarTitle);
licBorrowPanel.add(licBorrowTop, BorderLayout.PAGE_START);
//Create central part of licBorrowPanel
JPanel licBorrowCenter = new JPanel();
licBorrowCenter.setLayout(new BoxLayout(licBorrowCenter, BoxLayout.Y_AXIS));
// Label selectedDate
final JLabel labelSelectedDate = new JLabel("<HTML><BODY> </HTML></BODY>");
labelSelectedDate.setHorizontalAlignment(JLabel.CENTER);
labelSelectedDate.setFont(ViewerDefaults.DEF_LIC_FONT);
labelSelectedDate.setText("<HTML><BODY><b>Some Text</HTML></BODY>");
// Calendar
final JXDatePicker datePicker = new JXDatePicker(new Date());
final JXMonthView monthView = datePicker.getMonthView();
monthView.setPreferredColumnCount(1);
monthView.setPreferredRowCount(1);
monthView.setZoomable(true); //Removing this results in several Columns and Rows being displayed.
// Set selection to "in two days" as default
Date newDate = monthView.getSelectionDate();
newDate.setDate(newDate.getDate()+2);
datePicker.addActionListener(new ActionListener() {
//...
});
licBorrowCenter.add(monthView, BorderLayout.CENTER);
licBorrowCenter.add(labelSelectedDate, BorderLayout.CENTER);
// Footer Calendar
JLabel calendarSubTitle = new JLabel("<HTML><BODY><CENTER>Some other Text.</CENTER></HTML></BODY>", SwingConstants.LEFT);
calendarSubTitle.setFont(ViewerDefaults.DEF_FONT);
calendarSubTitle.setHorizontalAlignment(JLabel.CENTER);
calendarSubTitle.setFont(ViewerDefaults.DEF_ROW_FONT);
calendarSubTitle.setForeground(Color.RED);
licBorrowCenter.add(calendarSubTitle, BorderLayout.CENTER);
licBorrowPanel.add(licBorrowCenter);
//Create lower part of licBorrowPanel
JPanel licBorrowBottom = new JPanel();
licBorrowBottom.setLayout(new BorderLayout());
// Button OK
JButton buttonLicBorrowOK = new JButton("OK");
buttonLicBorrowOK.setFont(ViewerDefaults.DEF_LIC_FONT);
licBorrowBottom.add(buttonLicBorrowOK, BorderLayout.LINE_START);
// Button Return
JButton buttonLicReturn = new JButton("Return");
buttonLicReturn.setFont(ViewerDefaults.DEF_LIC_FONT);
licBorrowBottom.add(buttonLicReturn, BorderLayout.CENTER);
// Button Cancel
JButton buttonLicBorrowCancel = new JButton("Cancel");
buttonLicBorrowCancel.setFont(ViewerDefaults.DEF_LIC_FONT);
licBorrowBottom.add(buttonLicBorrowCancel, BorderLayout.LINE_END);
licBorrowPanel.add(licBorrowBottom, BorderLayout.PAGE_END);
// Update licBorrowPanel
licBorrowPanel.update(licBorrowPanel.getGraphics());
// several action listeners for buttons eluded..
}
"hauptframe" is the parent JFrame.
An additional problem occurred, but I think this will be solved once the JXMonthView is displayed as wanted: The licBorrowBottom is not displayed.
I hope this clarifies the problem and helps helping :)
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
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.
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);
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 );
}