Using the same JToolBar on 2 different JFrames - jframe

This example creates the ToolBar and attempts to add the same ToolBar to 2 different JFrames.
I was expecting that both JFrames would have the identical ToolBar, but apparently the ToolBar is being added only to the 2nd JFrame.
If the code for the 2nd JFrame is commented out then the ToolBar gets added to the 1st Frame as expected.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
public class ToolBarSample {
public static void main(final String args[]) {
JFrame frame = new JFrame("JToolBar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
JButton button = new JButton("button");
toolbar.add(button);
toolbar.addSeparator();
toolbar.add(new JButton("button 2"));
toolbar.add(new JButton("button 3"));
toolbar.add(new JButton("button 4"));
Container contentPane = frame.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
contentPane.setPreferredSize(new Dimension(10,10));
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
JFrame frame2 = new JFrame("JToolBar Example 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane2 = frame2.getContentPane();
contentPane2.add(toolbar, BorderLayout.NORTH);
frame2.setSize(500, 500);
frame2.setVisible(true);
}
}
Why is the JToolBar only being added to the 2nd JFrame and not both?

You can add a JToolbar (Component) into only 1 JFrame (Container) at the same time. Because Swing/AWT component's struct is a tree. A tree's node can not have more than 1 parent.
In your case, you can implement a method to create a JToolbar. Then, you create 2 JToolbar and add to 2 JFrame as follows:
class ToolBarSample {
public static void main(final String args[]) {
JFrame frame = new JFrame("JToolBar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.add(createToolbar(), BorderLayout.NORTH);
contentPane.setPreferredSize(new Dimension(10, 10));
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
JFrame frame2 = new JFrame("JToolBar Example 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane2 = frame2.getContentPane();
contentPane2.add(createToolbar(), BorderLayout.NORTH);
frame2.setSize(500, 500);
frame2.setVisible(true);
}
private static JToolBar createToolbar() {
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
JButton button = new JButton("button");
toolbar.add(button);
toolbar.addSeparator();
toolbar.add(new JButton("button 2"));
toolbar.add(new JButton("button 3"));
toolbar.add(new JButton("button 4"));
return toolbar;
}
}

Related

how to remove JPanel correctly

I've checked How can I remove a JPanel from a JFrame? but when I tried to do frame.remove(mainScreen) the whole thing just froze. I also tried mainScreen.setVisible(false); but that showed the balls but they were static (they should move).
// important variables
FlowLayout fL = new FlowLayout();
// create frame
JFrame frame = new JFrame("Ball");
frame.setTitle("GaoMolecules");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setResizable(false);
Rectangle fDim = frame.getBounds(); // frameDimensions
// add panel to frame
JPanel mainScreen = new JPanel();
mainScreen.setLayout(fL);
fL.setAlignment(FlowLayout.TRAILING);
frame.add(mainScreen);
// other panel components
JTextField numBallsField = new JTextField(4);
JLabel numBallsLabel = new JLabel("Enter the number of balls to generate");
JButton startButton = new JButton("START");
mainScreen.add(numBallsLabel);
mainScreen.add(numBallsField);
mainScreen.add(startButton);
// add listener to button
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//System.out.println("Button Working!");
mainScreen.setVisible(false);
frame.add(new DrawManager());
start = true;
}
});
// this needs to be the very LAST
frame.setVisible(true);
Any time you do a remove(), you should call
frame.validate();
frame.repaint();

Multiple RadioButtons Simulator

I'm trying to make a sort of simple soccer simulator. This is the code I created after watching tutorials and i know its pretty bad. All i want to do is add a value to the team, like 1 for the best team and 10 for the worst, and when i click simulate a pop up would show up telling me which team would win given the teams value. But i cant figure out how to do it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class sim extends JPanel {
public sim() {
// JFrame constructor
super(true);
JRadioButton chelsea, arsenal, chelsea2, arsenal2;
this.setLayout(new GridLayout(3,0));
ButtonGroup group = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
// takes image and saves it the the variable
Icon a = new ImageIcon(getClass().getResource("a.PNG"));
Icon c = new ImageIcon(getClass().getResource("c.JPG"));
chelsea = new JRadioButton("Chelsea",c);
chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal = new JRadioButton("Arsenal",a);
arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);
group.add(chelsea);
group.add(arsenal);
JLabel label = new JLabel("");
TitledBorder titled = new TitledBorder("Team 1");
label.setBorder(titled);
chelsea.setBorder(titled);
arsenal.setBorder(titled);
JButton button = new JButton("Simulate");
button.setHorizontalAlignment(JButton.CENTER);
add(button, BorderLayout.CENTER);
chelsea2 = new JRadioButton("Chelsea",c);
chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal2 = new JRadioButton("Arsenal",a);
arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);
group2.add(chelsea2);
group2.add(arsenal2);
JLabel label2 = new JLabel("");
TitledBorder titled2 = new TitledBorder("Team 2");
label2.setBorder(titled2);
chelsea2.setBorder(titled);
arsenal2.setBorder(titled);
add(label);
add(chelsea);
add(arsenal);
add(button);
add(chelsea2);
add(arsenal2);
HandlerClass handler = new HandlerClass();
chelsea.addActionListener(handler);
}
private class HandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, String.format("%s", e.getActionCommand()));
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setContentPane(new sim());
frame.setVisible(true);
}
}
Put the ActionListener into the sim class.
public void actionPerformed(ActionEvent e)
{
JButton clicked = (JButton)e.getSource();
if(clicked == button)
{
JOptionPane.showMessageDialog(null, "this team will win");
}
}
You need to decrees the amount of code to just amount needed you dont need to show us all the raidio

