I'm creating a program for table reservations for practice. I came across the next problem: When I click on button "Create new table", a Button should be added on centerPanel, but it does not appear there.
Here's the code
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
public class CreateNewFloorV2 extends JFrame implements ActionListener{
JFrame frame=new JFrame("Create new table");
BorderLayout borderLayout=new BorderLayout();
JPanel centerPanel=new JPanel();
SpringLayout centerPanelLayout=new SpringLayout();
JPanel bottomPanel=new JPanel();
GridLayout bottomPanelLayout=new GridLayout(1,2);
JButton btn1=new JButton("Create new table");
JButton btn2=new JButton("Delete table");
//Constructor
public CreateNewFloorV2() {
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
//Create layout
frame.setLayout(borderLayout);
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
centerPanel.setLayout(centerPanelLayout);
frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
bottomPanel.setLayout(bottomPanelLayout);
bottomPanel.add(btn1);
bottomPanel.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//ActionListener
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
JButton newTable=new JButton("Table X");
centerPanel.add(newTable);
}
}
public static void main(String[] args) {
CreateNewFloorV2 newFloor=new CreateNewFloorV2();
}
}
I've tried putting
JButton newTable=new JButton("Table X");
centerPanel.add(newTable);
into constructor CreateNewFloorV2() and then it does appear. But I don't know why it doesn't appear when I click on button btn1 and how should I fix it?
Simply adding the component won't suffice - You have to tell the windowing toolkit to redraw the frame. Try using something like this:
if(e.getSource()==btn1) {
JButton newTable=new JButton("Table X");
centerPanel.add(newTable);
centerPanel.invalidate();
centerPanel.repaint();
}
Also see https://www.oracle.com/technetwork/java/painting-140037.html
Related
I have two JTextFields with text and a MouseListener:
JTextField tf1 = new JTextField("Input your text here: ");
JTextField tf2 = new JTextField("Input your pattern here: ");
tf1.addMouseListener(mm);
tf2.addMouseListener(mm);
I want to erase text in the TextField that I clicked using MouseListener:
MouseListener mm = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
tf1.setText("");
//tf2.setText("");
}
};
Nevertheless I can only manage to erase both or only one. I could add one more MouseListener, but I am curious if it is possible to create such MouseListener that erases text depending on the TextField I clicked.
Try this, it will erase based on mouse click the TextField(s).
import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
public class JFrameTest{
public static void main(String[] args){
JFrame frame= new JFrame();
frame.setTitle("JFrame");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constr = new GridBagConstraints();
constr.gridx=0;
constr.gridy=0;
JTextField txt1 = new JTextField(20);
txt1.setText("abc");
JTextField txt2= new JTextField(20);
txt2.setText("efg");
constr.gridx=1;
panel.add(txt1, constr);
constr.gridy=1;
panel.add(txt2, constr);
MouseListener mm = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
//System.out.println(e);
//System.out.println(e.getSource());
//be sure to have the component in case that listener is added on more objs
if(e.getSource() instanceof JTextField)
{
//set whatever you wanted on each TF individualy
((JTextField)e.getSource()).setText("");
}
}
};
//add the same listener on both but process on each as click
txt1.addMouseListener(mm);
txt2.addMouseListener(mm);
mainPanel.add(panel);
frame.add(mainPanel);
frame.pack();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Hi i'm new to Java and kinda lost in the codes i tried to write. It compiles with no error but everything i added on panel don't show up on the frame
Here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Mainframe extends JFrame {
private AddingWindow addingWindow = new AddingWindow(); //Passing AddingWindow Class to the Main Class as statement
private JFrame addingWindowFrame = new JFrame(); //This is the frame i wanted to add the JPanel with its labels and buttons
public Mainframe() {
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1 ,1));
JButton addingBtn = new JButton("Add");
controlPanel.add(addingBtn);
//Add controlPanel to the mainframe
setLayout(new BorderLayout());
add(controlPanel, BorderLayout.WEST);
//Set showAddingPanel button event
addingBtn.addActionListener (new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
addingWindowFrame.setVisible(true);
}
});
addingWindowFrame.pack();
addingWindowFrame.setTitle("Title);
addingWindowFrame.setSize(600, 400);
addingWindowFrame.setResizable(false);
addingWindowFrame.setLocationRelativeTo(null);
addingWindowFrame.getContentPane().add(addingWindow); //Here i'm adding JPanel Class to the Frame
}
//Main method
public static void main(String[] args) {
JFrame mainFrame = new Mainframe();
mainFrame.setTitle("\"Mainframe\"");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
mainFrame.setMinimumSize(new Dimension(800, 600));
}
}
This is the other panel Class file i wanted to show up on the AddingWindowFrame
import java.awt.*;
import javax.swing.*;
public class AddingWindow extends JPanel {
AddingWindow() {
JPanel addingPanel = new JPanel();
addingPanel.setLayout(new GridLayout(2, 2));
JLabel fullNameLbl = new JLabel("Name");
JTextField fullNameTextField = new JTextField(25);
JButton addBtn = new JButton("add");
JButton cancelBtn = new JButton("cancel");
//Adding buttons, label and textfield to addingPanel
addingPanel.add(fullNameLbl);
addingPanel.add(fullNameTextField);
addingPanel.add(addBtn);
addingPanel.add(cancelBtn);
}
}
I think you want to display
What you did is you added all the things to your addingPanel but you forgot to add the addingPanel itself.
import java.awt.*;
import javax.swing.*;
public class AddingWindow extends JPanel {
AddingWindow() {
JPanel addingPanel = new JPanel();
addingPanel.setLayout(new GridLayout(2, 2));
JLabel fullNameLbl = new JLabel("Name");
JTextField fullNameTextField = new JTextField(25);
JButton addBtn = new JButton("add");
JButton cancelBtn = new JButton("cancel");
//Adding buttons, label and textfield to addingPanel
addingPanel.add(fullNameLbl);
addingPanel.add(fullNameTextField);
addingPanel.add(addBtn);
addingPanel.add(cancelBtn);
add(addingPanel);
}
}
You have two frames
MainFrame
AddingWindowFrame -> contains AddingPanel
And when you click on the button you are just displaying the AddingWindowFrame(I guess it should displayed somewhere in the background). Instead you need to add the AddingPanel directly to the currentFrame.
Mainframe.this.getContentPane().add(addingWindow);
But you should check how to use LayoutManagers
the code is used to create an application which shows picture and if your anwser is correct your supposed to see the next picture but the pictureCount does not go up. all of the variables ar declared after the main class and i created an Actionlistener to check if the awnser is correct.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {new Main().test();}
public int pictureCount = 1;
JFrame frame = new JFrame();
JButton button1 = new JButton("Submit");
JTextField text = new JTextField();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JLabel label = new JLabel(new ImageIcon("C:\\Users\\Admin\\Desktop\\practicum 3\\" + pictureCount + ".jpg"));
void test(){
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(text.getText().equals("5")){
pictureCount++;
new Main().test();
}
}
});
panel1.add(button1);
panel2.add(text);
panel3.add(label);
text.setPreferredSize(new Dimension(100,50));
panel1.setPreferredSize(new Dimension(1000, 200));
panel2.setPreferredSize(new Dimension(1000, 100));
panel3.setPreferredSize(new Dimension(1000, 450));
frame.getContentPane().add(BorderLayout.SOUTH, panel1);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.NORTH, panel3);
frame.setSize(1000,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Operation Screen");
frame.setLocationRelativeTo(null);
}
}
You need to read in all the pictures as ImageIcons into an array or ArrayList, say called imageIconArray and then display imageIconArray[0] in your JLabel when you start.
When the button is pressed, increment pictureCount, and then reset the JLabel's icon via its setIcon(...) method:
// in the ActionListener code:
pictureCount++;
label.setIcon(imageIconArray[pictureCount];
Whatever you do, don't create a new Main object, despite what others might say. Why create a new GUI when all you need to do is swap displayed images?
I am creating a GUI using Netbeans and my question is "how to remove the jpanel"...the hierarchical order as it is present Jframe->Jscrollpane->Jpanel->Jbutton. By clicking on the Jbutton I want to remove all components of the jpanel including the Jbutton(on which I am clicking) and the panel itself. Please do help.Urgent.THanks in advance
-Sayantan
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
final JPanel jp;
JTextField tf,of1,of2,xf,yf;
int i=1;
int m=(int)sp3.getValue();
JButton jb;
jp=new JPanel();
//jp.setLayout(new GridBagLayout());
DesignGridLayout layout = new DesignGridLayout(jp);
jsp2.getViewport().add(jp);
while(i<=m){
tf=new JTextField(5);
of1=new JTextField(5);
of2=new JTextField(5);
layout.row().grid(new JLabel("Type:")).indent(9).add(tf).grid(new JLabel("Length:")).indent().add(of1).grid(new JLabel("Breadth:")).indent().add(of2).empty();
fields1.add(tf);
fields1.add(of1);
fields1.add(of2);
xf=new JTextField(5);
yf=new JTextField(5);
layout.row().grid(new JLabel("X-axis:")).indent(9).add(xf).grid(new JLabel("Y-axis:")).indent().add(yf).empty(2);
fields1.add(xf);
fields1.add(yf);
i++;
}
jb=new JButton("Submit");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
for(JTextField field: fields1){
String s=field.getText();
lbr.setText(lbr.getText()+s);
}
lb3.setVisible(false);
sp3.setVisible(false);
b4.setVisible(false);
//jp.setVisible(false);
//jp.removeAll();
// jp.revalidate();
//jp.repaint();
// jsp2.remove(jp);
//jsp2.revalidate();
//jsp2.repaint();
}
});
layout.emptyRow();
layout.row().right().add(jb);
jp.revalidate();
jp.repaint();
// jsp2.revalidate();
//jsp2.repaint();
}
Note: I am using DesignGridLayout package.
This works:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScratchSpace {
public static void main(String[] args) {
final JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(Color.YELLOW);
panel.add(new JButton(new AbstractAction("Kill me") {
#Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.revalidate();
panel.repaint();
}
}));
final JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I want to remove all components of the jpanel including the Jbutton(on which I am clicking) and the panel itself.
Then you need code something like:
JButton button = (JButtton)event.getSource();
JPanel grandparent = button.getParent().getParent();
grandparent.removeAll();
grandparent.revalidate();
grandparent.repaint();
Although I question any code where I see a removeAll(). You might look into using a Card Layout.
i made this code to navigate trough panel1 and panel2
with buttons.
(button1 and button2) but when i run my code the frame stays empty.
Can somebody explain me what i'm doing wrong and how i can accomplish
toggling between panel1 and panel2 in this way? Starting with panel1 first
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class togglepanel {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
JButton button1 = new JButton("previous frame!");
JButton button2 = new JButton("next frame");
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
frame.setSize(600, 400);
frame.add(panel1);
frame.add(panel2);
panel1.add(button2);
panel1.setVisible(true);
panel2.add(button1);
panel2.setVisible(false);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel1.setVisible(true);
panel2.setVisible(false);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel1.setVisible(false);
panel2.setVisible(true);
}
});
}
}
thanks in advance
Use a layout manager.
frame.setLayout(new FlowLayout());
Another useful way to do this, and I think better is to use a CardLayout and to add both JPanels to a container that uses this CardLayout. You can then easily swap views by calling the CardLayout methods.
e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TogglePanel {
public static void main(String[] args) {
final CardLayout cardlayout = new CardLayout();
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane();
contentPane.setLayout(cardlayout);
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
JButton button1 = new JButton("previous frame!");
JButton button2 = new JButton("next frame");
contentPane.setPreferredSize(new Dimension(600, 400));
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
panel1.add(button2);
panel2.add(button1);
ActionListener btnListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardlayout.next(contentPane);
}
};
button1.addActionListener(btnListener);
button2.addActionListener(btnListener);
}
}