I am a newbie to SWING.
I have written a code for frame having a button when clicked a new frame opens. But i don't know whats wrong with the code that the new frame/window is not containing the button i have added in code.
setTitle("Frame1");
setSize(250, 250);
setDefaultCloseOperation(1);
setVisible(true);
JPanel panel1= new JPanel();
getContentPane().add(panel1);
JButton button1= new JButton();
button1.setText("Click to open new window");
button1.setBounds(20, 15, 14, 18);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JFrame newFrame= new JFrame();
newFrame.setBounds(150, 150, 150, 150);
newFrame.setDefaultCloseOperation(1);
newFrame.setTitle("New Frame");
newFrame.setVisible(true);
JPanel panel2= new JPanel();
getContentPane().add(panel2);
JButton button2= new JButton();
button2.setText("QUIT");
button2.setBounds(10, 5, 4, 8);
panel2.add(button2);
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
}
});
panel1.add(button1);}
Please help!
You add JButton button1 to JPanel panel1 but you don't add the JPanel pane1 to the JFrame itself.
You need to add the JPanel panel1 to the JFrame
add(panel1);
Related
I'm greatly confused on this. What is don't understand is how the stage and table layouts exactly work. All i want are 3 buttons that sends me to another screen. Could someone please write out an example for me to work with? Here is the code I have so far.
public class Menu implements Screen {
private SlingshotSteve game;
private Stage stage;
private TextButton button;
private TextButtonStyle textButtonStyle;
private BitmapFont font;
{
stage = new Stage(new ExtendViewport(800, 840));
Gdx.input.setInputProcessor(stage);
Table table = new Table();
table.setFillParent(true);
table.center().center();
stage.addActor(table);
font = new BitmapFont();
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
button = new TextButton("This is a button!!!", textButtonStyle);
stage.addActor(button);
}
// View Port Camera
private Viewport viewport;
PerspectiveCamera camera;
public Menu(SlingshotSteve gam) {
this.game = gam;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.end();
if (Gdx.input.isTouched()) {
game.setScreen((Screen) new GameScreen(game));
dispose();
}
}
#Override
public void resize(int width, int height) {
// View Port Camera
viewport.update(width, height);
stage.getViewport().update(width, height, true);
}
#Override
public void show() {
// Viewport Camera
camera = new PerspectiveCamera();
viewport = new FitViewport(800, 480, camera);
}
#Override
public void dispose() {
stage.dispose();
}
}
Don't add the button to the Stage. Instead add it to the Table you have created.
TextButton button1 = new TextButton("This is a button!!!", textButtonStyle);
TextButton button2 = new TextButton("This is a button!!!", textButtonStyle);
TextButton button3 = new TextButton("This is a button!!!", textButtonStyle);
table.add(button1);
table.row();
table.add(button2);
table.row();
table.add(button3);
table.row();
I have a form stored in both the inputWidget and the outputWidget. The buttons addInput and addOutput will show two different forms in the secondaryInOutPanel.
However there is a significant delay when moving between the form by clicking the buttons. In fact it changes when I attempting to click on the form. And there is still some visible drawings from the pervious form.
I tried using SwingUtilities but that caused the delay to be worst.
secondaryInOutPanel = new JPanel(new BorderLayout());
secondaryInOutPanel.setMinimumSize(new Dimension(200,400));
JPanel btnPanel = new JPanel();
outinPanel.add(btnPanel, BorderLayout.NORTH);
JButton addInput = new JButton("Add Input");
btnPanel.add(addInput);
addInput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
secondaryInOutPanel.removeAll();
secondaryInOutPanel.add(inputWidget, BorderLayout.NORTH);
JButton addBtn = new JButton("Save Input");
secondaryInOutPanel.add(addBtn, BorderLayout.SOUTH);
addBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
});
JButton addOutput = new JButton("Add Output");
btnPanel.add(addOutput);
addOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
secondaryInOutPanel.removeAll();
secondaryInOutPanel.add(outputWidget, BorderLayout.NORTH);
JButton addBtn = new JButton("Save Output");
secondaryInOutPanel.add(addBtn, BorderLayout.SOUTH);
addBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
});
A better design is to use a Card Layout to hold your input and output panels. Then you can just swap panels as required. The CardLayout will then manage the revalidating and repainting of the panel for you.
You need to make a call to revalidate() and or repaint() on the secondaryInOutPanel after you make changes.
Hello I am trying to solve the following problem: Write a program that prompts the user to enter the x- and y-positions of a center point and a radius, using text fields. When the user clicks a "Draw" button, draw a circle with that center and radius in a component. I do not see what is wrong in my code but something is because it doesnt seem like repaint() is invoking paintComponent() as message will change to TESTING 1 but not TESTING 2 and no drawing is made.
My Code:
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class q3{
public static class cgPanel extends JPanel{
private static double x;
private static double y;
private static double r;
private static JTextField xField;
private static JTextField yField;
private static JTextField rField;
private static JButton draw;
private static JLabel message;
//This is all just Layout work.
public cgPanel(){
setLayout(new BorderLayout());
JPanel drawPanel = new JPanel();
drawPanel.setBackground(Color.WHITE);
add(drawPanel, BorderLayout.CENTER);
message = new JLabel("");
JPanel sub1ForSub1 = new JPanel();
sub1ForSub1.add(message);
JLabel coordinates = new JLabel("Coordinates:");
JPanel sub2ForSub1 = new JPanel();
sub2ForSub1.add(coordinates);
JPanel subPanel1 = new JPanel();
subPanel1.setLayout(new GridLayout(2, 1));
subPanel1.add(sub1ForSub1);
subPanel1.add(sub2ForSub1);
JLabel xLabel = new JLabel("x:");
xField = new JTextField(4);
JLabel yLabel = new JLabel(" y:");
yField = new JTextField(4);
JLabel rLabel = new JLabel(" Radius:");
rField = new JTextField(4);
JPanel subPanel2 = new JPanel();
subPanel2.add(xLabel);
subPanel2.add(xField);
subPanel2.add(yLabel);
subPanel2.add(yField);
subPanel2.add(rLabel);
subPanel2.add(rField);
draw = new JButton("Draw");
ActionListener bL = new ButtonListener();
draw.addActionListener(bL);
JPanel subPanel3 = new JPanel();
subPanel3.add(draw);
JPanel Panel = new JPanel();
Panel.setLayout(new BorderLayout());
Panel.add(subPanel1, BorderLayout.NORTH);
Panel.add(subPanel2, BorderLayout.CENTER);
Panel.add(subPanel3, BorderLayout.SOUTH);
add(Panel, BorderLayout.SOUTH);
setVisible(true);
}
static class ButtonListener extends JComponent implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
String xString = xField.getText();
String yString = yField.getText();
String rString = rField.getText();
message.setText("TESTING 1");
x = Double.parseDouble(xString);
y = Double.parseDouble(yString);
r = Double.parseDouble(rString);
repaint();
}
catch (NumberFormatException exception){
message.setText("Please enter a number.");
}
}
//This is where I cant seem to get the code in paintComponent to run when the Draw button is pressed.
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double(x - r, y - r, r*2, r*2);
g2.draw(circle);
message.setText("TESTING 2");
}
}
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(800, 800);
frame.setTitle("Circle Generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cgPanel panel = new cgPanel();
frame.add(panel);
frame.setVisible(true);
}
}
So, a couple of things.
Your problem stems from that fact that your ButtonListener is extending a JComponent, so the repaint() method is calling the one for the ButtonListener (which really isn't a JComponent).
And the paintComponent method is also for the the ButtonListener.
Instead, you want your button listener to have access to your cgPanel, so it can tell IT to repaint. And your paintComponent needs to be moved to the cgPanel, but even then you probably don't want it there since you have a bunch of other components on cgPanel.
It's not clear from your code where you really want the circle to be drawn.
You should probably create a CirclePanel that extend JPanel, and overrides paintComponent to draw your circles, and then add that to your cgPanel. Then make your ButtonListener tell the CirclPanel instance to repaint.
I want to add a button to JTabbedPane's title bar (similar to the 'open new tab' ('+') button in Firefox)
I have tried to add to the glass pane of JTabbedPane's container. but since my tabbedpane contains within a JPanel seems it doesn't work for me.
Any suggestion will be a great help for me.
Thank you.
Instead of adding a button I have tried it in a different way and worked for me... I have added a JLabel (with '+') as a hidden tab and when user tries to select that tab i'll be adding a new tab.
public class AddTabButtonDemo extends JFrame{
private JTabbedPane tabbedPane = new JTabbedPane();
public AddTabButtonDemo() {
JLabel tab1Label = new JLabel("tab1");
JPanel tab1 = new JPanel();
tab1.add(tab1Label);
tabbedPane.addTab("tab1", tab1);
tabbedPane.addTab("+", new JLabel());
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (tabbedPane.getSelectedComponent() instanceof JLabel) {
int count = tabbedPane.getTabCount();
JLabel newTabLabel = new JLabel("tab" + count);
JPanel newTab = new JPanel();
newTab.add(newTabLabel);
tabbedPane.add(newTab, count - 1);
tabbedPane.setTitleAt(count - 1, "tab" + count);
tabbedPane.setSelectedComponent(newTab);
}
}
});
this.add(tabbedPane, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setMinimumSize(new Dimension(300, 300));
this.setVisible(true);
}
public static void main(String[] args) {
new AddTabButtonDemo();
}
}
I'm Working on time schedule booking application , when I run the project it shows component(total frame) , however i want that when the button is pressed to reload ,component should be spitted.(two frames with one below other by spilt) ???
Based on your description I think you need to either add splitpane in frame on click of a button or you already have a splitpane in frame and want to add panel in it on click of button.
For first option you can do something like this:
final JFrame frame = new JFrame("Split test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel jPanel2 = new JPanel();
JLabel jLabel = new JLabel("I am added by click on button");
jPanel2.add(jLabel);
final JPanel jPanel = new JPanel();
JButton button = new JButton("Click me to add pane in split");
jPanel.add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
pane.add(jPanel);
pane.add(jPanel2);
pane.setDividerLocation(frame.getHeight()/2); // set Divider location.
frame.remove(jPanel);
frame.add(pane);
frame.validate();
}
});
frame.add(jPanel);
frame.setVisible(true);
If you are stuck in later one then try this:
final JFrame frame = new JFrame("Split test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
frame.add(pane);
pane.setEnabled(false); // stop user from clicking on divider of split pane.
final JPanel jPanel2 = new JPanel();
JLabel jLabel = new JLabel("I am added by click on button");
jPanel2.add(jLabel);
final JPanel jPanel = new JPanel();
JButton button = new JButton("Click me to add pane in split");
jPanel.add(button);
pane.add(jPanel);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
pane.add(jPanel2);
pane.setDividerLocation(frame.getHeight()/2); // set Divider location.
pane.setEnabled(true); // let user change divider location.
}
});
frame.setVisible(true);