How to replace a scene component with a button click inJavaFX

Am new to JavaFX and changing only a component of a scene on button click while other components of the scene remain unchanged is giving me hard times.
I have a splitpane with 2 divisions. One part containing a HBox with buttons and a VBox on the other. How can I replace the VBox depending on button clicked ? thanks in advance, Below is my sample code:
public class ShoolLibrary extends Application {
BorderPane b_pane;
SplitPane common;
Scene scene;
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("The Library");
b_pane = new BorderPane();
common = commonGround();
b_pane.setCenter(common);
scene = new Scene(b_pane, 700, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
//Main Content
private SplitPane commonGround(){
HBox hb = new HBox(); //Holds Buttons for Action
VBox vb = new VBox(); //This should change depending on button click
Button btn1 = new Button("library profile");
Button btn2 = new Button("Books");
Button btn3 = new Button("Members");
//Button Action
btn1.setOnAction(actionEvent -> /*Replace vb with profile()*/);
btn2.setOnAction(actionEvent -> /*Replace vb with books()*/);
btn2.setOnAction(actionEvent -> /*Replace vb with members()*/));
hb.getChildren().addAll(btn1,btn2,btn3);
SplitPane sp = new SplitPane();
sp.setOrientation(Orientation.HORIZONTAL);
sp.getItems().addAll(hb,vb);
return sp;
}
private VBox profile(){
txt = new Text("Inside library profile");
VBox vbx1 = new VBox();
vbx1.getChildren().add(txt);
return vbx1;
}
private VBox books(){
txt = new Text("Inside books");
VBox vbx1 = new VBox();
vbx1.getChildren().add(txt);
return vbx1;
}
private VBox members(){
txt = new Text("Inside Members");
VBox vbx1 = new VBox();
vbx1.getChildren().add(txt);
return vbx1;
}
public static void main(String[] args) {
launch(args);
}
}
How about using a wrapper pane in which you place your vBoxes?
Pane wrapperPane = new Pane();
sp.getItems().addAll(hb, wrapperPane);
then:
VBox library = profile()
btn1.setOnAction(actionEvent ->
wrapperPane.getChildren().clear();
wrapperPane.getChildren().add( library );
);
VBox books = books()
btn2.setOnAction(actionEvent ->
wrapperPane.getChildren().clear();
wrapperPane.getChildren().add( books );
);
and so on...

How do I add a button and label to my frame?

Ok basically my problem is I don't know how to get my buttons and my labels to appear in the frame that I just created. I tried using frame.add(new JButton("VOTE1")); but that doesn't work and it says it's missing an identifier. Here is my code so far, your help is much appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VotingMachine extends JFrame implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("Voting Machine");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JLabel Candidate1 = new JLabel("My Name");
private JLabel Candidate2 = new JLabel("Jennifer Lawrence");
private JLabel Candidate3 = new JLabel("Cristiano Ronaldo");
private JButton VOTE1 = new JButton("VOTE FOR Bishoy Morcos");
private JButton VOTE2 = new JButton("VOTE FOR Jennifer Lawrence");
private JButton VOTE3 = new JButton("VOTE FOR Cristiano Ronaldo");
int countcandidate1;
int countcandidate2;
int countcandidate3;
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == VOTE1)
countcandidate1++;
else if(o == VOTE2)
countcandidate2++;
else if(o == VOTE3)
countcandidate3++;
}
}
First you need to create a JPanel
JPanel welcomePanel = new JPanel();
Then add the buttons and labels to that panel
welcomePanel.add(Candidate1);
welcomePanel.add(VOTE1);
ETC.... for all of your buttons and labels
Lastly, add that JPanel to your JFrame
frame.add(welcomePanel);
If this answers your question, please mark it as answered so other viewers will be informed.

JFrame with JTabbedPane & JButton

I have a Jframe declared and adda JTabbedPane to it.
There are 4 tabbedPane and each has a table content.
Now, I need to add a refresh button to each tab, how can I do this?
This is how I'm doing:
frmSql = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Events", retrieveData("events"));
tabbedPane.addTab("Completed Events", retrieveData("completed"));
frmSql.add(tabbedPane);
Any suggestions?
static void createAndShowGui() {
JFrame frmSql = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane();
Action refreshEvents = null, refreshCompletedEvents = null;
tabbedPane.addTab("Events", createTab(retrieveData("events"), refreshEvents));
// more tabs
tabbedPane.addTab("Completed Events", createTab(retrieveData("completed"), refreshCompletedEvents));
frmSql.setContentPane(tabbedPane);
}
static JComponent createTab(JComponent content, Action refreshAction) {
JPanel p = new JPanel(new BorderLayout());
p.add(content, BorderLayout.CENTER);
JPanel btns = new JPanel();
BoxLayout layout = new BoxLayout(btns, BoxLayout.LINE_AXIS);
btns.setLayout(layout);
JButton refreshBtn = new JButton(refreshAction);
btns.add(refreshBtn);
btns.add(Box.createHorizontalGlue());
p.add(btns, BorderLayout.PAGE_END);
return p;
}
Of course, if retriveData is time-consuming, it shouldn't be called from EDT