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.
Related
I built a form with Netbeans JFrame Form, I mean from netbeanse itself by right click on the project and then New and then JFrame Form. Also I use a Jtable by dragging that on the form. Now in my code I want to colorize the rows on Jtable. I have seen some idea from https://coderanch.com/t/476777/java/Color-Row-JTable But all the direction which I have seen is about overriding Jatble. My problem is In my program netbeans it self does the code in declaration JFrame element from where I can override the code or specifically in this case how can I make the rows colorized ?
If you have already put the JTable in the JFrame you can right click on it and then chose Customize Code.
It will present you a window where some code is written. You can add custom code on the white lines, not the gray ones unless you want to change an already defined property.
Remember though that you should implement your custom cell renderer first.
In case you want to change an already defined property of the JTable, you have to first change the Default Code drop down menu on the left of the property to custom creation.
I hope this will help you out.
I'm Facing a problem while I try to add a label on the face of just another label in Java Netbeans. Every time I add a label just above of another label the new label (say label2) automatically goes to the leftmost side and shrink my first label( say label1). Actually, I want to add a background color blue to the top of the form as there are no other possible ways (as I know) to change certain parts of the form to another color I use this way. If there is another way of changing the background of certain parts of a form, share with me. But also help to add a label just above another label.
Thanks in advance .. :)
The following Image will show you my intention. Notice only to the head. There is a label on the left side of a blue label. I want to add that label on the surface of the blue label.
This is the image of Interface which I want to gain in JAVA. This is a Visual Basic MDI Form and list of options are available on the rightmost side. That's what I try to do but couldn't do yet.
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
I am new to libgdx. I was referring to the documentation here TableLayout-Alignment
Similar to this example, for simplicity's sake, let's say I have a nameLabel and a nameText widgets.
So when I do
table.add(nameLabel).width(100);
table.add(nameText).width(100);
I will get two widgets of of same size adjacent to each other in the middle of outside table as by-default it is centered aligned. something like this:
Now what I want to do, I want to send "nameLabel" to the extreme left and "nameText" to the extreme right.
What I am doing is that
table.add(nameLabel).width(100).left();
table.add(nameText).width(100).right();
But it won't work, until I expand both of these widgets as explained in the example above. Can anyone explain to me, why I have to expand for this alignment to work?
Calling cell.expand() make the cell expand its size to vertical or horizontal or both. Not everybody always want the cell expand after created by default.
The left and right functions only work if the cell is expanded.
I'm trying to write a scorekeeping app in Java that allows a server application to send scores to client applications. The clients will then display a list of teams, team IDs and their scores, sorted by score. Of course I could just use a swing JTable to display everything, but I want to achieve a unique effect: I want the text dynamically reorder itself every time a team moves up in the list. That is, it want to animate the text around the screen. I would also need to be able to update the contents of the labels after being added. What would be the best way to achieve this? Is there any component that allows you to add other components to it and place/move them freely?
JTable is a JComponent so you can set desired LayoutManager and add JLabels above the JTable. Then move/reorder them to achieve desired effect. See SwingWorker as well.
You could use a JTable and change the contents of the rows as teams move up. Or you could arrange a series of labels and change the text whenever you want. To change the value displayed for a JLabel you simply use the JLabel.setText("new value"); method.
I've never done this but I think you need to use a panel with a 'null' layout manager. Meaning you are responsible for absolutely positioning your elements
http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
You would need some kind of SwingWorker or Timer running to update the gui layout. Make sure you actually make the GUI changes on the EventThread, which you can do by using SwingUtilities.invokeLater();
As alternatives to a null layout, consider letting the layout work for you using
one of the marquee effects discussed here, or
shuffling labels in a suitable layout, shown here and here.