I have one query.i am creating 3 horizontal managers inside the vertical field manager.
while compiling my code i am getting IllegalStatException.i am doing this.
VerticalFieldmanager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
Then i am adding hfm's to the vfm
vfm.add(hfm1);
vfm.add(hfm1);
vfm.add(hfm1);
add(vfm);
i have done this but getting an exception.can anybody tell me solution for this..
You cant add the same field/manager to a manager over and over again.
you can do something like that using a for or a while and creating a new object inside and adding it to the parent manager
You're creating three HorizontalFieldManagers with the same name! Don't do that - try:
enter code here
VerticalFieldmanager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
hfm1 = new HorizontalFieldManager();
{somecode}
hfm1 = new HorizontalFieldManager();
{somecode}
OR
enter code here
VerticalFieldmanager vfm = new VerticalFieldManager();
for(int i=0;i<3;i++)
{
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
}
The thing is you are creating the same object hfm1 again and again for 3 times
You can either create 3 different objects like hfm1, hfm2 and hfm3
like
VerticalFieldmanager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
HorizontalFieldManager hfm2 = new HorizontalFieldManager();
{somecode}
HorizontalFieldManager hfm3 = new HorizontalFieldManager();
vfm.add(hfm1);
vfm.add(hfm2);
vfm.add(hfm3);
add(vfm)
or
VerticalFieldmanager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm1 = new HorizontalFieldManager();
{somecode}
hfm1 = new HorizontalFieldManager();
{somecode}
hfm1 = new HorizontalFieldManager();
{somecode}
But in this only the last hfm1 is added since due to runtime polymorphism... the object of first hfm1 releases it memory when the second hfm1 is initialised and the third hfm1 releases the object memory of the second hfm1 added
So only the last hfm1 is added to vfm
in this case ... Better follow the first approach.
Related
I'm trying to create a stacked bar chart using Primefaces dynamically based on a native SQL query. I'm able to iterate through my list to populate the chart, but the x values aren't showing up correctly. Here's a hardcoded example:
public CartesianChartModel ChartBean() {
model = new CartesianChartModel();
ChartSeries boys = new ChartSeries();
boys.setLabel("Boys");
boys.set("2004", 120);
ChartSeries boys2 = new ChartSeries();
boys2.setLabel("Boys2");
boys2.set("2005", 100);
ChartSeries girls = new ChartSeries();
girls.setLabel("Girls");
girls.set("2004", 52);
ChartSeries girls2 = new ChartSeries();
girls2.setLabel("Girls2");
girls2.set("2005", 60);
model.addSeries(boys);
model.addSeries(girls);
model.addSeries(boys2);
model.addSeries(girls2);
return model;
}
I post this as an answer because it is too lang to be posted as comment. If it does not answer your question please let me know and I will remove this answer..
I did not test it, but I could imagine that the model is expecting "complete series" for each series item. Depending on what you need either try using only two series:
ChartSeries boys = new ChartSeries();
boys.setLabel("Boys");
boys.set("2004", 120);
boys.set("2005", 100);
ChartSeries girls = new ChartSeries();
girls.setLabel("Girls");
girls.set("2004", 52);
girls.set("2005", 60);
Or fill up all your series
ChartSeries boys = new ChartSeries();
boys.setLabel("Boys");
boys.set("2004", 120);
boys.set("2005", 0);
ChartSeries boys2 = new ChartSeries();
boys2.setLabel("Boys2");
boys2.set("2004", 0);
boys2.set("2005", 100);
ChartSeries girls = new ChartSeries();
girls.setLabel("Girls");
girls.set("2004", 52);
girls.set("2005", 0);
ChartSeries girls2 = new ChartSeries();
girls2.setLabel("Girls2");
girls2.set("2004", 0);
girls2.set("2005", 60);
I am novice and trying to create programmatically:
But my program shows the following for now:
My code is here:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
layout.setBackgroundColor(0xFFFFFFDD);
TextView tView = new TextView(this);
tView.setText("TITLE");
tView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,34);
tView.setBackgroundColor(0xFFEEEE66);
tView.setGravity(Gravity.CENTER);
tView.setId(100);
layout.addView(tView);
View line = new View(this);
line.setId(150);
line.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,5 ));
line.setBackgroundColor(0xFF00FF00);
RelativeLayout.LayoutParams layoutParams150 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams150.addRule(RelativeLayout.BELOW,100);
layout.addView(line,layoutParams150);
TextView tView2 = new TextView(this);
tView2.setId(200);
tView2.setGravity(Gravity.LEFT);
tView2.setText("NAME1");
tView2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,34);
RelativeLayout.LayoutParams layoutParams200 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams200.addRule(RelativeLayout.BELOW,150);
layout.addView(tView2,layoutParams200);
I don't understand why "tView.setGravity(Gravity.CENTER);" does not work.
I don't understand why I can't draw the line.
BTW what is the best way to create such screen programmatically?
Thanks.
I am trying to set up my javafx scene, but i have failed miserable.
basically what i am trying to do is as per attached photo.
I have tried to set up a gridpane and then position another gridpane withing it so that I can set up the boxes and labels however this is not working properly.
I do not want to use FXML.
can you guys recommend how to go about this set up.
The JavaFx SceneBuilder is a good tool to create Layouts like the one you want to have. Take a look at the official website. The tool creates a FXML file which you can load in your applications start() method using:
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("application.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Another resource where you can get familiar with SceneBuilder is the #Java and #OracleLearning channels on Youtube.
This code worked for me in setting up the given arrangement:
GridPane outerPane = new GridPane();
GridPane innerPane1 = new GridPane();
GridPane innerPane2 = new GridPane();
HBox l = new HBox(new Label("Label"));
l.setAlignment(Pos.CENTER);
outerPane.add(l, 1, 1);
innerPane1.add(new Circle(20, Color.LIGHTCORAL), 1, 1);
outerPane.add(innerPane1, 2, 1);
innerPane2.add(new Button("Button"), 1, 1);
innerPane2.add(new Label(" Label"), 2, 1);
innerPane2.add(new Button("Button"), 1, 2);
innerPane2.add(new Label(" Label"), 2, 2);
innerPane2.add(new Button("Button"), 1, 3);
innerPane2.add(new Label(" Label"), 2, 3);
GridPane.setMargin(innerPane2, new Insets(5,5,5,5));
outerPane.add(innerPane2, 1, 2);
outerPane.setAlignment(Pos.CENTER);
Drop any further confusions in the comments :)
My current Mainscreen looks like :
The current code for each line of display is like:
RichTextField WeFix1 = new RichTextField("• Computer & Laptop", RichTextField.TEXT_JUSTIFY_HCENTER);
VFMTitle1 = new VerticalFieldManager(Field.USE_ALL_WIDTH| Field.NON_FOCUSABLE);
HFMTitle1 = new HorizontalFieldManager(FIELD_HCENTER);
HFMTitle1.add(WeFix1);
VFMTitle1.add(HFMTitle1);
add(VFMTitle21);
But need to it be positioned in a straight line :
There are many possibilities. You can adjust the margin of the container that holds list of services. Also you can make your custom field manager, and many more.
Option 1: Adjusting margin
VerticalFieldManager vfmServices = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);
vfmServices.add(new RichTextField("• Computer & Laptop"));
vfmServices.add(new RichTextField("• Modem / Router / Switches"));
vfmServices.add(new RichTextField("• Printer / Scanner"));
vfmServices.add(new RichTextField("• Tablet"));
final int horizontalMargin = 30;
vfmServices.setMargin(0, horizontalMargin, 0, horizontalMargin);
add(vfmServices);
Option 2: Using a custom field Manger
VerticalFieldManager vfmServiceLists = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);
vfmServiceLists.add(new RichTextField("• Computer & Laptop"));
vfmServiceLists.add(new RichTextField("• Modem / Router / Switches"));
vfmServiceLists.add(new RichTextField("• Printer / Scanner"));
vfmServiceLists.add(new RichTextField("• Tablet"));
Manager mListContainer = new Manager(Manager.USE_ALL_WIDTH) {
final int horizontalMargin = 30;
protected void sublayout(int width, int height) {
// this manager designed to contain only one list container.
if (getFieldCount() == 1) {
Field child = getField(0);
layoutChild(child, width - 2 * horizontalMargin, height);
// adjust manager height.
height = child.getHeight();
setPositionChild(child, horizontalMargin, 0);
}
setExtent(width, height);
}
};
mListContainer.add(vfmServiceLists);
add(mListContainer);
[Added Later]
As alishaik786 suggested in comment, if you use LabelField instead of RichTextField, you can check following code, which doesn't use any margin:
VerticalFieldManager vfmServiceLists = new VerticalFieldManager();
vfmServiceLists.add(new LabelField("• Computer & Laptop"));
vfmServiceLists.add(new LabelField("• Modem / Router / Switches"));
vfmServiceLists.add(new LabelField("• Printer / Scanner"));
vfmServiceLists.add(new LabelField("• Tablet"));
Manager mListContainer = new Manager(Manager.USE_ALL_WIDTH) {
protected void sublayout(int width, int height) {
// this manager designed to contain only one list container.
if (getFieldCount() == 1) {
Field child = getField(0);
layoutChild(child, width, height);
// adjust manager height.
height = child.getHeight();
setPositionChild(child, (width - child.getWidth()) / 2, 0);
}
setExtent(width, height);
}
};
mListContainer.add(vfmServiceLists);
add(mListContainer);
Better to use LabelField instead of RichTextField.
If you want the result on only RichTextField the below code is not suit for you; Then try to use our "Rupak" code;
This below code uses LabelField instead of RichTextField;
public class Abc extends MainScreen
{
VerticalFieldManager vertical;
Font font=Font.getDefault().derive(Font.BOLD, 18),small_font=Font.getDefault().derive(Font.PLAIN, 15);
LabelField labelField,labelFieldTwo;
public Abc()
{
createGUI();
}
private void createGUI()
{
vertical=new VerticalFieldManager(USE_ALL_WIDTH);
vertical.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
HorizontalFieldManager hor=new HorizontalFieldManager(Field.FIELD_HCENTER);
hor.add(new LabelField(" We Fix ", Field.NON_FOCUSABLE));
hor.setFont(font);
hor.setPadding(5, 0, 5, 0);
vertical.add(hor);
HorizontalFieldManager hr=new HorizontalFieldManager(Field.FIELD_HCENTER);
hr.add(new LabelField("."));
hr.add(new LabelField("Computer Science & Laptop", Field.NON_FOCUSABLE));
hr.setFont(small_font);
vertical.add(hr);
HorizontalFieldManager hr1=new HorizontalFieldManager(Field.FIELD_HCENTER);
hr1.add(new LabelField("."));
hr1.add(new LabelField("Modem/Router/Switches", Field.NON_FOCUSABLE));
hr1.setFont(small_font);
vertical.add(hr1);
//------------------- Up to now you want like this; and you can do like this type also like left align and right align;
HorizontalFieldManager hr2=new HorizontalFieldManager(Field.FIELD_LEFT);
hr2.add(new LabelField("."));
hr2.add(new LabelField("Printer/Scanner", Field.NON_FOCUSABLE));
hr2.setFont(small_font);
vertical.add(hr2);
HorizontalFieldManager hr3=new HorizontalFieldManager(Field.FIELD_LEFT);
hr3.add(new LabelField("."));
hr3.add(new LabelField("Tablet", Field.NON_FOCUSABLE));
hr3.setFont(small_font);
vertical.add(hr3);
HorizontalFieldManager hr4=new HorizontalFieldManager(Field.FIELD_RIGHT);
hr4.add(new LabelField("."));
hr4.add(new LabelField("Printer/Scanner", Field.NON_FOCUSABLE));
hr4.setFont(small_font);
vertical.add(hr4);
HorizontalFieldManager hr5=new HorizontalFieldManager(Field.FIELD_RIGHT);
hr5.add(new LabelField("."));
hr5.add(new LabelField("Tablet", Field.NON_FOCUSABLE));
hr5.setFont(small_font);
vertical.add(hr5);
vertical.setPadding(0, 0, 10, 0);
add(vertical);
}
}
I got like this when field align center, right and left:
Try this..
LabelField lbl=new LabelField("• Computer & Laptop\r\n• Modem / Router / Switches\r\n•
Printer / Scanner\r\n• Tablet",LabelField.FIELD_HCENTER);
I'm using LWUIT to create J2ME application. I used GridLayout to add the controls but they are not aligned.
Code:
flowLayout = new FlowLayout();
roundsPage.setLayout(flowLayout);
try {
image = image.createImage("/11.png"); // All image 40 x 40
starImage = Image.createImage("/j1.png");
smileImage = Image.createImage("/j2.png");
imgClock = Image.createImage("/clock.jpg");
imageTeam = Image.createImage("/name.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dateLabel=new Label("14-02-2012, Round1 ");
dateLabel.getStyle().setBgColor(0x8b8989);
dateLabel.getStyle().setFgColor(0xffffff);
roundsPage.addComponent(dateLabel);
//int screenWidth = Display.getInstance().getDisplayWidth();
Container tabContainer1 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
tabContainer1.addComponent(new Button("Click"));
tabContainer1.addComponent(new Button("Click"));
tabContainer1.addComponent(new Button("Click"));
tabContainer1.addComponent(new Button("Click"));
Container tabContainer2 = new Container(new BoxLayout(BoxLayout.X_AXIS));
team1=new Label(starImage);
team1.setText("Villarreal");
team1.setTextPosition(Component.BOTTOM);
//team1.setWidth(10);
//team1.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN ,Font.SIZE_SMALL));
tabContainer2.addComponent(team1);
tabContainer2.setWidth(100);
clockLabel=new Label(imgClock);
clockLabel.setText("18:00");
clockLabel.setTextPosition(Component.RIGHT);
tabContainer2.addComponent(clockLabel);
teamLabel=new Label(imageTeam);
teamLabel.setText("El Madrigal");
teamLabel.setTextPosition(Component.RIGHT);
tabContainer2.addComponent(teamLabel);
team2=new Label(smileImage);
team2.setText("Betis");
team2.setTextPosition(Component.BOTTOM);
team2.setFocusable(true);
tabContainer2.addComponent(team2);
Container tabContainer3 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
tabContainer3.addComponent(new Button("Click"));
tabContainer3.addComponent(new Button("Click"));
tabContainer3.addComponent(new Button("Click"));
tabContainer3.addComponent(new Button("Click"));
TabbedPane tabbbed = new TabbedPane();
tabbbed.addTab(null, image, tabContainer1 );
tabbbed.addTab(null, image, tabContainer2);
tabbbed.addTab(null, image, tabContainer3);
roundsPage.addComponent(tabbbed);
Thanks in advance.
Don't use like this "14-02-2012, Round1 ". Get the width of the display and set the preferredW on dateLabel. For getting screen width use this,
int width = Display.getInstance().getDisplayWidth();.
Also Don't use TabbedPane. Use Tabs. Add the button in to addTab and set the width of the button. Because each tab having same width and height when you use TabbedPane. Thats an issue.