I'm doing a little connect4 game and I've started on the GUI. I made the piece class which creates the button for the piece and sets its color. But when I test it out in the GUI, the only thing that appears is the text, if there was no text(which was only used to check) the button is invisible
code for creating button
piece = new Button("testing");
piece.setShape(new Circle());
if (colour == 1) {
piece.setStyle("fx-background-color: #DC143C");
} else {
piece.setStyle("fx-background-color: #0000FF");
}
code for adding button
int col = piece.getCol();
Button button = piece.getPiece();
root.addColumn(col, button);
I have no skill at all with CSS and very little in JavaFX so I don't understand why it's not working
Related
Am new to javafx. I wanted to write a simple javafx program that when clicking a button for the first time, the color of a rectangle should change to something else, e.g green, and when clicking it for the second time, the color should change back to the original color.
But the button is only allowing a single click only?
Help if there is any solution on this.
Here is a code.
You can try using simple boolean value to check if color has been changed before:
boolean flag = false;
#FXML
private void onButtonClick(){
if (flag){
//change to e.g. red
} else {
//change to e.g. green
}
flag = !flag;
}
This code is simple and if you want only to change between two colors it will be enough :)
I am having issues with my java application and I can't find a suitable reply.
In summary a right click triggered default pop up menu changes my chart's background color behind the pop up.
You can find images below. I am happy to keep the default popup without the "buggy" behaviour or develop my own if required.
A click of a button starts a stream of data and adds a chart to a JInternalFrame component:
If I right click on the image a default pop up comes up:
If I then click away the rectangle area covered by the popup will overlay the chart like this:
TimeseriesMonitorModel model = new DefaultTweetMonitorModel();
jif.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try {
jif.setContentPane(new TweetSeriesChartPane(model, TweetMonitor.keywords, tkc));
jif.setSize(jif.getWidth(), jif.getHeight());
} catch (InterruptedException ex) {
Logger.getLogger(TweetMonitor.class.getName()).log(Level.SEVERE, null, ex);
}
jif.setVisible(true);
where jif is the Jinternalframe and
public TweetSeriesChartPane(TimeseriesMonitorModel model, String[] seriesNames, TweetKeywordCount tkc) throws InterruptedException {
this.seriesNames = seriesNames;
this.tkc = tkc;
this.model = model;
XYChartTimeseries myRealTimeChart = new XYChartTimeseries();
chart = myRealTimeChart.getChartWithTitle();
List[] tweetData = model.getFrequencyCount(new AtomicIntegerArray(seriesNames.length)); // we are starting from 0
int i = 0;
for (String keyword : seriesNames) {
List<Integer> yData = (List<Integer>) tweetData[1].get(i);
chart.addSeries(keyword, tweetData[0], yData); // adding first value
i++;
}
setLayout(new BorderLayout());
XChartPanel<XYChart> chartPane = new XChartPanel<>(chart);
add(chartPane);
UpdateWorker worker = new UpdateWorker(this, seriesNames, this.tkc);
worker.execute();
}
I've managed to temporary solve the above.
I've checked specifically this line of code
XChartPanel<XYChart> chartPane = new XChartPanel<>(chart);
which extends Chart and Jpanel. The code is from the knowm-chart dependency https://github.com/knowm/XChart/blob/develop/xchart/src/main/java/org/knowm/xchart/XChartPanel.java)
Apparently it adds a listener and the customized PopUpMenu. After reviewing it, it looks like it didn't do any repainting when the mouse was clicked outside the PopUpMenu area.
So I created a new class and tried to customise it. However, the repaint was flickering the screen and I couldn't get it to work only in the PopUpMenu area.
I ended up disabling the .addMouseListener call so now I don't get any popUpMenu. I sad compromise, but oh well.
BTW:
Thanks to both, regardless of the last unneeded comment which didn't add any value.
I did read the link and I though I provided enough information.
In any case, posting to code helped me troubleshoot it
So, I dont know if I am missing something but any way I work it or look into it I am unable to click on any buttons on the screens.
I made a dialog box with a button essentially as shown in the sample projects but when ever I try to click the button nothing happens at all.
I tried firing the onClick programmatically and it works fine, I also tested that the mouse inputs were being registered okay and they were.
I am at a total loss for any reason why this wouldnt work.
My class code is below:
public class Interactable : UICanvas
{
public void interact()
{
var skin = Skin.createDefaultSkin();
var table = stage.addElement(new Table());
table.center();
table.setFillParent(true);
table.add(talk(this.entity.name, "Stay a while and glisten", "Bye!"));
}
public Dialog talk(string title, string messageText, string closeButtonText)
{
var skin = Skin.createDefaultSkin();
var style = new WindowStyle
{
background = new PrimitiveDrawable(new Color(50, 50, 50)),
//Dims the background
stageBackground = new PrimitiveDrawable(new Color(0, 0, 0, 150))
};
var dialog = new Dialog(title, style);
dialog.getTitleLabel().getStyle().background = new PrimitiveDrawable(new Color(55, 100, 100));
dialog.pad(20, 5, 5, 5);
dialog.addText(messageText);
var exitButton = new TextButton(closeButtonText, skin);
exitButton.onClicked += butt => dialog.hide();
dialog.add(exitButton);
return dialog;
}
}
}
The interact() is called when running up to another entity and pressing "E".
Which causes everything to render properly but I can't click on the button.
Additionally:
When i try to view exitButton co-ordinates theyre always 0 no matter what although the dialog appears in the middle of the window
Monogame version: 3.7
Nez version: 0.9.2
UPDATE:
So it seems like buttons are clickable but their click box is not even nearly aligning with where the buttons are truely rendered.
UPDATE2:
It seems that the issue is that where the button is being rendered and where the actual click box is are not the same.
I have Zoomed in the camera by 2 and the camera also follows my little character around. The dialog will then appear at the X,Y in relation to the current camera view but the actual click box appears at the X,Y in terms of the TiledMap (which is not always on screen).
Not too sure how to work around this.
So! The issue I was having was I was using one renderer for the entire this (The RenderLayerRenderer.) What I have done to fix this is start another renderer (A ScreenSpaceRenderer). This allows me to use it to render UI and its XY variables do not change but are just static to the visual area.
So i ended up with two renders like so:
addRenderer(new RenderLayerRenderer(0,new int[] { (int)RenderLayerIds.First, (int)RenderLayerIds.Second, (int)RenderLayerIds.Third}));
addRenderer(new ScreenSpaceRenderer(1, new int[] { (int)RenderLayerIds.UILayer }));
Using the first for my game rendering and the bottom on just for HUD things!
I'm a very unexperienced programmer, so please bear with me for my lack of knowledge.
I have a program with 168 different buttons, which each count how many times they have been each pressed. After having either pressed or not pressed all of the buttons i need to inactivate and grey-out those which have not been pressed. So far I've used an 3D array to store how many times each button has been pressed, and have made a simple code:
if(a[0][0][0]<1)
{
ImageButton button_a1=(ImageButton) findViewById(R.id.button1a);
button_ca1.setEnabled(false);
button_ca1.setAlpha(6);
}
The only problem is that since each buttonID is different i have to do this 168 separated times. Is there any way to make this into a simple loop that doesn't take up over 1000 lines of code?
The program is written using Eclipse and is used for an Android app.
If all the buttons are in same layout, you can use getChildCount():
LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
v = layout.getChildAt(i);
if (v instanceof ImageButton) {
v.setEnabled(false);
v.setAlpha(6);
}
}
If not, simply create a Set, List or Array with all buttons and iterate over it:
List<ImageButton> buttons = new ArrayList<ImageButton>();
// be sure buttons is visible in the method
// when creating the buttons put them inside the list
Then in your method:
if(a[0][0][0]<1)
{
// disable all buttons
for(ImageButton button : buttons) {
button.setEnabled(false);
button.setAlpha(6);
}
}
I am creating a game of connect-4 using swing in java.
I have an array of 6 buttons which are used to input the move the player wants to make.
When I click a button, the ActionListener does what I want it to do, but also places an image of the most recently pressed button in the upper left of the screen. I say "image" of a button because it cannot be clicked. Here's my code:
public class ButtonPanel extends JPanel implements ActionListener{
ArrayList<JButton> buttonList;
public ButtonPanel(){
//set up the JPanel...
for (int i = 0; i < 7; i++){
buttonList.add(new JButton("" + i));
buttonList.get(i).addActionListener(this);
add(buttonList.get(i));
}
}
public void actionPerformed(ActionEvent e) {
for (JButton b : buttonList){
if(e.getSource() == b){
frame.playerMove = buttonList.indexOf(e.getSource());
return;
}
}
}
}
Here's what happens when I click the button 3
And when I click button 5
Does anybody know what's happening here, or how to fix it?
but also places an image of the most recently pressed button in the upper left of the screen. I say "image" of a button because it cannot be clicked.
Sounds to me like you are doing custom painting.
Make sure you are overriding paintComponent(), not paint();
Make sure the first statement in the paintComponent() method is super.paintComponent(...) so the background of your panel is cleared before the custom painting is done.
If this doesn't help, then post a proper SSCCE that demonstrates the problem.
In the future, make sure you post a SSCCE with all your questions.