LogoLogo
v7.0.0
v7.0.0
  • TotalCross Overview
  • TotalCross Javadoc
  • TotalCross Changelog
  • Roadmap
  • Documentation
    • Getting Started
      • First embedded project with TotalCross
    • Components
      • Accordion
      • Aligned Labels
      • Button
      • Check
      • ComboBox
      • Dynamic Scroll
      • Edit
      • Floating Button
      • Gpiod
      • Grid
      • GridContainer
      • Image
      • ImageControl
      • ImageList
      • Label
      • Material Icons
      • Material Window
      • MessageBox
      • Multi Edit
      • Progress Bar
      • Progress Box
      • Radio
      • Radio Group
      • Scroll Container
      • Side Menu
      • Slider
      • Sliding Window
      • Spin List
      • Spinner
      • Switch
      • Tabbed Container
      • Velocimeter
    • APIs
      • API Overview
      • API Rest
      • Asynchronous Task
      • Camera
      • Control
        • Main Window
        • Window
        • Container
      • GPS
      • HTTPS and SSL
      • JSON
      • Maps
        • Maps - Deprecated
        • Static Map
      • Material Design Standards
      • Ninepath
      • Notifications
      • PrinterManager
      • Push Notification Firebase
      • Scanner
      • SOAP
      • Socket
      • SocketServer
      • SQLite Encryption
      • QR Code Generator
      • totalcross.sys
      • Youtube API
    • Creating an Issue
    • Contributing
      • Branch workflow
      • Writing documentation
    • Guides
      • App Architecture
        • Suggested Architecture
        • Why do Design Patterns help with the application's organization?
          • MVC Architecture Pattern
          • Template Pattern
          • Data Persistence: DAO Pattern.
        • Separation of concepts: What is the best way to create UI interfaces?
        • Positioning
          • Manual Positioning
        • Relative Positioning
        • Best practices to improve project maintenance
      • Device Simulator
      • Package your app from scratch
        • TotalCross SDK
        • Environment Variables in IDE
          • Eclipse
          • IntelliJ
        • Deploy your app with a dependecy TC
        • Deploy iOS
          • Using Development certificate to test your apps
      • Understanding TotalCross for Linux ARM
      • Running C++ applications with TotalCross
      • Web Services
    • Miscelaneous
      • Java JDK 8
      • Maven
      • Installing Visual Studio Code
    • FAQ
      • IMEI in Android 10
Powered by GitBook
On this page
  • Overview
  • Source Code
  • Attributes
  • Methods

Was this helpful?

  1. Documentation
  2. Components

Aligned Labels

PreviousAccordionNextButton

Last updated 6 years ago

Was this helpful?

Overview

Aligned Label is a Container used to align all controls to the maximum width of a set of labels.

In Totalcross this component is called AlignedLabelContainer.

Source Code

AlignedLabelsSample.java
import totalcross.ui.AlignedLabelsContainer;
import totalcross.ui.Button;
import totalcross.ui.ComboBox;
import totalcross.ui.Edit;
import totalcross.ui.Label;
import totalcross.ui.ListBox;
import totalcross.ui.ScrollContainer;
import totalcross.util.UnitsConverter;

public class extends ScrollContainer {
	private int gap = UnitsConverter.toPixels(10 + DP);
	private boolean canInsert = true;
	private ListBox lb;
	private Label output;

