i am adding widgets to a horizontal panel, and i want them to be all once next to the other on the left corner.
even though i have set the spacing=0 and alignment= left the widgets still have space between them. they are spread evenly in the panel.
please see the code here for the widget C'tor and the function that adds a new tab (toggle button)
tabsPanel is a horizontalPanel, that you can see is aligned to left/right according to the locale
any advise would be appreciated
thanks....
public TabsWidgetManager(int width, int height, int tabs_shift_direction){
DecoratorPanel decorContent = new DecoratorPanel();
DecoratorPanel decorTitle = new DecoratorPanel();
widgetPanel.setSize(Integer.toString(width), Integer.toString(height));
tabsPanel.setSize(Integer.toString(UIConst.USER_CONTENT_WIDTH), Integer.toString(UIConst.TW_DEFAULT_TAB_HEIGHT));
tabsPanel.setSpacing(0);
if (tabs_shift_direction==1)
tabsPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
else
tabsPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
decorTitle.add(tabsPanel);
contentPanel.setSize(Integer.toString(UIConst.USER_CONTENT_WIDTH), Integer.toString(UIConst.USER_CONTENT_MINUS_TABS_HEIGHT));
decorContent.add(contentPanel);
widgetPanel.add(decorTitle, 0, 0);
widgetPanel.add(decorContent, 0, UIConst.TW_DEFAULT_TAB_HEIGHT+15);
initWidget(widgetPanel);
}
public void addTab(String title, Widget widget){
widget.setVisible(false);
ToggleButton tab = new ToggleButton(title);
tabsList.add(tab);
tab.setSize(Integer.toString(UIConst.TW_TAB_DEFAULT_WIDTH), Integer.toString(UIConst.TW_TAB_DEFAULT_HEIGHT));
tab.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
handleTabClick((ToggleButton)event.getSource());
}
});
//adding to the map
tabToWidget.put(tab, widget);
// adding to the tabs bar
tabsPanel.add(tab);
//adding to the content
contentPanel.add(widget);
}
I assume contentPanel is your HorizontalPanel. HorizontalPanel will generate an html table. You're setting the width of the panel. If the width is wider than the sum of all widths of your sub widgets the table will spread the widgets evenly. Remove the setWidth() call.
Related
I have a JScrollPane with a JPanel in it, which shows something like a time sheet. This time sheet panel is zoomable abd scrollable. I have many entries in this time sheet panel, each entry is an own panel and has a start time and a length.
When I now click on an entry in this time sheet, I want to zoom in this entry and, of course, move to it.
For example: The actual zoom shows 3 Month from January to March. Now I have an entry at the 1st of March until the 3rd of March. This entry is now for example 10 pixel long. Wren I click on this entry, the zoom factor is changed in that way, that the time sheet only shows 3 days and starts on the 1st of March, so that the entries width is over the complete scroll panes view port.
For this, I calculate the new zoom factor and the new Start X Position of the time sheet panel and call a repaint of the panel.
This works with the zoom, but because the time sheet panel has not the new width at this moment, this leads to that the scroll pane scrollbars have not the new sizes. So I can not set the new scrollbar position at this point of code.
So, how can I set the right scrollbar position after the repaint of the scroll pane is done and the scrollers have their new sizes?
Here is some pseudo code for mor understanding (Sorry for the formating but I didnt manage to paste the code so that the indents are correct):
public class MyRootPanel extends JPanel
{
TimeSheetPanel tsPanel = new TimeSheetPanel(this);
tsPanel.setLayout(null);
scrollPanel=new JScrollPane(tsPanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.add(scrollPanel);
public setScrollPos(int xPos)
{
scrollPanel.getHorizontalScrollBar().setValue(xPos);
}
}
public class TimeSheetPanel extends JPanel
{
MyRootPanel rootPanel;
public int pixelPerHour = 5; // Zoom factor
public int rowHeight = 30; // height of one row on table
public TimeSheetPanel(MyRootPanel root)
{
rootPanel = root;
this.setLayout(null);
setBounds(0,0, maxDays * pixelPerHour *24, maxrows * rowHeight);
for(Entry e : entryList)
{
EntryPanel entryPanel = new EntryPanel(this, entry);
this.add(entryPanel);
}
}
public void zoomToEntry(Entry entry)
{
// calc new zoom factor here.
pixelPerHour = ......
newXPos = .....
// and set it to the panel
setBounds(0,0, maxDays * pixelPerHour *24, maxrows * rowHeight);
// call repaint
repaint();
// THIS WILL NOT WORK
rootPanel.setScrollPos(newXPos);
}
}
public class EntryPanel extends JPanel
{
TimeSheetPanel parentPanel;
Entry entry;
public EntryPanel(TimeSheetPanel parent, Entry e)
{
parentPanel = parent;
Entry = e;
this.setLayout(null);
int left = entry.start * parentPanel.pixelPreHour;
int width = entry.length * parentPanel.pixelPerHour;
int top = entry.row * parentPanel.rowHeight;
int height = parentPanel.rowHeight;
setBounds(left, top, width, height);
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
parentPanel.zoomToEntry(entry);
}
}
}
For anybody searching for an answer, I solved my problem by using invokeLater
repaint();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
valuePanelScrollPane.getHorizontalScrollBar().setValue(xpos);
valuePanelScrollPane.getVerticalScrollBar().setValue(ypos);
}
});
I have implemented a custom view in my Eclipse plugin project, where I want to display different graphs, which are selected by the user. I have no problem with creating a custom view, using org.eclipse.swt.graphics.GC and drawing the required parts but I would like to implement the following:
Inside the custom view I want to have some fixed area on the bounds of the view, where I can display the coordinate system (x- and y-axis with the corresponding labeling), which is fixed. Between these bounds I would like to display the graph, which is changing dynamically, depending on user selection.
So what I need is a custom view, that is built like the following:
Inside the grey area on the left and on the bottom I want to have the coordinate system axes (shown red) and inside the white area I want to draw the graph.
How do I create such an area inside my view? It should just be a filed, without any translations or scales, just a independend area, like a view inside a view...
#Override
public void init(IViewSite site) throws PartInitException {
super.init(site);
}
#Override
public void createPartControl(Composite parent) {
canvas = new Canvas(parent, SWT.NONE);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
drawCoordinateSystem(e);
drawGraph(e);
});
public void drawCoordinateSystem(PaintEvent e) {
// 1. create area inside view
// 2. draw coordinate system
}
public void drawGraph() {
// 3. draw graph
}
I know how to solve point 2 and 3. But I don't know, how to create such an area inside my view.
I would appreciate any help!
I would suggest that you add multiple controls and handle drawing for different cases in a different way.
For example, instead of having just one monolithic Canvas that draws everything from graph to coordinates, to helpful labels. Add a bunch of label controls on top for showing coordinates, add a parent, or labels for coordinate systems etc.
Your createPartControl should look something like below:
Composite topBar = new Composite(parent, SWT.None);
topBar.setLayout(new GridLayout(3, false));
Label title = new Label(topBar, SWT.NONE);
title.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
Label x = new Label(topBar, SWT.NONE);
Label y = new Label(topBar, SWT.NONE);
Composite graphArea = new Composite(parent, SWT.NONE);
graphArea.setLayout(new GridLayout(2, false));
Canvas xAxis = new Canvas(graphArea, SWT.NONE);
GridData gd = new GridData(GridData.CENTER, GridData.FILL, false, true);
gd.widthHint = 10;
xAxis.setLayoutData(gd);
xAxis.addPaintListener(new PaintListener() {
#Override
public void paintControl(PaintEvent e) {
drawXAxis();
}
});
Canvas graph = new Canvas(graphArea, SWT.NONE);
graph.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
drawGraph(e);
}
});
Label emptySpace = new Label(graphArea, SWT.NONE);
Canvas yAxis = new Canvas(graphArea, SWT.NONE);
GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
gd.heightHint = 10;
xAxis.setLayoutData(gd);
yAxis.addPaintListener(new PaintListener() {
#Override
public void paintControl(PaintEvent e) {
drawYAxis();
}
});
You should draw everything in Your paintListener, if Your graph changes just redraw area of graph for example:
Rectangle rect = getGraphBoundries(); //you must implement this method
canvas.redraw(rect.x, rect.y, rect.width, rect.height, true);
this will redraw graph area. To avoid unnecessary computing of axes area in in paintListener paintEvent's gc has area that need to be redrawn called clipping. You can write condition like this:
public void paintControl(PaintEvent e) {
...
if(drawGraph(e.gc.getClipping().intersects(getGraphBoundries())) {
drawGraph(e);
}
...
}
I use libgdx's scene2d built-in UI library to make an UI in my game. I'm interesting how can I draw 2 images(or actors) in a table one on another? I'm looking to the similar like LayerDrawable in Android. Is there any exists stuff to make it?
There is a Stack widget in libGDX UI library for these purposes.
UPDATE: if you want to place one widget on top of another widget and these widgets have different size then you should wrap the smaller widget. Something like:
//First add actual content
stack.add(content);
//Second add wrapped overlay object
Table overlay = new Table();
overlay.add(overlayWidget).expand().fillX().bottom().left();
stack.add(overlay);
stack variable is a Stack instance
It's an actual excerpt from my project. You should tune it for your case.
You can use somethings like this :
//create stage
stage = new Stage(new StretchViewport(game.width, game.height));
//create table
Table table = new Table();
table.setSize(game.width,game.height); //fullscreen
//add Image 1
table.add(new Image(game.skin.getDrawable("image1")));
//add Image 2 (create new col)
table.add(new Image(game.skin.getDrawable("image2"))).width(xx).height(xx);
//add new row
table.row();
//add Image 3 - padding to draw over image1/2 && colspan
table.add(new Image(game.skin.getDrawable("image3"))).width(xx).height(xx).padTop(-50f).colspan(2);
//add table to stage
stage.addActor(table);
/*
* Add action
*
*/
#Override
public void render(float delta) {
...
//don't forget to draw ur stage
stage.act(delta); // or stage.act(Gdx.graphics.getDeltaTime())
stage.draw();
}
Also you can add action to stage/table or image etc ... :
stage.addAction(Actions.sequence(
Actions.fadeOut(0.0001f),
Actions.fadeIn(0.5f),
Actions.delay(0.1f),
Actions.run(new Runnable() {
#Override
public void run() {
//mmmm new screen
dispose();
game.setScreen(new BoobsScreen(game));
}
})));
Of course you can draw more than image : button, textbutton, label etc or add scrollPane for Table but before take a look at Skin on libgdx
For exemple a programmatically ninepatch textbutton :
private NinePatch processNinePatchFile(String fname) {
final Texture t = new Texture(Gdx.files.internal(fname));
final int width = t.getWidth() - 2;
final int height = t.getHeight() - 2;
return new NinePatch(new TextureRegion(t, 1, 1, width, height), 3, 3, 3, 3);
}
//create skin
skin = new Skin();
...
//create font
...
//create ninepatch button from android/assets/style/button
NinePatch ninepatch = processNinePatchFile("style/button.9.png");
skin.add("button.9", ninepatch);
//create textbuttonstyle
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = skin.getFont("font_black_38");
textButtonStyle.fontColor = Color.DARK_GRAY;
textButtonStyle.up = skin.getDrawable("button.9");
skin.add("buttonTextStyle", textButtonStyle);
//now you can create textbutton
TextButton btn = new TextButton("Hello", game.skin.get("buttonTextStyle", TextButton.TextButtonStyle.class));
//add a clicklistener
btn.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
//mmmmmm add action for exemple
stage.addAction(Actions.sequence(Actions.fadeOut(0.5f), Actions.run(new Runnable() {
#Override
public void run() {
//dispose and load screen
dispose();
game.setScreen(new MoreBoobsScreen(game));
}
})));
}
});
table.add(btn);
To create bitmapfont take a look at Gdx freetype, it's really easy to use.
I am using a JTabbedPane in my application. I have added two tabs which are instances of a custom class "ContentPanel". This extends JPanel and sets the background, border etc etc. Basically it means I dont have to set the properties of each JPanel I want to apply this colour scheme to. I notice that not only does their border appear but another border (which, I think, is blue - at least on my screen) appears around this border, connected to the tab "selectors" themselves (i.e. the buttons you click on to get the appropriate view). I would like to change this border as it just looks odd against a gold / brown colour scheme. Does anyone have any idea how to do this? I have tried JTabbedPane.setBorder(Border b) but that doesnt work. That simply sets a border around the entire thing, including the tab selectors.. not what I want.
Any help with this would be greatly appreciated.
These colors are defined in the Look and Feel. If you look at the code for BasicTabbedPaneUI, you will notice that installDefaults() sets a bunch of protected Color instance variables. The keys they are defined against in the L&F are also available here.
protected void installDefaults() {
LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background",
"TabbedPane.foreground", "TabbedPane.font");
highlight = UIManager.getColor("TabbedPane.light");
lightHighlight = UIManager.getColor("TabbedPane.highlight");
shadow = UIManager.getColor("TabbedPane.shadow");
darkShadow = UIManager.getColor("TabbedPane.darkShadow");
//...
// a lot more stuff
//...
}
If you do not want to go as far as define your own L&F, you have the ability to set a custom UI delegate on your tabbed pane:
myTabbedPane.setUI(new BasicTabbedPaneUI() {
#Override
protected void installDefaults() {
super.installDefaults();
highlight = Color.pink;
lightHighlight = Color.green;
shadow = Color.red;
darkShadow = Color.cyan;
focus = Color.yellow;
}
});
you may of course want to change those color settings. As set, you will see which vars are used where.
None affecting L&F and JVM run-time system-wide settings code solution.
Create your own tabbed-pane class and nested tabbed-pane-UI class to deal with the issue for a "specific" class of tabbed-pane. The code below is original: (The last answer was 2010, but this may be useful too.)
public class DisplayTabbedPane extends JTabbedPane implements
MouseListener, ChangeListener {
public DisplayTabbedPane() {
setTabPlacement(SwingConstants.BOTTOM);
// UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
// works but is a JVM system wide change rather than a specific change
NoInsetTabbedPaneUI ui = new NoInsetTabbedPaneUI();
// this will build the L&F settings for various tabbed UI components.
setUI( ui );
// override the content border insets to remove the tabbed-pane
// blue border around the pane
ui.overrideContentBorderInsetsOfUI();
}
/**
* Class to modify the UI layout of tabbed-pane which we wish to override
* in some way. This modification only applies to objects of this class.
* Doing UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
* would affect all tabbed-panes in the JVM run-time.
*
* This is free to use, no copyright but is "AS IS".
*/
class NoInsetTabbedPaneUI extends MetalTabbedPaneUI {
/**
* Create tabbed-pane-UI object to allow fine control of the
* L&F of this specific object.
*/
NoInsetTabbedPaneUI(){
super();
}
/**
* Override the content border insets of the UI which represent
* the L&F of the border around the pane. In this case only care
* about having a bottom inset.
*/
public void overrideContentBorderInsetsOfUI(){
this.contentBorderInsets.top = 0;
this.contentBorderInsets.left = 0;
this.contentBorderInsets.right = 0;
this.contentBorderInsets.bottom = 2;
}
}
........
}
Change Look And Feel with "UIManager"
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new BackgroundPainter(Color.GRAY));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new BackgroundPainter(Color.white));
BackgroundPainter class
public class BackgroundPainter implements Painter<JComponent> {
private Color color = null;
BackgroundPainter(Color c) {
color = c;
}
#Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
if (color != null) {
g.setColor(color);
g.fillRect(0, 0, width - 1, height - 1);
}
}
}
I would like to remove the scrollbar arrow buttons from a scrollbar in a JScrollPane. How would I do this?
class NoArrowScrollBarUI extends BasicScrollBarUI {
protected JButton createZeroButton() {
JButton button = new JButton("zero button");
Dimension zeroDim = new Dimension(0,0);
button.setPreferredSize(zeroDim);
button.setMinimumSize(zeroDim);
button.setMaximumSize(zeroDim);
return button;
}
#Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
#Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
#Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
//own painting if needed
}
#Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
//own painting if needed
}
}
Removing buttons let space for then. I found make buttons zero as the simplest way.
If you are using the basic version of JScrollBar, then it is probably rendering using the BasicScrollBarUI. I would suggest that you extend BasicScrollBarUI to create a custom UI class (like MyBasicScrollBarUI) . The buttons are protected variables in the superclass. So you need to override the installComponents() methods in the subclass and make sure that you do not add the buttons. See the below code snippet and hide the lines as suggested there.
protected void installComponents(){
switch (scrollbar.getOrientation()) {
case JScrollBar.VERTICAL:
incrButton = createIncreaseButton(SOUTH);
decrButton = createDecreaseButton(NORTH);
break;
case JScrollBar.HORIZONTAL:
if (scrollbar.getComponentOrientation().isLeftToRight()) {
incrButton = createIncreaseButton(EAST);
decrButton = createDecreaseButton(WEST);
} else {
incrButton = createIncreaseButton(WEST);
decrButton = createDecreaseButton(EAST);
}
break;
}
scrollbar.add(incrButton); // Comment out this line to hide arrow
scrollbar.add(decrButton); // Comment out this line to hide arrow
// Force the children's enabled state to be updated.
scrollbar.setEnabled(scrollbar.isEnabled());
}
Then, in your code after you initialize a JScrollBar, you can call setUI() and pass in an instance of MyBasicScrollBarUI class.
Note: I havent tried this myself, but from the code it looks like it could work.
It is not the most elegant way... but works for me
JScrollBar jsb = getHorizontalScrollBar();
for(Component c : jsb.getComponents()) {
jsb.remove(c);
}
This is the way i went.
Set the scrollbar policy of the scrollbar you want to hide as never
Mimic this behavior with a MouseWheelListener
This method:
Is fast to implement with very few lines of code.
Retains the benefits of the L&F.
Will remove both the buttons and the bar.
Below is a sample code for removing the verticall scroll bar.
JScrollPane myScrollPane = new JScrollPane();
//remove the scroll bar you don't want
myScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
JTextPane myJTextArea = new JTextPane();
//myScrollPane.setViewportView(myJTextArea);
myScrollPane.addMouseWheelListener(new MouseWheelListener() {
//this will mimick the behavior of scrolling
public void mouseWheelMoved(MouseWheelEvent e) {
JScrollBar scrollBar = myScrollPane.getVerticalScrollBar();
//capturing previous value
int previousValue = scrollBar.getValue();
int addAmount;
//decide where the wheel scrolled
//depending on how fast you want to scroll
//you can chane the addAmount to something greater or lesser
if(e.getWheelRotation()>0) {
addAmount = 2;
}else {
addAmount = -2;
}
//set the new value
scrollBar.setValue(previousValue + addAmount);
}
});