I've been using SWT for a few years, but i can't seem to figure this one out:
I need to SPLIT a VIEW into 2 "zones": LEFT and RIGHT
So i use a Composite with GridLayout with 2 columns to do it.
Inside the RIGHT composite i have a FIXED number of columns (created inside another GridLayout composite) but inside the LEFT composite i need to create a DYNAMIC number of columns that spans on the composite limit...
Is there a way to achieve this?
I tried a RowLayout inside left composite, but these 2 don't match :-\
Thanks
As it is not clear what you mean by
columns that spans on the composite limit
I tried a RowLayout inside left composite, but these 2 don't match
Therefore I am assuming that you want a dynamically changing gridlayout for your left composite. See the code below, specially the button.addSelectionListener().
import java.util.Random;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class DynamicComposite
{
public static void main(String[] args) {
new DynamicComposite().start();
}
private Composite compositeLeft;
private Composite compositeRight;
private Random rand;
public void start()
{
rand = new Random(System.currentTimeMillis());
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, true));
shell.setText("Dynamic Columns");
createRightComposite(shell);
createLeftComposite(shell);
createButtonComposite(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private void createButtonComposite(Shell shell)
{
Label l = new Label(shell, SWT.SEPARATOR|SWT.HORIZONTAL);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
gridData.horizontalSpan = 2;
l.setLayoutData(gridData);
Button button = new Button(shell, SWT.PUSH);
gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
gridData.horizontalSpan = 2;
button.setLayoutData(gridData);
button.setText("Change Columns !!");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(compositeLeft == null || compositeLeft.isDisposed())
return;
GridLayout layout = (GridLayout)compositeLeft.getLayout();
int col = rand.nextInt(7);
if(col == 0)
col = 1;
layout.numColumns = col;
//compositeLeft.setLayout(layout);
compositeLeft.layout(); // You need to re-layout your composite
}
});
}
private void createRightComposite(Shell shell)
{
compositeLeft = new Composite(shell, SWT.NONE);
GridLayout gridLayout = new GridLayout(3, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.marginLeft = 0;
gridLayout.marginRight= 0;
gridLayout.marginTop = 0;
gridLayout.marginBottom = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
gridLayout.horizontalSpacing = 0;
compositeLeft.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
compositeLeft.setLayoutData(gridData);
int counter = 17;
for(int i=0; i<counter; i++)
{
Button button = new Button(compositeLeft, SWT.PUSH);
button.setText("Button " + (i+1));
GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
button.setLayoutData(bData);
}
}
private void createLeftComposite(Shell shell)
{
compositeRight = new Composite(shell, SWT.NONE);
GridLayout gridLayout = new GridLayout(3, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.marginLeft = 0;
gridLayout.marginRight= 0;
gridLayout.marginTop = 0;
gridLayout.marginBottom = 0;
gridLayout.horizontalSpacing = 0;
compositeRight.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
compositeRight.setLayoutData(gridData);
int counter = 7;
for(int i=0; i<counter; i++)
{
Button button = new Button(compositeRight, SWT.PUSH);
button.setText("Button " + (i+1));
GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
button.setLayoutData(bData);
}
}
}
Related
enter image description here
enter image description here
I created an App and when I run it on a Motorola Xoom it executes the main activity as I expected but when I run it on a Huawei phone it displays Hello World and never executes the main activity. (Android 4.1.2 and 8 respectively. I have created projects using the Basic Activity and the Empty Activity and get the same result with both). How do I stop the Hello World?
package com.example.xoom;
import android.hardware.usb.UsbConstants;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity
{
final Button[] pitchButton = new Button[50];
int startLength;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String ACTION_USB_PERMISSION = "com.example.xoom";
byte[] sendChar = {0x5A}; // ASCII "Z"
byte[] getChar = new byte[200];
boolean addListener;
TextView tv = new TextView(this);
int buttonCount = 0;
for (int i = 0; i < 50; i++)
{
pitchButton[i] = new Button(this);
pitchButton[i].setTypeface(Typeface.MONOSPACE);
}
tv.setTextSize(20);
getWindow().getDecorView().setBackgroundColor(Color.CYAN);
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
if (manager.getDeviceList().isEmpty()) // The device list is empty
{
tv.setTextColor(Color.RED);
tv.append("No USB Devices Found");
}
else
{
tv.setTextColor(Color.BLUE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
UsbDevice device = deviceIterator.next(); // Get first device in list
PendingIntent pi = PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(device, pi);
UsbDeviceConnection connection = manager.openDevice(device);
getDeviceProperties(device, manager, tv); // Show USB device properties
getInterfaceProperties(device, connection, tv); // Show USB Interface properties
tv.setText(""); // Clear information from above because you don't want to see
// it normally but sometimes you do. Make this statement
// a comment if you do.
setConnectionProperties(connection); // Set speed etc of connection
UsbEndpoint endpointOut = device.getInterface(1).getEndpoint(1);
UsbEndpoint endpointIn = device.getInterface(1).getEndpoint(0);
connection.bulkTransfer(endpointOut, sendChar, 1, 500); // Send request for data
//
// It takes more than one read to get the data out of the FIFO
// Build up message by polling the FIFO until there is nothing left
// This has taken a lot of fiddling about to make it work. The baud
// and the timeouts have to be just so.
// (9600 and 200 work here but why who knows (or cares))
//
int k = 0;
char[] labelStr = new char[20];
int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
int bytesReceived = connection.bulkTransfer(endpointIn, getChar, 200, 200);
while (bytesReceived > 0)
{
for (int j = 0; j < bytesReceived; j++)
{
labelStr[k++] = (char) getChar[j];
if (getChar[j] == 13) // Carriage return
{
String label = String.valueOf(labelStr, 1, k - 1);
SpannableString spanLabel = new SpannableString(label);
spanLabel.setSpan(new RelativeSizeSpan(1.0f), 0, spanLabel.length(), flag);
addListener = true;
if (label.contains("Cross Slide"))
{ // Start adding pitches
spanLabel = new SpannableString("Pitch");
spanLabel.setSpan(new ForegroundColorSpan(Color.RED), 0, spanLabel.length(), flag);
addListener = false;
}
if (label.contains("End"))
{ // Start adding lengths
spanLabel = new SpannableString("Length");
spanLabel.setSpan(new ForegroundColorSpan(Color.RED), 0, spanLabel.length(), flag);
addListener = false;
if (startLength == 0) // Avoid the second "End" messages from MCU
startLength = buttonCount;
}
pitchButton[buttonCount].setText(spanLabel);
pitchButton[buttonCount].setId(buttonCount);
if (addListener)
{
pitchButton[buttonCount].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int buttonIndex;
Button buttonText = (Button) v;
String bt = buttonText.getText().toString();
String message;
if (bt.contains(".")) // On Button press display selected value
{
message = "Pitch ";
buttonIndex = 0;
}
else
{
message = "Length ";
buttonIndex = startLength;
}
message = message + bt;
pitchButton[buttonIndex].setText(message);
pitchButton[buttonIndex].setBackgroundColor(Color.GREEN);
}
});
}
buttonCount++;
k = 0;
}
}
bytesReceived = connection.bulkTransfer(endpointIn, getChar, 200, 200);
}
connection.close();
RelativeLayout.LayoutParams[] params = new RelativeLayout.LayoutParams[100];
RelativeLayout rel = new RelativeLayout(this);
rel.addView(tv);
int leftMargin = 0;
int topMargin = 0;
int lineCount = 0;
for (int i = 0; i < buttonCount - 1; i++)
{
params[i] = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params[i].width = 150;
params[i].height = 50;
if (Math.IEEEremainder(lineCount, 5) == 0) // Put 5 buttons per line
{
topMargin = topMargin + params[i].height;
leftMargin = 0;
}
lineCount++;
if (pitchButton[i].getText().toString().contains("Pitch"))
{
topMargin = topMargin + params[i].height;
leftMargin = 0;
lineCount = 0;
}
if (pitchButton[i].getText().toString().contains("Length"))
{
topMargin = topMargin + params[i].height;
leftMargin = 0;
lineCount = 0;
}
params[i].leftMargin = leftMargin;
leftMargin = leftMargin + params[i].width;
params[i].topMargin = topMargin;
pitchButton[i].setLayoutParams(params[i]);
rel.addView(pitchButton[i]);
}
//
//Add control for setting taper
//
ArrayList<String> taperArray = new ArrayList<String>();
taperArray.add("1");
taperArray.add("2");
taperArray.add("3");
Spinner spinner = new Spinner(this,Spinner.MODE_DIALOG);
spinner.setBackgroundColor(Color.BLUE);
String taperPrompt = "Set Taper Angle";
RelativeLayout.LayoutParams spinnerParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.width = 50;
spinnerParams.height = 40;
spinnerParams.topMargin = 600;
spinnerParams.leftMargin = 200;
spinner.setPrompt("Select Taper Angle");
spinner.setLayoutParams(spinnerParams);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, taperArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
}
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
RelativeLayout.LayoutParams taperParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
taperParams.width = 200;
taperParams.height = 50;
taperParams.topMargin = 600;
taperParams.leftMargin = 10;
TextView taperView = new TextView(this);
taperView.setLayoutParams(taperParams);
taperView.setText(taperPrompt);
taperView.setTextColor(Color.RED);
taperView.setTextSize(20);
rel.addView(taperView);
rel.addView(spinner);
setContentView(rel);
}
}
public void setConnectionProperties(UsbDeviceConnection connection)
{
connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
// mConnection.controlTransfer(0×40,
// 0, 1, 0, null, 0,
// 0);//clear Rx
connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);// flow
// control
// none
connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);// baudrate
// 9600 (4138 is 9600, 1A is 115200 0271 is 4800)
connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);// data bit
// 8, parity
// none,
// stop bit
// 1, tx off
}
public void getInterfaceProperties(UsbDevice device, UsbDeviceConnection connection, TextView tv)
{
int interfaceCount = device.getInterfaceCount();
int endPointCount;
for (int i=0; i < interfaceCount-1; i++)
{
if (connection.claimInterface(device.getInterface(i), true)) {
tv.append("\nConnection Interface " + i + " Claim succeeded \n");
UsbInterface interfce = device.getInterface(i);
endPointCount = interfce.getEndpointCount();
tv.append("Endpoint count for Interface " + i + " is " + endPointCount + "\n");
for (int j = 0; j < endPointCount; j++) {
if (interfce.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_INT)
tv.append("Endpoint " + j + " Type is Xfer Int \n");
if (interfce.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
tv.append("Endpoint " + j + " Type is Bulk Xfer \n");
if (interfce.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN)
tv.append("Endpoint " + j + " Direction is IN \n");
if (interfce.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT)
tv.append("Endpoint " + j + " Direction is OUT \n");
tv.append("Max packet size = " + interfce.getEndpoint(j).getMaxPacketSize() + "\n");
}
tv.append("Interface Protocol is " + interfce.getInterfaceProtocol() + "\n");
}
}
}
public void getDeviceProperties(UsbDevice device, UsbManager manager, TextView tv)
{
tv.setTextSize(20);
tv.setTextColor(Color.BLUE);
tv.append("USB Device name = " + device.getDeviceName() + "\n");
tv.append("USB Product Id = " + device.getProductId() + "\n");
tv.append("USB Vendor Id = " + device.getVendorId() + "\n");
tv.append("USB Device Id = " + device.getDeviceId() + "\n");
if (manager.hasPermission(device)) // Can the App access the device ?
{
tv.append("Device Permission = Yes \n");
}
else
{
tv.append("Device Permission = No \n");
}
}
}
I have found the answer – when no USB devices are attached to the phone the message No USB Devices Found could ever be displayed on the screen. By adding the line setContentView(tv) after the line tv.append(“No USB Devices Found”) leaves this message on the screen rather than falling back to the Hello World message. Thank you for looking at it. I would still like to know if it is possible to get rid of the Hello World fragment as it seems totally unnecessary (or maybe it is).
Add New Row in GridView winform on Button Click.
Need to have single button in grid rather than DataGridViewButtonColumn
you can add it by using
GridView1.DataSource = datatable;
and then add new row by this
datatable.Rows.Add();
this will add new row and give control id into add().
use this
public partial class Form1 : Form
{
Button textBoxDgv1 = new Button();
Label labelDgv1 = new Label();
the next is on the Form_Load event
private void Form1_Load(object sender, EventArgs e)
{
labelDgv1.Text = "Delete";
labelDgv1.Height = 20;
labelDgv1.AutoSize = false;
labelDgv1.BorderStyle = BorderStyle.FixedSingle;
labelDgv1.TextAlign = ContentAlignment.MiddleCenter;
int Xdgv1 = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgv1;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
this.dataGridView1.Controls.Add(labelDgv1);
and one more section is in dataGridView1_CellPainting
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sum = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[3].Value != string.Empty)
{
sum += Convert.ToInt32(this.dataGridView1[3, i].Value);
}
}
textBoxDgv1.Text = sum.ToString();
int Xdgvx = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgvx;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
textBoxDgv1.Width = this.dataGridView1.Columns[3].Width;
Xdgvx = this.dataGridView1.GetCellDisplayRectangle(3, -1, true).Location.X;
textBoxDgv1.Location = new Point(Xdgvx, this.dataGridView1.Height - textBoxDgv1.Height);
}
Is there any way to create a Tab Menu in j2me?
I found a code but I am unable to understand it
In this code there is Tab Menu created which is in Canvas class and then Tab menu is created which is totally done in Canvas or painted. The only part I found difficult to grasp was the void go() method and then
When I try to draw anything above and below this code using paint method, it doesn't work - what's the problem?
Below is the code
// Tab Menu CANVAS class
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class TabMenuCanvas extends Canvas
{
TabMenu menu = null;
public TabMenuCanvas()
{
menu = new TabMenu(
new String[]{"Home", "News", "Community", "Your files", "Credits", "Events", "Blog", "Upload", "Forum Nokia"},
getWidth() - 20
);
}
protected void keyPressed(int key)
{
int gameAction = getGameAction(key);
if(gameAction == Canvas.RIGHT)
{
menu.goRight();
repaint();
}
else if(gameAction == Canvas.LEFT)
{
menu.goLeft();
repaint();
}
}
protected void paint(Graphics g)
{
g.translate(10, 30);
menu.paint(g);
g.translate(- 10, - 30);
}
}
// Tab Menu Class
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class TabMenu
{
int background = 0xffffff;
int bgColor = 0xcccccc;
int bgFocusedColor = 0x0000ff;
int foreColor = 0x000000;
int foreFocusedColor = 0xffffff;
int cornerRadius = 6;
int padding = 2;
int margin = 2;
Font font = Font.getDefaultFont();
int scrollStep = 20;
int selectedTab = 0; //selected tab index
int[] tabsWidth = null; //width of single tabs
int[] tabsLeft = null; //left X coordinate of single tabs
int tabHeight = 0; //height of tabs (equal for all tabs)
String[] tabs = null; //tab labels
int menuWidth = 0; //total menu width
int viewportWidth = 0; //visible viewport width
int viewportX = 0; //current viewport X coordinate
public TabMenu(String[] tabs, int width)
{
this.tabs = tabs;
this.viewportWidth = width;
initialize();
}
void initialize()
{
tabHeight = font.getHeight() + cornerRadius + 2 * padding; //[ same for all tabs]
menuWidth = 0;
tabsWidth = new int[tabs.length];
tabsLeft = new int[tabs.length];
for(int i = 0; i < tabsWidth.length; i++)
{
tabsWidth[i] = font.stringWidth(tabs[i]) + 2 * padding + 2 * cornerRadius;
tabsLeft[i] = menuWidth;
menuWidth += tabsWidth[i];
if(i > 0)
{
menuWidth += margin;
}
}
}
public void goRight()
{
go(+1);
}
public void goLeft()
{
go(-1);
}
private void go(int delta)
{
int newTab = Math.max(0, Math.min(tabs.length - 1, selectedTab + delta));
boolean scroll = true;
if(newTab != selectedTab && isTabVisible(newTab))
{
selectedTab = newTab;
if( (delta > 0 && tabsLeft[selectedTab] + tabsWidth[selectedTab] > viewportX + viewportWidth) ||
(delta < 0 && tabsLeft[selectedTab] < viewportX))
{
scroll = true;
}
else
{
scroll = false;
}
}
if(scroll)
{
viewportX = Math.max(0, Math.min(menuWidth - viewportWidth, viewportX + delta * scrollStep));
}
}
private boolean isTabVisible(int tabIndex)
{
return tabsLeft[tabIndex] < viewportX + viewportWidth &&
tabsLeft[tabIndex] + tabsWidth[tabIndex] >= viewportX;
}
public void paint(Graphics g)
{
int currentX = - viewportX;
g.setClip(0, 0, viewportWidth, tabHeight);
g.setColor(background);
g.fillRect(0, 0, viewportWidth, tabHeight);
for(int i = 0; i < tabs.length; i++)
{
g.setColor(i == selectedTab ? bgFocusedColor : bgColor);
g.fillRoundRect(currentX, 0, tabsWidth[i], tabHeight + cornerRadius, 2 * cornerRadius, 2 * cornerRadius);
g.setColor(i == selectedTab ? foreFocusedColor : foreColor);
g.drawString(tabs[i], currentX + cornerRadius + padding, cornerRadius + padding, Graphics.LEFT | Graphics.TOP);
currentX += tabsWidth[i] + margin;
}
}
}
When I try to draw anything above and below this code using paint method, it doesn't work
what of the paint methods you use to draw above and below? Pay attention that there are two methods named that way - first is in TabMenuCanvas, second is in TabMenu (second method is invoked from TabMenuCanvas#repaint).
whatever you would try to draw in TabMenuCanvas#paint will most likely be overwritten by setClip and fillRect when TabMenu#paint is invoked following repaint request
The only place where one can expect to be able to draw something visible seems to be in TabMenu#paint method, inside the clip area that is set there.
You can use GUI Libraries for J2ME,for example Lightweight User Interface Toolkit (LWUIT),Flemil have "tab menu".You can see list of GUI Libraries here.
How do I get JTextPane to scroll? In this example, JScrollPane is employed but as running the code will reveal, the scroll bar can be displayed but it is not functional.
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.text.*;
public class JTextPaneTester
{
JTextPane jtp = new JTextPane();
StyledDocument doc;
Style style;
JScrollPane jsp = new JScrollPane(jtp);
JTextPaneTester()
{
doc = (StyledDocument)jtp.getDocument();
style = doc.addStyle("fancy", null);
jsp.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
void append(String s)
{
try
{
doc.insertString(doc.getLength(), s, style);
}
catch (BadLocationException e) { assert false: "problem"; }
}
public static void main(String[] args)
{
JTextPaneTester thing = new JTextPaneTester();
for (int i = 0; i < 100; i++)
thing.append("nouns verbs adjectives \n");
JFrame f = new JFrame();
JPanel center = new JPanel();
f.add(center, BorderLayout.CENTER);
center.add(thing.jsp);
f.setSize(400, 400);
f.setVisible(true);
}
}
If I place the snippet
for (int i = 0; i < 100; i++) {
thing.append("nouns verbs adjectives \n");
}
after
f.setVisible(true);
it seems to work.
I am new to SWT but have plenty of experience w/ other GUI layout managers.
I have run in to a weird problem when nesting a composite inside of another composite.
http://www.swooby.com/swt/nestedcontrolproblem.png
If I run the audiocontrol as a standalone bean it works fine.
If I run it nested in another composite it starts to act funny.
When I add this composite to a more complex parent (6 columns), the nested composite does not seem to be honoring its own class defined horizontal span correctly.
The audiocontrol lays out fine in a lesser complex parent (total of 2 columns).
I am using Eclipse Visual Editor to lay these out, so I have not written any of my own code to alter the layout (except to change a Composite type to CompositeAudio).
Code to repro this:
Parent.java
package com.twistpair.qa;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
public class Parent extends Composite {
private AudioControl compositeAudio = null;
public Parent(Composite parent, int style) {
super(parent, style);
initialize();
}
private void initialize() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
this.setLayout(gridLayout);
createCompositeAudio();
setSize(new Point(97, 673));
}
/**
* This method initializes compositeAudio
*
*/
private void createCompositeAudio() {
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.CENTER;
gridData.grabExcessHorizontalSpace = false;
gridData.grabExcessVerticalSpace = false;
gridData.horizontalSpan = 2;
gridData.verticalAlignment = GridData.CENTER;
compositeAudio = new AudioControl(this, SWT.NONE);
compositeAudio.setLayout(new GridLayout());
compositeAudio.setLayoutData(gridData);
}
} // #jve:decl-index=0:visual-constraint="36,7"
AudioControl.java:
package com.twistpair.qa;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;
public class AudioControl extends Composite {
private CLabel cLabelAudio = null;
private Button checkBoxRunning = null;
private CLabel cLabelVolume = null;
private Scale scaleVolumeLeft = null;
private CLabel cLabelVuMeter = null;
private Canvas canvasVuMeterLeft = null;
private Button checkBoxMuteRx = null;
private Button checkBoxMuteTx = null;
private Button checkBoxMuteBoth = null;
private Scale scaleVolumeRight = null;
private Canvas canvasVuMeterRight = null;
public AudioControl(Composite parent, int style) {
super(parent, style);
initialize();
}
private void initialize() {
GridData gridData31 = new GridData();
gridData31.horizontalAlignment = GridData.CENTER;
gridData31.verticalAlignment = GridData.CENTER;
GridData gridData21 = new GridData();
gridData21.horizontalAlignment = GridData.CENTER;
gridData21.verticalAlignment = GridData.CENTER;
GridData gridData11 = new GridData();
gridData11.horizontalSpan = 2;
GridData gridData5 = new GridData();
gridData5.horizontalSpan = 2;
gridData5.verticalAlignment = GridData.CENTER;
gridData5.horizontalAlignment = GridData.CENTER;
GridData gridData4 = new GridData();
gridData4.horizontalSpan = 2;
gridData4.verticalAlignment = GridData.CENTER;
gridData4.horizontalAlignment = GridData.CENTER;
GridData gridData3 = new GridData();
gridData3.horizontalSpan = 2;
gridData3.verticalAlignment = GridData.CENTER;
gridData3.horizontalAlignment = GridData.CENTER;
GridData gridData2 = new GridData();
gridData2.horizontalSpan = 2;
GridData gridData1 = new GridData();
gridData1.horizontalSpan = 2;
GridData gridData = new GridData();
gridData.horizontalSpan = 2;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
cLabelAudio = new CLabel(this, SWT.NONE);
cLabelAudio.setText("Audio");
cLabelAudio.setFont(new Font(Display.getDefault(), "Segoe UI", 9, SWT.BOLD));
cLabelAudio.setLayoutData(gridData5);
checkBoxRunning = new Button(this, SWT.CHECK);
checkBoxRunning.setText("Running");
checkBoxRunning.setLayoutData(gridData11);
cLabelVolume = new CLabel(this, SWT.NONE);
cLabelVolume.setText("Volume");
cLabelVolume.setFont(new Font(Display.getDefault(), "Segoe UI", 9, SWT.BOLD));
cLabelVolume.setLayoutData(gridData4);
scaleVolumeLeft = new Scale(this, SWT.VERTICAL);
scaleVolumeLeft.setLayoutData(gridData21);
scaleVolumeRight = new Scale(this, SWT.VERTICAL);
scaleVolumeRight.setLayoutData(gridData31);
cLabelVuMeter = new CLabel(this, SWT.NONE);
cLabelVuMeter.setText("VU Meter");
cLabelVuMeter.setFont(new Font(Display.getDefault(), "Segoe UI", 9, SWT.BOLD));
cLabelVuMeter.setLayoutData(gridData3);
createCanvasVuMeterLeft();
this.setLayout(gridLayout);
this.setSize(new Point(151, 415));
createCanvasVuMeterRight();
checkBoxMuteRx = new Button(this, SWT.CHECK);
checkBoxMuteRx.setText("Mute RX");
checkBoxMuteRx.setLayoutData(gridData);
checkBoxMuteTx = new Button(this, SWT.CHECK);
checkBoxMuteTx.setText("Mute TX");
checkBoxMuteTx.setLayoutData(gridData1);
checkBoxMuteBoth = new Button(this, SWT.CHECK);
checkBoxMuteBoth.setText("Mute Both");
checkBoxMuteBoth.setLayoutData(gridData2);
}
/**
* This method initializes canvasVuMeterLeft
*
*/
private void createCanvasVuMeterLeft() {
canvasVuMeterLeft = new Canvas(this, SWT.BORDER);
}
/**
* This method initializes canvasVuMeterRight
*
*/
private void createCanvasVuMeterRight() {
canvasVuMeterRight = new Canvas(this, SWT.BORDER);
}
} // #jve:decl-index=0:visual-constraint="10,10"
My other problem was that the audiocontrol initially had the three "Mute *" checkboxes underneath the two VU (left/right) canvases. In a lesser complex parent the layout was mostly behaving, but the three checkboxes below the VU canvases were not being created.
I used a spy program to browse the running UI, and the controls were indeed not there. The code did have valid objects that I could manipulate.
I thought this was weird, and seem to have temporarily solved the problem by just moving the checkboxes, but I think this could be an indicator that there is something wrong w/ my audiocontrol.
Has anyone seen anything like these two problems? I searched the net and stackoverflow and didn't see anything directly related.
Thanks!
Pv