how to add and remove components(JButons , JTextField etc) at runtime in a Swing program (Java ) , without using NetBeans ? which Layout should I use ?
I want the user to enter Username & Password and , when he clicks on Submit button , the new screen with new components(JButtons , JTextField etc) should appear , I am unable to achieve the transition at runtime.
You want to use two different panels to achieve this result. It's not a good idea to use the same panel and remove all of the first components and add all of the second ones.
Make a LoginPanel class which lays out the username and password fields, labels, and submit button. Then, when the submit button is pressed, after the login is authenticated, hide the login panel and display a new panel with the layout you want for the next screen.
This sort of approach makes it much easier to maintain the two panels, and clearly separates their layouts and functionality from each other.
You can center this new frame over the existing panel using this code:
public static void centerFrameOverComponent(JFrame frame, JComponent component) {
Window parent = SwingUtilities.getWindowAncestor(component);
Rectangle frameRect = frame.getBounds();
Rectangle parentRect = parent.getBounds();
int x = (int) (parentRect.getCenterX() - frameRect.getWidth() / 2);
int y = (int) (parentRect.getCenterY() - frameRect.getHeight() / 2);
frame.setLocation(x, y);
}
Another approach is to call setVisible(false) on the specific component when you wish to hide it.
In cases where I have wanted to add/remove an entire sub panel, I have used the following:
panel.remove(subPanel);
panel.revalidate();
panel.repaint();
You want to call the last two methods whenever you add/remove components.
One approach would be to use CardLayout. Your login button handler would check the credentials and use show() to reveal the second pane.
Addendum: For security, consider using JPasswordField for the password; for convenience, consdier setLocationRelativeTo() for positioning the frame.
you could create a new class for exsample MyFrame thath extends JFrame, that rapresetns your new windows, in the constructor of that class you have to add at the contentpanel your all contolr....
in this way you can add a jbutton to your JFrame.
class MyFrame extends JFrame{
private JButton jb= new JButton("hello");
public MyFrame(){
setSize(new Dimension(300,180)); //set the MyFrame size
getContentPane().add(jb); //add your Component at ContentPane
}
}
after the login you could show your new frame in this way:
new MyFrame().setVisible(true);
Related
We have a project for university which is a program to hold handouts and feedback for courseworks done.
What we've thought of is breaking the whole thing down into smaller pieces, for example:
You have a coursework which requires to write a program and a report on results etc.
So the user will create a new coursework by selecting the "code" and "report" options, since that's what is required. And then we need to create the respective tabs in the program so the user can input what is needed.
I have created all necessary forms and windows, It's just I'm not sure how to move on forward.
a) where should I put my code? should I have it on the "create" event?
b) how do I do this whole custom population thing?
Obviously, I'm not asking for the entire thing in code. I'm not even sure what to read and what to search for.
Following are some screenshots of the ui to help explain what I mean.
New project window
How the main window should be after creating a new projet. Notice the various tabs.
A form for report feedback
On your "Create" button click check for the checkbox.isSelected() and use the method below as:
if(reportCheckbox.isSelected()){
addonScreen(new reportFrame(),"Report Submission");
addonScreen(new reportFeedbackFrame(),"Report Feedback");
}
Use a desktop pane as a container...add your tabbed pane to it
public static JTabbedPane tabbedPane = new JTabbedPane();
jDesktopPane1.add(tabbedPane);
Use this method to add tabs to the layout at runtime
public static void addOnScreen(JInternalFrame inFrame, String title) {
//border for the internal frame
javax.swing.plaf.InternalFrameUI ifu = inFrame.getUI();
((javax.swing.plaf.basic.BasicInternalFrameUI) ifu).setNorthPane(null);
Border b1 = new LineBorder(new Color(114, 139, 173), 3, true) {
};
tabbedPane.setBounds(0, 0, jDesktopPane1.getWidth(), jDesktopPane1.getHeight());
inFrame.setLocation(0, 0);
inFrame.setSize(jDesktopPane1.getWidth(), jDesktopPane1.getHeight());
inFrame.setBorder(b1);
JPanel jp = new JPanel();
jp.setLayout(new GridLayout());
jp.setOpaque(true);
jp.add(inFrame);
tabbedPane.addTab(title, jp);
tabbedPane.setSelectedComponent(jp);
inFrame.requestFocusInWindow();
inFrame.setVisible(true);
tabbedPane.setVisible(true);
}
Hello I am fairly new to java programming and I am currently trying to work on a small game with a GUI, Right now I am stuck at an issue where I can only use a normal button with a card layout, unfortunately I want to use my own icon with the button and to do this I need to use a JButton. but for some reason when i use a JButton it does not get switched with the panels. I'm just wondering what I am doing wrong. I am programming as an applet with a IDE called Ready To Program.
{
CardLayout PanelLayout = new CardLayout ();
JPanel AppPanel = new JPanel (); //Main App Panel using cardlayout
JPanel StartPanel = new JPanel (); //Start Menu Panel
JPanel GamePanel = new JPanel (); //Running Game Panel
JButton StartBtn = new JButton ();
public void init ()
{
StartBtn.setIcon(new ImageIcon ("StartIcon.png"));
StartBtn.setBorder(BorderFactory.createEmptyBorder());
StartBtn.setContentAreaFilled(true);
StartPanel.add (StartBtn);
AppPanel.setLayout (PanelLayout);
AppPanel.add (StartPanel, "1");
AppPanel.add (GamePanel, "2");
PanelLayout.show (AppPanel, "1");
setLayout (new BorderLayout ());
add ("Center", AppPanel);
}
public boolean action (Event e, Object o)
{
if (e.target == StartBtn)
{
PanelLayout.show (AppPanel, "2");
}
return true;
}
}
Frist of all variable names should NOT start with an upper case character. I have never seen a tutorial or answer in any forum that uses an upper case character, so don't make up your own conventions. Learn by example.
I can only use a normal button with a card layout, unfortunately I want to use my own icon with the button and to do this I need to use a JButton.
A JButton is a normal button. What other kind of button are you referring to? It doesn't matter whether the button has text or Icon or both, it is still a button.
The button in your code doesn't work because you didn't add and ActionListener to the button.
Read the section from the Swing tutorial on How to Use Button for more information and examples. The tutorial also has a section on How to Write ActionListeners.
Your code has an action(...) method. I don't know if that is generated by the IDE or not, but basically the code in that method would be the code in your ActionListener.
add ("Center", AppPanel);
That is not the proper form of the method to add a component to a panel. First all don't use hardcoded literal strings, use the variables provided by the API:
add (appPanel, BorderLayout.CENTER);
I have a display that just shows a score on a page:
for example:
JIMBOB
Total Score: 22
When a point is scored an event is triggered that updates the score and redraws the new score on the page:
public void showScore(String username, int score)
{
contentPane.remove(layout.getLayoutComponent(BorderLayout.CENTER));
score.setText("<html>" + username + "<br/>Total Hits: " + totalHits + "</html>");
contentPane.add(score, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
Every time a point is scored this function is called, it removes the old score message adds an new one then packs and redraws.
The problem is that the window flickers when drawing the updated frame. And it seems for a split second to be displaying the info unformatted
contentPane is a Container
and frame is a JFrame
Invoking pack() will completely re-render the JFrame, a top-level, heavyweight container owned by the host platform. Instead, update the JComponent used to display the revised score. A complete example is cited here. Excerpted form RCStatus,
public class RCStatus extends JPanel implements Observer {
/** Update the game's status display. */
public void update(Observable model, Object arg) {
// invoke setText() here
}
}
See this answer for more on the observer pattern.
First of all, I would recommend to take a look at how to use JPanels and other GUI components in Swing. You could use a JPanel to be added to the contentPane of your JFrame. Add a desired LayoutManager that is fitting for your need and use it to position a JLabel according to your wishes.
When you call your showScore() method, use setText(String text) on a JLabel instead of the int score that you supply as parameter.
In order to set the text using a String, you need to transform your int score. You can do either of the following two things (but use the first option):
label.setText(String.valueOf(score));
label.setText("" + score);
If you want the score label to appear or disappear, add/ remove it from the JPanel that you added to the JFrame. After adding it, call setText() on the label to display the score. Then, since you reset the text of the label, you should call repaint() on the JPanel. If you choose to add/ remove the JLabel instead of just changing its text you should also call revalidate() on the JPanel.
So all in all the function should look something like this (this is incomplete), assuming totalHits is actually your score.
// declare instance fields before constructor
// both variables are reachable by showScore()
private JPanel scorePanel;
private JLabel scoreLabel;
// in constructor of your class
scorePanel = new JPanel(); // initialize the JPanel
scorePanel.setLayout(new BorderLayout()); // give it a layout
scoreLabel = new JLabel(); // initialize the label
frame.add(scorePanel); // add JPanel to your JFrame
// method obviously outside constructor
public void showScore(String username, int score)
{
// clear the JPanel that contains the JLabel to remove it
scorePanel.removeAll();
scoreLabel.setText("<html>" + username + "<br/>Total Hits: " + String.valueOf(score) + "</html>");
scorePanel.add(label, BorderLayout.CENTER);
scorePanel.revalidate();
scorePanel.repaint();
}
You could also declare and initialize the JPanel and JLabel in the showScore() method, making it more local. But then you would also need to call pack() on the JFrame and clear the previous JPanel from it every time you call the function.
.
I am in the process of making my own java socket game. My game is painting alright to the full screen (where it says "paint graphics here", but im painting to the whole jframe at the moment). I want to add a textbox with a scroll bar for displaying only text, not taking any input and another textbox to take text inputs from the user and then a button to send the text, for chat purposes. But onto my question, how do I even start to lay this out? I understand I need a layout, but can someone help me on this? Here is my code at the moment (this code only sets up painting to the whole screen at the moment, need to divide the screen up now like I have in the picture above):
public class Setup extends JFrame implements Runnable{
JPanel panel;
JFrame window;
public Setup(Starter start, JFrame window){
window.setSize(600,500);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
panel = new Display(start);
this.window = window;
}
public void run(){
window.getContentPane().add(panel);
window.setBackground(Color.BLACK);
window.setVisible(true);
}
}
"new Display(start)" - this extends jpanel, its basically where I paint everything graphics wise.
In addition, I've seen people add in different panels but I cant have them be the same size. Like in the picture, the "paint graphics here" panel is the biggest one, and so on.
The JPanel is actually only a container where you can put different elements in it (even other JPanels). So in your case I would suggest one big JPanel as some sort of main container for your window. That main panel you assign a Layout that suits your needs ( here is an introduction to the layouts).
After you set the layout to your main panel you can add the paint panel and the other JPanels you want (like those with the text in it..).
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel paintPanel = new JPanel();
JPanel textPanel = new JPanel();
mainPanel.add(paintPanel);
mainPanel.add(textPanel);
This is just an example that sorts all sub panels vertically (Y-Axis). So if you want some other stuff at the bottom of your mainPanel (maybe some icons or buttons) that should be organized with another layout (like a horizontal layout), just create again a new JPanel as a container for all the other stuff and set setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS).
As you will find out, the layouts are quite rigid and it may be difficult to find the best layout for your panels. So don't give up, read the introduction (the link above) and look at the pictures – this is how I do it :)
Or you can just use NetBeans to write your program. There you have a pretty easy visual editor (drag and drop) to create all sorts of Windows and Frames. (only understanding the code afterwards is ... tricky sometimes.)
EDIT
Since there are some many people interested in this question, I wanted to provide a complete example of how to layout a JFrame to make it look like OP wants it to.
The class is called MyFrame and extends swings JFrame
public class MyFrame extends javax.swing.JFrame{
// these are the components we need.
private final JSplitPane splitPane; // split the window in top and bottom
private final JPanel topPanel; // container panel for the top
private final JPanel bottomPanel; // container panel for the bottom
private final JScrollPane scrollPane; // makes the text scrollable
private final JTextArea textArea; // the text
private final JPanel inputPanel; // under the text a container for all the input elements
private final JTextField textField; // a textField for the text the user inputs
private final JButton button; // and a "send" button
public MyFrame(){
// first, lets create the containers:
// the splitPane devides the window in two components (here: top and bottom)
// users can then move the devider and decide how much of the top component
// and how much of the bottom component they want to see.
splitPane = new JSplitPane();
topPanel = new JPanel(); // our top component
bottomPanel = new JPanel(); // our bottom component
// in our bottom panel we want the text area and the input components
scrollPane = new JScrollPane(); // this scrollPane is used to make the text area scrollable
textArea = new JTextArea(); // this text area will be put inside the scrollPane
// the input components will be put in a separate panel
inputPanel = new JPanel();
textField = new JTextField(); // first the input field where the user can type his text
button = new JButton("send"); // and a button at the right, to send the text
// now lets define the default size of our window and its layout:
setPreferredSize(new Dimension(400, 400)); // let's open the window with a default size of 400x400 pixels
// the contentPane is the container that holds all our components
getContentPane().setLayout(new GridLayout()); // the default GridLayout is like a grid with 1 column and 1 row,
// we only add one element to the window itself
getContentPane().add(splitPane); // due to the GridLayout, our splitPane will now fill the whole window
// let's configure our splitPane:
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); // we want it to split the window verticaly
splitPane.setDividerLocation(200); // the initial position of the divider is 200 (our window is 400 pixels high)
splitPane.setTopComponent(topPanel); // at the top we want our "topPanel"
splitPane.setBottomComponent(bottomPanel); // and at the bottom we want our "bottomPanel"
// our topPanel doesn't need anymore for this example. Whatever you want it to contain, you can add it here
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); // BoxLayout.Y_AXIS will arrange the content vertically
bottomPanel.add(scrollPane); // first we add the scrollPane to the bottomPanel, so it is at the top
scrollPane.setViewportView(textArea); // the scrollPane should make the textArea scrollable, so we define the viewport
bottomPanel.add(inputPanel); // then we add the inputPanel to the bottomPanel, so it under the scrollPane / textArea
// let's set the maximum size of the inputPanel, so it doesn't get too big when the user resizes the window
inputPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75)); // we set the max height to 75 and the max width to (almost) unlimited
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS)); // X_Axis will arrange the content horizontally
inputPanel.add(textField); // left will be the textField
inputPanel.add(button); // and right the "send" button
pack(); // calling pack() at the end, will ensure that every layout and size we just defined gets applied before the stuff becomes visible
}
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
#Override
public void run(){
new MyFrame().setVisible(true);
}
});
}
}
Please be aware that this is only an example and there are multiple approaches to layout a window. It all depends on your needs and if you want the content to be resizable / responsive. Another really good approach would be the GridBagLayout which can handle quite complex layouting, but which is also quite complex to learn.
You'll want to use a number of layout managers to help you achieve the basic results you want.
Check out A Visual Guide to Layout Managers for a comparision.
You could use a GridBagLayout but that's one of the most complex (and powerful) layout managers available in the JDK.
You could use a series of compound layout managers instead.
I'd place the graphics component and text area on a single JPanel, using a BorderLayout, with the graphics component in the CENTER and the text area in the SOUTH position.
I'd place the text field and button on a separate JPanel using a GridBagLayout (because it's the simplest I can think of to achieve the over result you want)
I'd place these two panels onto a third, master, panel, using a BorderLayout, with the first panel in the CENTER and the second at the SOUTH position.
But that's me
I am creating a class that acts as a container which holds a label and buttons. I want the buttons to only appear when I mouseover the label on a new layer, and be clickable, but this is not my question (I have discovered OyverLay Layout and JLayeredPane for this purpose, and I will figure that out later). Like I said I plan to use JLayeredPane vs regular JPanel (because of it's multi layer abilities) most likely, but my first question is this the correct type of container to use if this class will instantiate objects onto another classes JFrame container? I don't think it is because as a panel it creates a window with a title bar and buttons, and I don't want that. I just want this class to instantiate a box with my label and buttons and add it to a JFrame to be in a grid with other buttons???
My main issue is, I want this container that holds the label and buttons to be added to a JFrame that is apart of a class who's main job is to hold the containers from my first class and have the user be able to add more and remove via a UI.
Essentially I want a form with text boxes and a button, and the button adds a new ListItem object to the JFrame of another class and the new objects label text is built form the text boxes, instantiating a new object on that JFrame. So, if I hit the button 5 times I'll see 5 boxes with 5 labels in them with unique words one right on top of the other.
I have looked into using the root container, using JInternalFrames, and tried every other magic tricks but can't seem to get it to work right.
Here is the code for the class I have built.
*above this in my class is the importing of the objects I need, and the creation of various strings, buttons and labels used here. what I have pasted here is the meat of my code, the object which is created and displayed when instanciated***
public ListItem(String nameC, String timeC, String recordNum)
{
JLayeredPane panel1 = new JLayeredPane();
name = nameC; time = timeC; recordN = recordNum;
//fullItemString is a String object
fullItemString = "Help " + name + " at " + time + " regarding " + "Record # " + recordNum;
//item is a label
item.setText(fullItemString);
item.setFont(verdana);
minusButton.putClientProperty("JButton.buttonType", "square");
exportButton.putClientProperty("JButton.buttonType", "square");
editButton.putClientProperty("JButton.buttonType", "square");
// setSize(425, 50);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
minusButton.setBounds(175, -1, 35, 30);
editButton.setBounds(210, -1, 35, 30);
exportButton.setBounds(130, -1, 35, 30);
panel1.add(minusButton);//, new Integer(2));
panel1.add(top);//, new Integer(1));
panel1.add(editButton);//, new Integer(3));
panel1.add(exportButton, new Integer(4));
minusButton.addActionListener(this);
item.addMouseListener(this);
exportButton.setVisible(false);
minusButton.setVisible(false);
editButton.setVisible(false);
lp.setVisible(true);
setVisible(true);
}
Multiple panel adding in JFrame using CardLayout. try it...