JPanel subclass won't appear in JFrame - jframe

I have this class PokerPanel which extends JPanel, whatever I put in the JPanel subclass will not appear in the JFrame when it is set as the Content Pane. Below is the code
public class PokerPanel extends JPanel {
private int x = 1000;
private int y = 1000;
public PokerPanel(){
this.setPreferredSize(new Dimension(x, y));
this.setBackground(Color.BLACK);
}
Then in my main class
public static void main(String[] args) {
PokerPanel pp = new PokerPanel();
pp.setOpaque(true);
JFrame frame = new JFrame();
frame.setSize(pp.getX(), pp.getY());
frame.setContentPane(pp);
frame.setVisible(true);
}
This does not show the black background as intended (or anything else i put in there for that matter JLabels etc), however if I do it as shown below then it does work.
public static void main(String[] args) {
PokerPanel pp = new PokerPanel(rg);
pp.setOpaque(true);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
JFrame frame = new JFrame();
frame.setSize(pp.getX(), pp.getY());
frame.setContentPane(panel);
frame.setVisible(true);
}
I can't seem to figure out why it would not show the JPanel when it is a subclass of JPanel any help would be appreciated

Related

Read JavaFx Application

I created a small javaFx Program. The Program just displays random points. Now I want to get and read the generated Stage from a different class and read the co-ordinates. Is it possible ?
I tried creating a class variable to get stage but its null always.
I added a class variable and assigned the prepared Stage object to the variable. I am then trying to get the Stage object from the variable.
public class DCGUI extends Application {
private Stage primaryStage1;
public static void main(String[] args) {
launch(args);
Platform.exit();
}
#Override
public void start(Stage primaryStage) throws Exception {
Random r = new Random(64);
// List<Integer> points = r.ints(1000, 0, 400).boxed().collect(Collectors.toList());
List<Node> cList = new ArrayList<>();
Line line = null;
Circle c = null;
for (int i = 0; i < 50001; i++) {
//System.out.println(r.nextInt());
c = new Circle(r.nextInt(400), r.nextInt(400), 0.0125);
c.setStroke(Color.RED);
c.setId(i+"");
cList.add(c);
}
Group group = new Group(cList);
Scene scene = new Scene(group, 400, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Dynamic Connectivity");
primaryStage1 = primaryStage;
primaryStage.show();
}
Let's assume the following code example. By calling the getMainStage() method you can have access to your Stage object.
Of course, the main.fxml file is binded with the MainController class
Main.java
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader("path/to/main.fxml").toFile().toURI().toURL());
MainController.setMainStage(stage); <---- !
Parent root = loader.load();
stage.setScene(new Scene(root, 1400, 850));
stage.show();
}
public static void main(String[] args) {
launch(args);
}}
MainController.java
public class MainController{
// fxml view elements...
private static Stage mainStage;
//...
public static Stage getMainStage() {
return mainStage;
}
}

How to position controls binding there position into the stage's size using the controller class in JavaFx?

I found some thing similar in this link
How to call functions on the stage in JavaFX's controller file
and here is what I found in one of the answers
StageTrackingSample.java
public class StageTrackingSample extends Application {
#Override public void start(final Stage stage) throws Exception {
final FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"stagetracking.fxml"
)
);
final Parent root = (Parent) loader.load();
final StageTrackingController controller = loader.getController();
controller.initData(stage);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
StageTrackingController.java
public class StageTrackingController {
#FXML private Label stageX;
public void initialize() {}
public void initData(final Stage stage) {
stageX.textProperty().bind(
Bindings.format(
"(%1$.2f, %2$.2f)",
stage.xProperty(),
stage.yProperty()
)
);
}
}
I wanted to position the progressIndicator in the middle of the window, so I tried this in my controller class
Controller.java
public void initInterface(Stage stage) {
progressIndicator.layoutXProperty().bind(stage.widthProperty().divide(2));
progressIndicator.layoutYProperty().bind(stage.heightProperty().divide(2));
}
and this in Main.java
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
final FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
final Parent root = loader.load();
final Controller controller = loader.getController();
final Scene scene = new Scene(root);
controller.initInterface(primaryStage);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
it doesn't work even when I tried passing the scene or the anchorpane(which is defined in the fxml file) as a parameter into initInterface method, it seems that it has problem with binding progressIndicator properties
by using the layoutXProperty and layoutYProperty and binding them to the Stage's width and height you must be trying to put it in the lower right hand corner of the stage. You can achieve this much easier, and JavaFX insists you do so, by using layouts in your scene and making the scene fill the entire area in question.
"From the Javadocs for the layoutX property:
If the node is managed and has a Region as its parent, then the layout region will set layoutX according to its own layout policy. If the node is unmanaged or parented by a Group, then the application may set layoutX directly to position it.
What this means is that the LayoutX/Y properties are controlled by the parent (and so it should be able to 'set' them). However, when you bind them they cannot be set anymore resulting in " A bound value cannot be set" exception."
Here is a good tutorial on regions and how to get things to layout in SceneBuilder as you please. If you're not using SceneBuilder I recommend it.
https://www.youtube.com/watch?v=zvgWgpGZVKc&list=PL6gx4Cwl9DGBzfXLWLSYVy8EbTdpGbUIG&index=35

