SWT button position after resizing - java

I'm working with SWT. I created a button, and it supposed to change its text after being pressed.
The second text is wider than the first one, and the button does not being re-sized appropriately.
so I used button.pack(), and setSize() functions. The real issue is actually with its alignment in the shell after this size changing.
it's being expanded to the right instead to the left.
I guess it's actually the normal behavior, but I'm not sure where to change it.
I tried to change things by creating a gridData but it only did worse.
The button parent is tabFolder, and the last consists of a gridlayout with two uneven columns, the spesific button is on the second column.
I tried to change things with the margins and few others, but without success.
currently all the configurations are default.
This is my button:
This is how I would like it to behave:
any help would be highly appreciated.
many thanks!

i would need some code to be sure, but it looks like you need to call tabFolder.layout(true) after changing the button text to compute the new position of your button

Related

Swing - Custom JButton messes with other components at rollover

On a JPanel I have a few buttons and JScrollPane containing a JTable.
One of the buttons is custom made, I have overridden the paintComponent method to draw an image.
However, if I set the contentAreaFilled or isOpaque properties to false (so that the background is not displayed) whenever I roll over the button the content of the scroll pane is hidden (a grey image appears inside, instead of the table contents). I tried using different layouts for my panel, but got the same result. The only fix I found is setting rolloverEnabled to false for my custom button, but that is not really an option for me, as I want the button image to change at rollover.
The weird thing is, it only happens when I set one of those two properties mentioned above to false. The button acts perfectly if I do not hide it's background. Just out of curiosity, I tried the same on my other two buttons, which are just JButtons with nothing customized, except their size, and it had the same effect on the scroll pane, so I'm pretty sure it doesn't have anything to do with the implementation of the custom button. Has anyone encountered this problem or know anything that may help me with this?

How to replace existing labels instead of adding more

I'm trying to make a program which outputs a 8x8 grid with each cell being a random colour once the button is pressed. I've got it to work, but now when I press the button twice, instead of replacing all the cells with another random colour, it creates another 8x8 grid right next to it. It makes sense why it does this from the way i've implemented it but I want to replace each cell not create more and I'm not sure how to do this.
Any help would be appreciated.
Pictures:
https://gyazo.com/282de6b6e324b246d23378052365efbe (the way it should be)
https://gyazo.com/7d9884be75957c4d50c8fc28c24f8f05 (2nd button press)
but I want to replace each cell
Don't replace the components.
Instead just change the existing components.
So instead of your looping code creating the new components. Your looping code will update the existing components. So you just change the property of each cell by using setBackground() or setIcon() or whatever method you use to set the color.

Eclipse android widget moving

I am designing my activity screen using eclipse but whenever I move an object e.g. a button it snaps around and I find it hard to position things where i want them.
Is there some setting I can change as as soon as I add a second button it snaps the other button to another position it is so annoying!
Also if I have an image button why can't i resize this, even with a normal button if i try to make it larger it just fits to the text and wont make the button bigger or it just flicks to a random place on the activity?
thanks
Probably you're trying to place components freely in the View but you're not considering the LayoutManager behavior. If you try to position your component in a LinearLayout, for example, the components will be placed following its rules, and these components will not stay where you drop them.
Try to understand better how the layouts works on Android. But for now, the AbsoluteLayout or RelativeLayout may be what you're looking for.
About the components size, you'll need to understand better how to use layout properties for these components. See the question How to size buttons for more information.

When i click a button i would like to activate only its surrounding buttons. How is this possible?

I have an 8 by 8 linear Layout of buttons. I am making a small game similar to boggle, when i click a button i would like to be able to disable all buttons except for its surrounding buttons. This also involves the buttons on the edge. I basically need some help to start the logic of this game. any help is appreciated. i have so far thought about the structure of the program and how an 8 by 8 graph can possibly activate its neighbors, maybe if i can access the cell of each button the i can progress further?. Thank you.
Extend Button and give Buttonlisteners to each of the buttons according to who their neighbors are. Each button would have a buttonlistener tied to each of it's neighbors according to how you tie it together. If you don't want to use diagonal neighbors, a button would have a max of 5 buttonlisteners (if the button is listening to it's own button).)
This should be possible to accomplish using a simple Map:
Map<Button, List<Button>> surroundingButtons = new HashMap<Button, List<Button>>();
This Map is then, for each Button, populated with a list of it's surrounding Buttons. Unfortunately I have no clever idea how to do this easily at this hour.
I am thinking something like adding all buttons to a single list and pick them out using index of the current button +/- 8 modulo some index... Adding them manually might be just as easy.
When the Map is ready, it is very simple to disable the surrounding Buttons.
Button clickedButton; // not showing the onClickListener
for (Button sButton: surroundingButtons.get(clickedButton)) {
sButton.setEnabled(false);
}
Edit:
Read the question wrong but; disable all buttons except for its surrounding buttons
The idea still applies, just add all the buttons you wish to disable to the list associated with a button in the map.

GUI components of a memory game

I am working on a homework so i am not asking for code, i am trying to make this by myself. by the way, i am stuck again with the GUI part and have little problems with code part. first things is about button size and image size. i didnt use methods for size of buttons just set the image as an icon for the button. but as you see below, buttons dont fit the image.
second thing is how can i first disable the icon and when user presses to button it will reveal the icon ?. and how can i embed 8 pictures in a loop? can i create an array for images... i appreciate if you can help me. and thanks anyway : )
I'd start with How to Use Buttons. JToggleButton works well for this, as you can change the Icon in an ItemListener based on the button's selected state. Examples may be found here and here.

Categories