Scrolling text in Dialog not working - java

I am creating a Dialog with the following code:
Dialog dlg = new Dialog();
dlg.setUIID("AboutDialog");
dlg.setTitle("About");
dlg.setScrollableY(true);
dlg.setScrollVisible(true);
dlg.setLayout(new BorderLayout());
SpanLabel spl = new SpanLabel(DialogText.aboutTxt[txtItem]);
dlg.addComponent(BorderLayout.CENTER, spl);
height = screenHorizontalSize / 9;
width = screenVerticalSize / 10;
Button close = new Button("Close");
close.addActionListener((ee) -> dlg.dispose());
dlg.addComponent(BorderLayout.SOUTH, close);
dlg.show(height, height, width, width);
The DialogText contains several lines that need to be scrollable on low resolution devices, but the above code doesn't achieve this. What have I missed? Also tried making the SpanLabel scrollable but that didn't work.
Just moved this project from Eclipse to Netbeans (new to this). Using old GUI builder but not for this Dialog.

I think I found the answer - use the show methods in Dialog as in:
Dialog dlg = new Dialog();
dlg.setUIID("AboutDialog");
String title = "About";
String txt = DialogText.aboutTxt[txtItem];
dlg.setLayout(new BorderLayout());
dlg.setScrollableY(true);
dlg.setScrollVisible(true);
dlg.show(title, txt, Dialog.TYPE_INFO, logo_icon, "", "Close");
Needs a bit of tweaking but the scroll now works.
Apologies if I've wasted anyone's time.
Later: Unable to 'tweak' the above code, so in case it helps someone else, I finally got scrollable text in the Dialog using:
String title = DialogText.getTitleUIID(txtItem);
String txt = DialogText.dialogTxt[txtItem];
Dialog dlg = new Dialog();
dlg.setTitle(title);
dlg.setLayout(new BorderLayout());
TextArea txtArea = new TextArea(txt);
txtArea.setScrollVisible(true);
dlg.add(BorderLayout.CENTER, txtArea);
Button close = new Button("Close");
close.addActionListener((ee) -> dlg.dispose());
dlg.addComponent(BorderLayout.SOUTH, close);
dlg.showAtPosition(0, 0, 0, 0, true);

Related

Eclipse SWT Text Boxes change size