JFrame and JPanel problems with calling between

package pozivanjeProzora;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
SidePanel panel ;
MainWindow instanca;
public MainPanel()
{
super();
JButton button = new JButton("Pozdrav iz main panela");
add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
instanca.add(panel);
}
});
}
}
So, I extend JFrame in my MainWindow and I created instance of it. I am in MainPanel class which is called in MainWindow right now. But if I come in MainPanel and if I click on this button I want to my MainWindow(which extends JFrame) to add panel from SidePanel class(which has one button and extends JPanel also).
But if I click on button it shows me only NullPointerException. Where is my foult? MainWindow only has couple of lines about location of window and visible and I added MainPanel there as a first panel when I run my program. But, when I call another panel from MainPanel and when I try to add that panel with instance of MainWindow it shows error.
Your variable instanca is never initialised. Perhaps pass it in the constructor.
i.e.
public MainPanel(MainWindow mainWindow)
{
instanca = mainWindow;

Prompt Text for javaFX ChoiceBox

I want to add "Please Select" as the promptText for javaFX ChoiceBox component.Can any one please help me.
(Note:- This is not the default selected value. It is the value that display before get select any thing)
Thanks
You might want to use ComboBox instead. It is similar to ChoiceBox but has a promptText property.
public class Main extends Application {
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void start(Stage primaryStage) {
VBox vbox = new VBox();
ComboBox combo = new ComboBox();
combo.getItems().addAll("Item 1", "Item 2");
combo.setPromptText("Please Select");
vbox.getChildren().add(combo);
primaryStage.setScene(new Scene(vbox));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

weird JFrame problems

I am trying to get a box to move in a JFrame by repainting it, but for some reason i cannot figure out it will not work. I know its probably something really stupid but here are my classes: (also sorry for the formatting its a pain in the ass...) It gives me an error on the frame.add(square) line.
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class Runner extends JFrame{
final static int FRAME_WIDTH = 1000;
final static int FRAME_HEIGHT = 600;
final static int BOX_WIDTH = 50;
final static int BOX_HEIGHT = 50;
public static void main (String[] args){
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Animation");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setVisible(true);
Component square = new Component();
Dimensions.setBoxDimensions(BOX_WIDTH, BOX_HEIGHT);
frame.add(square);
frame.setVisible(true);
for (int i = 0; i < 100; i++){
Dimensions.setPosition(i,i);
square.repaint();
frame.setVisible(true);
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Component extends JFrame{
public void paintComponent (Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.fillRect(Dimensions.xPos, Dimensions.yPos, Dimensions.boxWidth, Dimensions.boxHeight);
}
}
public class Dimensions {
public static int boxHeight = 50;
public static int boxWidth = 50;
public static int xPos = 0;
public static int yPos = 0;
public static void setBoxDimensions(int width, int height){
boxHeight = height;
boxWidth = width;
}
public static void setPosition(int x, int y){
xPos = x;
yPos = y;
}
}
Since your component class is a JFrame you cannot add teh JFrame to a JFrame. You may want to add a JPanel to the JFrame and paint on the JPanel instead.
Or you may be intending to extend java.awt.Component instead?
import javax.swing.JPanel;
public class MyComponent extends JPanel{
public void paintComponent (Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.fillRect(Dimensions.xPos, Dimensions.yPos, Dimensions.boxWidth, Dimensions.boxHeight);
}
}
And when you want a Component you can do this:
JPanel square = new MyComponent();
It's better to override the "paint" method in your JFrame. It's much easier to do.
#override
public void paint(Graphics g){
super();
/*
put the code to draw your shape of shapes here
*/
}
You can call repaint method of your JFrame whenever you want to update your JFrame.