I'm trying to create a scrollpane that has a background. However, I want the background to take up more than the area of the scrollpane. For instance, Here is my background and colored in red is where I want the scrollpane to be:
However, when I add buttons I get the following result:
How can I limit the actual scrollpane part to just a section (colored in red in the above picture)?
Here is what I have so far. I toyed around with spacing/padding but that did not produce any good results:
Skin skin = Resources.getSkin(Resources.SKIN_JSON);
container = new Table();
container.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
this.addActor(container);
Table table = new Table();
// table.debug();
final ScrollPane scroll = new ScrollPane(table, skin);
table.pad(10).defaults().expandX().space(4);
table.setSize(Resources.VIRTUAL_WIDTH, Resources.VIRTUAL_HEIGHT);
container.add(scroll).expand().fill().colspan(1);
container.row().space(10).padBottom(10);
Image background = new Image(Resources.getAtlas(Resources.SKIN_ATLAS).findRegion("main-panel-horz"));
container.setBackground(background.getDrawable());
TextButton button1 = new TextButton("Button1", skin);
table.add(button1);
table.row();
TextButton button2 = new TextButton("button2", skin);
table.add(button2);
table.row();
TextButton button3 = new TextButton("button3", skin);
table.add(button3);
table.row();
TextButton button4 = new TextButton("button4", skin);
table.add(button4);
What you need to do is pad the outer table to get its contents to fit in the region of the background that you would like. (Or you could alternatively pad the cell that you put the scroll pane in.)
If you use a 9-patch drawable for the background, the padding of the outer table will be done for you automatically. You could also specify a TextureRegionDrawable in your skin Json and use that as the background. That would also allow you to pad the table automatically.
You can easily get texture region drawables from the skin with skin.getDrawable(drawableName);--no need to create an intermediate Image just to create a drawable out of a region.
Here's how you would create a TextureRegionDrawable with explicit padding in json:
com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable: {
main-panel-horz-padded: { region: main-panel-horz, leftWidth: 50, rightWidth: 50, topHeight: 50, bottomHeight: 50 }
},
Then in your code:
container.setBackground(skin.getDrawable("main-panel-horz-padded"));
Related
I would like to make the next graphic with MPAndroidChart, but the pie chart is not possible to add an image, then someone knows how to add it's
enter image description here
MPAndroidChart you can add drawable icon as a label. Here is sample how to add drawable.
List<PieEntry> entries = new ArrayList<>();
Drawable icon = ContextCompat.getDrawable(getActivity(), R.drawable.name);
// Set the drawable icon to piechart
entries.add(new PieEntry(18.5f, icon));
PieDataSet set = new PieDataSet(entries, "Election Results");
PieData data = new PieData(set);
pieChart.setData(data);
I'm trying to add label here
Label missionTitle = new Label(mission.title, skin);
Label missionDesct = new Label(mission.desct, skin);
Label remainDay = new Label(TaskManager.getRemainMissionDay(mission.deadLine) + "day remain", skin);
missionDesct.setFontScale(width * 1.4f / 1080);
remainDay.setFontScale(width * 1.2f / 1080);
missionDesct.setWrap(true);
Table table = new Table();
table.debug();
table.background(new TextureRegionDrawable(new TextureRegion(new Texture("image/imgray.png"))));
table.add(missionTitle).width(width / 1.1f);
table.row();
table.add(missionDesct).width(width / 1.1f);
table.row();
table.add(remainDay).width(width/2.3f).left();
for that i'm adding this lines;
Label goldLabel = new Label("30 gold", skin);
table.add(goldLabel);
But result is;
this
How can i add a new cell over there?
I tried to adding ".width(width/2);" but it's not working.
...
table.row();
table.add(remainDay).width(width/2.3f).left();
Label goldLabel = new Label("30 gold", skin);
table.add(goldLabel).width(width/2.3f);
You can do that by creating a table with the 2 labels and then adding the table into the container table.
Example -
Table nestedTable = new Table();
nestedTable.add(remainDay).width(THE_WIDTH);
nestedTable.add(goldLabel).width(THE_WIDTH);
mainTable.add(nestedTable);
You can play around more with this to get the desired result :)
This class can also help - HorizontalGroup
Finally i understand my mistake.I was trying to add 2 cell to a row.But that was effecting the other row's cell count.I changed my code to this and solved;
Table table = new Table();
table.background(new TextureRegionDrawable(new TextureRegion(new Texture("image/imgray.png"))));
table.add(missionTitle).width(width/1.5f).left();
table.add(new TextButton("!", skin)).height(height/23).width(width / 5).right();
table.row().fillX();
table.add(missionDesct).expandX();
table.add(goldAndExp).right();
table.row();
table.add(remainDay).left();
Screenshot
I'll be happy if someone fix my grammar problems.
I've got a JPanel that has a BoxLayout (Page axis), and I want to lay out two components, one on top of the other.
My problem is the margin to the left of the large lipsum box, how can I get rid of this? If I don't add the top components, there is no margin.
Here's my code, the second image is created by not adding headerPanel:
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
this.add(headerPanel);
this.add(descLabel);
This class extends JPanel, and is added to a JFrame, which is simply pack()'d
Though I couldn't tell where the observed behaviour comes from, the expected display could be achieved by using an intermediate JPanel to contain your label, rather than adding the JLabel directly :
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
descPanel.add(descLabel);// added
this.add(headerPanel);
this.add(descPanel);// modified
My problem is the margin to the left of the large lipsum box, how can I get rid of this?
You need to make the alignments of your components consistent. That is the alignment "X" property of all the components should be left aligned.
I'm guessing the JLabel is center aligned so you need to use:
descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
See Fixing Alignment Problems section from the Swing tutorial on How to Use BoxLayout for more information and examples.
I have a Groovy app which uses a scrollPane built via swing builder:
BinsicWindow(def controller)
{
controlObject = controller
swinger = new SwingBuilder()
mainFrame = swinger.frame(
title: "Binsic is not Sinclair Instruction Code",
size:[640, 480],
show:true,
defaultCloseOperation: WindowConstants.DISPOSE_ON_CLOSE){
scrollPane(autoscrolls:true) {
screenZX = textArea(rows:24, columns:32) {visble:true}
}
screenZX.setFont(new Font("Monospaced", Font.PLAIN, 18))
}
}
I add text to the textArea programatically (i.e. no user input) and I would like the textArea to scroll down automatically as content is added. But the view remains fixed at the top and I can only see the bottom (once the screen is more than full) by dragging the mouse.
Can I fix this? I have been searching for an answer to this for a wee while now and getting nowhere. Apologies if it's a simple answer.
The following lines should scroll your textarea to the last text position:
rect = screenZX.modelToView(screenZX.getDocument().getLength() - 1);
screenZX.scrollRectToVisible(rect);
I am trying to add a scrollable area to my tabbed window. So far I have a CTabFolder in a shell. I have added 5 CTabItems to it and everything works as expected.
On one of my CTabItems the contents are too big to fit on the screen so I would like to be able to scroll. The contents is a collection of Groups each containing various widgets.
So the CTabFolder is created as follows:
CTabFolder tabs = new CTabFolder(shell, SWT.BORDER);
tabs.setSimple(false);
tabs.setUnselectedImageVisible(false);
tabs.setUnselectedCloseVisible(false);
tabs.setMinimizeVisible(false);
tabs.setMaximizeVisible(false);
FormData tabsLayoutData = new FormData();
tabsLayoutData.top = new FormAttachment(0, 5);
tabsLayoutData.left = new FormAttachment(0, 5);
tabsLayoutData.bottom = new FormAttachment(92, 0);
tabsLayoutData.right = new FormAttachment(100, -5);
tabs.setLayoutData(tabsLayoutData);
Then the CTabItem:
CTabItem tab = new CTabItem(tabs, SWT.NONE);
tab.setText("Role");
Then the contents:
Composite tabArea = new Composite(tabs, SWT.V_SCROLL);
tabArea.setLayout(new FormLayout());
tab.setControl(tabArea);
So the groups contained within the tab are created with tabArea as the parent and everything appears as you would expect. The problem is though that the vertical scroll bar is always present but doesn't seem to do anything. The contents are chopped off at the bottom of the tabArea composite.
Is there anything else I need to do to get the scrolling to work properly?
You need to use a ScrolledComposite. (Scroll down and find links to JavaDoc & snippets)
Here is code that should work in your situation:
ScrolledComposite scroller = new ScrolledComposite(tabs, SWT.BORDER | SWT.V_SCROLL);
Composite tabArea = new Composite(scroller, SWT.NONE);
scroller.setContent(tabArea);
// create some controls in TabArea and assign a layout to TabArea
scroller.setExpandVertical(true);
scroller.setExpandHorizontal(true);
scroller.setMinSize(tabArea.computeSize(SWT.DEFAULT, SWT.DEFAULT));
tab.setControl(scroller);