	@Override
	public void initUI() {
		uiAdjustmentsBasedOnFontHeightIsSupported = false;
		setBackForeColors(0xF7F7F7, 0x000000);
		setScrollBars(false, true);

		String[] labels = { "Name", "Born date", "Telephone", "Address", "City", "Country" };
		Edit edits[] = new Edit[5];
		edits[0].transparentBackground = true;
		Edit.useNativeNumericPad = true;

		for (int i = 0; i < edits.length; i++) {
			switch (i) {
			case 1:
				edits[i] = new Edit("99/99/9999");
				edits[i].setMode(Edit.NORMAL, true);
				edits[i].setValidChars(Edit.numbersSet);
				edits[i].setKeyboard(Edit.KBD_NUMERIC);
				break;
			case 2:
				edits[i] = new Edit("(99)9999-99999");
				edits[i].setMode(Edit.NORMAL, true);
				edits[i].setValidChars(Edit.numbersSet);
				edits[i].setKeyboard(Edit.KBD_NUMERIC);
				break;
			default:
				edits[i] = new Edit();
			}
		}

		Label title = new Label("This is an AlignedLabelsContainer.\nAll the content will be automatically aligned.",
				CENTER, 0, true);
		title.autoSplit = true;
		add(title, LEFT + gap, TOP + gap, FILL - gap, PREFERRED);

		AlignedLabelsContainer alc = new AlignedLabelsContainer();
		alc.uiAdjustmentsBasedOnFontHeightIsSupported = false;
		alc.labelAlign = RIGHT;

		alc.setInsets(gap, gap, 0, 0);
		alc.setLabels(labels, edits[0].getPreferredHeight());
		add(alc, LEFT, AFTER, FILL, PREFERRED);
		int i;
		for (i = 0; i < edits.length - 1; i++) {
			alc.add(edits[i], LEFT + gap, alc.getLineY(i), FILL - gap, PREFERRED);
		}

		Button btnInsert = new Button("Insert data", (byte) 0);
		btnInsert.setBackForeColors(0x4583d4, 0xFFFFFF);
		alc.add(edits[edits.length - 1], LEFT + gap, alc.getLineY(i), edits[3].getWidth() / 2 - gap / 2, PREFERRED);
		alc.add(btnInsert, RIGHT - gap, CENTER_OF, SAME, PREFERRED, edits[edits.length - 1]);

		ComboBox cbCountry = new ComboBox(new String[] { "Brazil", "USA" });
		alc.add(cbCountry, LEFT + gap, alc.getLineY(++i), SAME, PREFERRED, edits[edits.length - 1]);

		Button btnClear = new Button("CLEAR DATA", (byte) 0);
		alc.add(btnClear, RIGHT - gap, CENTER_OF, SAME, PREFERRED);

		btnInsert.addPressListener(e -> {
			if (canInsert) {
				lb = new ListBox();
				for (int j = 0; j < edits.length; j++)
					lb.add(labels[j] + ": " + edits[j].getText());
				if (cbCountry.getSelectedIndex() != -1)
					lb.add("Country: " + cbCountry.getSelectedItem());
				else
					lb.add("Country: ");

				output = new Label("OUTPUT:");
				output.setFont(font.asBold());
				add(output, CENTER, AFTER);
				add(lb, CENTER, AFTER + gap, SCREENSIZE + 80, PREFERRED);
				canInsert = false;

				scrollToControl(lb);
			} else {
				lb.removeAll();
				for (int j = 0; j < edits.length; j++)
					lb.add(labels[j] + ": " + edits[j].getText());
				if (cbCountry.getSelectedIndex() != -1)
					lb.add("Country: " + cbCountry.getSelectedItem());
				else
					lb.add("Country: ");
			}
			// reposition(); reposition bugando o edit
		});

		btnClear.addPressListener(e -> {

			// Cleaning the labels' content
			for (Edit edit : edits)
				edit.clear();
			cbCountry.setSelectedIndex(-1);
			if (!canInsert) {
				// Cleaning the output
				remove(lb);
				remove(output);
				canInsert = true;
			}
		});
	}
}

Attributes

Type

Name

Description

Font

childrenFont

Set this member to the font you want to set to the controls that are added to this container

int[ ]

foreColors

Sets an array with the same number of labels and the colors you want to show for each label

int

labelAlign

The alignment of the labels

Methods

ype

Name

Description

Construtor

AlignedLabelsContainer( )

Creates a new AlignedLabelsContainer without labels

Construtor

AlignedLabelsContainer(String[] labels)

Creates a new AlignedLabelsContainer with the given labels

Construtor

AlignedLabelsContainer(String[] labels, int vgap)

Creates a new AlignedLabelsContainer with the given labels and a vertical gap between the labels

void

add(Control c)

Since this is an AlignedLabelsContainer, use this to add a label

int

getLineY(int line)

Given a line (staring from 0), returns the y position

void

setLabels(String[ ] labels, int vgap)

Sets the labels and the extra gap between rows (which may be 0)