I had an Eclipse plug-in app (ten-year-old code, no documentation, etc.) dropped in my lap and while adding new features to it, I noticed that when a panel is resized, the text boxes change size continuously while the separator is being dragged.
As you can see in the second picture, the text boxes are kind of randomly sized. Is there a setting in SWT that will prevent this from happening?
Here's how I'm creating one of the text boxes. The others are basically clones of this:
Font font = parent.getFont();
setLayout(new FillLayout());
SashForm sashForm = new SashForm(this, SWT.VERTICAL);
FormToolkit toolkit = new FormToolkit(getParent().getDisplay());
Section section = toolkit.createSection(sashForm,
Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
section.setLayoutData(new GridData(GridData.FILL_BOTH));
section.setLayout(new GridLayout());
section.setText("Section Title");
Composite controlComposite = toolkit.createComposite(section);
GridLayout controlLayout = new GridLayout();
controlLayout.numColumns = 2;
controlLayout.verticalSpacing = 20;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Font bold = ResourceManager.getBoldFont(font);
Label textLabel = toolkit.createLabel(controlComposite, "Title:", SWT.BOLD);
GridData gd = new GridData();
gd.horizontalSpan = 1;
textLabel.setLayoutData(gd);
textLabel.setFont(bold);
textBox = new ExtendedText(controlComposite, SWT.BORDER | SWT.SINGLE, false);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 1;
gd.verticalSpan = 2;
textBox.setLayoutData(gd);
The ExtendedText class is an extension of StyledText. The important bits of it are this:
GridData gd_bg = new GridData(GridData.FILL_BOTH);
setLayoutData(gd_bg);
final GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 0;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
sashForm.setWeights(new int[] { 1, 1 });
Okay, after digging in a little deeper, I got it working as expected.
First, the controlComposite and controlLayout objects are now created using
Composite controlComposite = new Composite(section, SWT.NONE)
controlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
controlComposite.setBackground(section.getBackground());
GridLayout controlLayout = new GridLayout(2, true);
controlLayout.marginHeight = 20;
controlLayout.marginWidth = 0;
controlLayout.verticalSpacing = 10;
controlLayout.horizontalSpacing = 0;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Once I did that, things started to stabilize. I also ended up tweaking the weights to this:
sashForm.setWeights(new int[] { 2, 3 });
It's not perfect, but it'll do for now.
Thanks to #greg-449 and #Baz for taking a look

How to draw the subtitle that is in the ImageJ window

I have made my own customized GUI where it displays images that are opened and read by ImageJ. I am able to display the images but I can't display the subtitle that is displayed in the ImageJ window before the image.
This is what I have been trying but it doesn't seem to display anything. Is there something simple that I am missing?
JPanel panel2 = new JPanel();
ImageCanvas ic = new ImageCanvas(image);
StackWindow sw = new StackWindow(image,ic);
Insets insets = sw.getInsets();
BufferedImage bi = image.getBufferedImage();
Graphics2D g = (Graphics2D)bi.getGraphics();
//g.drawImage(image.getBufferedImage(), 0, 0, null);
g.drawString(sw.createSubtitle(), insets.left+5, insets.top+10);
//image.draw();
sw.drawInfo(g);
panel2.add(sw.getContentPane());
frame.add(panel2);

Disable window resizing in SWT - using composite

Am developing an eclipse plugin which has few wizard pages. I need the wizard window size to be constant, with "BOTH MAXIMISE and MINIMISE disabled", "window RESIZE disabled".
The point is I am not using SHELL. I am using COMPOSITE instead, which doesn't have any style bits.
How can I do that? I am just providing a part of my entire code:
public void createControl(Composite parent)
{
// TODO Auto-generated method stub
composite = new Composite(parent, SWT.NONE );
composite.setLayout(new GridLayout());
Composite selectAdapterComposite = new Composite(composite, SWT.NONE);
FormLayout reportOptionsCompositeLayout = new FormLayout();
reportOptionsCompositeLayout.marginHeight = 1;
reportOptionsCompositeLayout.marginWidth = 1;
selectAdapterComposite.setLayout(reportOptionsCompositeLayout);
buttonInterfaceSelection = new Button(selectAdapterComposite,SWT.RADIO);
//SWT.CHECK);
buttonInterfaceSelection.setText("Generate adapter using interface !");
buttonInterfaceSelection.setSelection(true);
buttonInterfaceSelection.addListener(SWT.Selection, this);
FormData exportInToExcelButtonData = new FormData();
exportInToExcelButtonData.left = new FormAttachment(null, 5);
buttonInterfaceSelection.setLayoutData(exportInToExcelButtonData);
// One Text Box
Label searchBoxLabel = new Label(selectAdapterComposite, SWT.None);
searchBoxLabel.setText("Search to select [Type to get the results below]");
FormData destinationLabelData = new FormData();
destinationLabelData.top = new FormAttachment(buttonInterfaceSelection, 10);
destinationLabelData.left = new FormAttachment(null, 5);
searchBoxLabel.setLayoutData(destinationLabelData);
searchTextBox = new Text(selectAdapterComposite, SWT.BORDER);
searchTextBox.setSize(20, 2);
FormData searchTextBoxData = new FormData();
searchTextBoxData.top = new FormAttachment(searchBoxLabel, 8);
searchTextBoxData.left = new FormAttachment(null, 5);
// destinationFolderPathData.left = new
// FormAttachment(destinationLabel,15);
searchTextBoxData.width = 400;
searchTextBox.addListener(SWT.Modify, this);
searchTextBox.setEnabled(true);
searchTextBox.setLayoutData(searchTextBoxData);
.
.
.
.
.
setControl(composite);
}
Please help me out.
Your code snippet is irrelevant to your question. The key word is wizard. When you create that wizard, it requires a Shell, so you can set its style bits there.
A WizardDialog's constructor:
public WizardDialog(Shell parentShell, IWizard newWizard)
Example of shell style bits:
parentShell.setShellStyle(parentShell.getShellStyle() | (~SWT.RESIZE));
Thanks for your reply... am a newbie to swt and your answer gave me an important info which I dint know before. Now then, I just took some time to go through widgets documentation and found something.
Composite : Instances of this class are controls which are capable of containing other controls.
Shell : Instances of this class represent the "windows" which the desktop or "window manager" is managing.
I realised that my understanding of SHELL and COMPOSITE was wrong.
Conclusion: So I have to depend upon SHELL to give window resizing controls and using a COMPOSITE does not give me any resizing option...
Correct me if am wrong please.. hope this will be useful to other noobs too...
Thanks.
P.S.: now i understoood, my code segment is irrelevant to my question cos, I am working on someone else's code and trying to make some changes to it. instead of making changes in SHELL (which is created in some other class) i am doing it in COMPOSITE.

Grid Layout does not display correctly

I'm trying to build simple calculator gui with display and 9 buttons
public void init()
{
setSize(60,80);
inf = new InfoButton(this);
zero = new CalcButton(this,"0");
one = new CalcButton(this,"1");
add = new CalcButton(this,"+");
sub = new CalcButton(this,"-");
div = new CalcButton(this,"/");
mlt = new CalcButton(this,"*");
modu = new CalcButton(this,"%");
blank = new JButton("");
wys = new Wyswietlacz(); // its JTextPane
wys.setSize(60,20);
przyciski = new JPanel();
przyciski.setSize(60,60);
przyciski.setLayout(new GridLayout(3,3));
przyciski.add(zero);
przyciski.add(one);
przyciski.add(add);
przyciski.add(sub);
przyciski.add(mlt);
przyciski.add(div);
przyciski.add(modu);
przyciski.add(inf);
przyciski.add(blank);
calosc = new JPanel();
calosc.setLayout(new BoxLayout(calosc,BoxLayout.Y_AXIS));
calosc.add(wys);
calosc.add(przyciski);
calosc.setSize(60,80);
add(calosc);
}
and in main i make frame with size (60,80) but when i make it visible all i can see is display and one row of buttons. What am i doing wrong?
Call setPreferredSize(..) instead of setSize() on wys and przyciski. Then use JFrame's pack() instead of specifying a size for it.

Java fixed element's position

I need to build BlackJack game as an study project.
I want build it with SWING GUI. What I need it just divide the screen in 2 parts, and then to be able insert elements (in my case it's extended JButton with signed ImageIcon) using absolute (x, y) position relative to specified part.
Something like that:
I came from developing under Android, where you can work with elements in very simple way, and I feel lost in SWING. There aren't AbsoluteLayout or something like that?
Here is one example of my several attempts to this:
public void run() {
// TODO Auto-generated method stub
JFrame jFrame = new JFrame("Blackjack");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = jFrame.getContentPane();
Insets insets = pane.getInsets();
URL url = ClassLoader.getSystemClassLoader().getResource("10_of_clubs.png");
BufferedImage bi = null;
try {
bi = ImageIO.read(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Image resizedImage = bi.getScaledInstance(128, 186, 0);
ImageIcon icon = new ImageIcon(resizedImage);
ImageButton imgButton = new ImageButton(icon);
imgButton.setPreferredSize(new Dimension(128, 186));
ImageButton imgButton2 = new ImageButton(icon);
imgButton.setPreferredSize(new Dimension(128, 186));
pane.setLayout(new GridBagLayout());
JPanel headPanel = new JPanel();
JPanel headPanel2 = new JPanel();
GridBagConstraints cns = new GridBagConstraints();
cns.gridx = 0;
cns.gridy = 0;
cns.weightx = 0.5;
cns.weighty = 0.2;
cns.anchor = GridBagConstraints.FIRST_LINE_START;
cns.fill = GridBagConstraints.BOTH;
headPanel.setBackground(Color.RED);
headPanel.add(imgButton, cns);
GridBagConstraints cns2 = new GridBagConstraints();
cns2.gridx = 0;
cns2.gridy = 0;
cns2.weightx = 0.5;
cns2.weighty = 0.2;
cns2.anchor = GridBagConstraints.FIRST_LINE_START;
cns2.fill = GridBagConstraints.CENTER;
headPanel2.setBackground(Color.BLUE);
headPanel2.add(imgButton2, cns2);
pane.add(headPanel);
pane.add(headPanel2);
jFrame.setSize(800, 600);
jFrame.setVisible(true);
jFrame.setLocationRelativeTo(null);
}
That what I get:
Tnx.
if you want absolute layout, please take a look at: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
in general to read about layouts in java you can take a look at:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
here is all java swing components: visual guide:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
I think you can use JSplitPane (http://algo.math.ntua.gr/~symvonis/other-material/java_material/JavaTutorial/uiswing/components/splitpane.html) to create vertical separation
Since you have overlapping elements you can:
Use your existing JButtons with images inside a JLayeredPane. Put your cards on different layers for a clean rendering. Set the position of your Cards absolute with 'setBounds()'
Draw your cards with absolute position yourself using a Canvas. If you take this approach, you will also have to do your Click handling yourself (check if a click is inside a card.)

Categories

Resources