I have written a java gui code for many options available on it. the gui is also set visible true but it doesn't show until I pick its border and drag them to resize the gui window. After manually resizing it, it shows everything. Also, the textlabels and the textfields and buttons are not in new lines, they are placed one after one. Please tell me whats wrong with that: here is a part of code:
public static void initGUI(){
JFrame fr = new JFrame();
Container cont = fr.getContentPane();
cont.setLayout( new FlowLayout( ) );
FlowLayout layout = new FlowLayout();
cont.setLayout(layout);
frame.setSize(200,300) ;
frame.setVisible(true) ;
JTextField tName = new JTextField(30);
JTextField tCNIC = new JTextField(15);
JLabel nameLabel = new JLabel("Name:");
JLabel cnicLabel = new JLabel("CNIC #:");
cont.add(nameLabel);
cont.add(tName);
cont.add(cnicLabel);
cont.add(tCNIC);
JButton Cancel = new JButton ("Canel" );
JButton OK = new JButton ("OK" );
savebtn.setPreferredSize(new Dimension(150, 50));
retbtn.setPreferredSize(new Dimension(150, 50));
cont.add(savebtn);
cont.add(retbtn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
frame.setVisible(true) ;
The above statement should be invoked AFTER all the components have been added to the frame. So it should be the last statement in your method.
Also, you should be invoking:
frame.pack();
instead of setSize(), before making the frame visible so all the components are displayed at their preferred size.
frame.setVisible(true);
This Statement should be invoked in the last of adding other components to the Frame.
Related
public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
I am working on a simple java chat client gui application. (the server etc, is done by others).
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
For example:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
Won't work.
Thanks for the help.
In Swing, you have two options for layout: do everything manually or let a LayoutManager handle it for you.
Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.
Try calling setPreferredSize() and setMinimumSize().
Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.
check resizing text area in a JFrame
but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()
I am trying to make a UI to view recipes from a cookbook stored on the computer. Part of this tab is a JScrollPanel storing a JTextArea that displays the available recipes. All called functions work as intended (e.g. allRecipes() returns a string of the available recipes properly); however, the scroll pane itself does not appear. It is added to the frame, as I can see by a small grey block where the pane would be, but it is not filled as it should be. The code is as follows:
//First panel, buttons to limit displayed recipes
JPanel pane1 = new JPanel();
JButton all = new JButton("All");
JButton makeable = new JButton("Makeable");
JTextField search = new JTextField("", 10);
JButton searchButton = new JButton("Search Ingredient");
//Second panel, display of recipes
JPanel pane2 = new JPanel();
JTextArea recipes = new JTextArea(allRecipes());
JLabel list = new JLabel("List of Recipes:");
JScrollPane scroll = new JScrollPane(recipes);
//Third panel, options to add recipe and view specific recipe
JPanel pane3 = new JPanel();
JButton add = new JButton("Add Recipe");
JTextField view = new JTextField("", 10);
JButton viewButton = new JButton("View Recipe");
//Central method
public Recipes() {
//basic UI stuff
super("Recipes");
setSize(475,350);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
//add pane 1
pane1.add(all);
pane1.add(makeable);
pane1.add(search);
pane1.add(searchButton);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane1);
//add pane 2
pane2.add(list);
scroll.setPreferredSize(new Dimension(10,15));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane2.add(scroll, BorderLayout.CENTER);
pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane2);
//add pane 3
pane3.add(add);
pane3.add(view);
pane3.add(viewButton);
add(pane3);
//start up the UI
setVisible(true);
}
JTextArea recipes = new JTextArea(allRecipes());
We don't know what allRecipes() does, but I would guess it sets the text of the text area.
Instead you should define your text area with the rows/columns you wish. Something like:
JTextArea recipes = new JTextArea(5, 30);
then in the constructor you would add the text:
recipes.setText( allRecipes() );
You should NOT be trying to set the preferred size of the scroll pane. The preferred size will automatically be determined from the preferred size of the text area which is calculated based on the rows/columns provided in the constructor.
//scroll.setPreferredSize(new Dimension(10,15));
Also, the preferred size of a component is specified in pixels, to the above makes no sense.
pane2.add(scroll, BorderLayout.CENTER);
The default layout manager for a JPanel is the FlowLayout. So you can't just use a BorderLayout constraint when adding the component.
I am trying to display JLabels on top of a JProgressBar so that I can have fancy formatted text on a JProgressBar.
Here is the constructor for my component, which extends JPanel:
public InfoDisplay() {
//setLayout(new GridLayout(0, 1));
setLayout(new OverlayLayout(this));
lblPlayer = new JLabel();
lblPlayer.setName("Owner");
lblUnit = new JLabel();
lblUnit.setName("Unit");
lblCoords = new JLabel();
lblCoords.setName("Coordinates");
lblResources = new JLabel();
lblResources.setName("Resouces");
prgProgress = new JProgressBar();
prgProgress.setMaximum(4);
prgProgress.setStringPainted(true);
fmtDefault = "<html><font color='gray'>%s:</font> %s</html>";
JPanel pnlInfo = new JPanel();
pnlInfo.setLayout(new FlowLayout(FlowLayout.LEADING));
pnlInfo.setOpaque(false);
pnlInfo.add(lblPlayer);
pnlInfo.add(lblUnit);
pnlInfo.add(lblCoords);
pnlInfo.add(lblResources);
add(pnlInfo);
add(prgProgress);
displayInfo(Collections.EMPTY_LIST);
setProgress("Upkeep", 2);
}
I have tried using OverlayLayout, which achieves this goal when the form is created. However, when the JProgressBar updates, it covers up the other panel which holds the text fields.
I have also tried using a JLayeredPane, but I would have to write a custom layout manager for that to work and I was hoping to avoid doing all of the resizing code by hand for such a simple thing.
Does anyone have any other solutions or ideas?
An easier approach is to just add the panel to the progress bar then you don't have to worry about invoking repaint. The progress bar will automatically repaint its child component:
See the Swing tutorial on How to Use Progress Bars. I added the following code to the ProgressBarDemo:
progressBar.setLayout( new BorderLayout() );
JPanel child = new JPanel( new BorderLayout() );
child.setOpaque(false);
child.add( new JLabel("WEST"), BorderLayout.WEST );
child.add( new JLabel("EAST"), BorderLayout.EAST );
progressBar.add(child);
public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
I am working on a simple java chat client gui application. (the server etc, is done by others).
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
For example:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
Won't work.
Thanks for the help.
In Swing, you have two options for layout: do everything manually or let a LayoutManager handle it for you.
Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.
Try calling setPreferredSize() and setMinimumSize().
Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.
check resizing text area in a JFrame
but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()
I have created a JFrame, and now I want to add a JLabel and textfields in it. Please tell me how to do that. I have used the following code but its not working.
JFrame i_frame = new JFrame("Select Locations");
i_from = new JLabel("From");
i_frame.getContentPane().add(i_from);
i_frame.add(Box.createRigidArea(new Dimension(2,0)));
i_frame.setLocationRelativeTo(null);
i_frame.setSize(200,200);
i_frame.setVisible(true);
Problem is this code is showing frame window but it is not showing label in it. Please help.
That's your first question, later you'll ask:
How do I get the value from my text field
So, I suggest you to take a look at this tutorial
http://java.sun.com/docs/books/tutorial/uiswing/start/index.html
And ask us if you have questions from that tutorial. Using an IDE will simplify your life.
Here's a start anyway:
Code:
import javax.swing.*;
import java.awt.Color;
public class MyApp {
public static void main( String [] args ) {
JFrame frame = new JFrame();
frame.setBackground( new Color(0,0,0,64 ));
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JPanel top = new JPanel();
top.add( new JLabel("What is your name"){{
setForeground(Color.white);
}});
top.add( new JTextField(10) );
frame.add( top );
frame.pack();
frame.setVisible( true );
}
}
EDIT
Dismiss this answer. It was posted when the question was rather ambiguous. I'm leaving it as CW for it may be helpful.
plus OSX frames with alpha background are too cool!
The main issue is the layout being used by the content pane (most likely JRootPane.RootLayout, A custom layout manager that is responsible for the layout of layeredPane, glassPane, and menuBar ) is not the best. This code should work;
JFrame i_frame = new JFrame("Select Locations");
JLabel i_from = new JLabel("From");
i_frame.getContentPane().setLayout(new FlowLayout());
i_frame.getContentPane().add(i_from);
i_frame.add(Box.createRigidArea(new Dimension(2,0)));
i_frame.setLocationRelativeTo(null);
i_frame.setSize(200,200);
i_frame.setVisible(true);
setting a different layout does the trick
The problem is that you are adding a second component (Box.createRigidArea) in the same layout position (BorderLayout.CENTER) as the Label.
JFrame.add() results in the same as JFrame.getContentPane().add()...
So the label is being replaced by the RigidArea.
Try this:
JFrame i_frame = new JFrame("Select Locations");
i_from = new JLabel("From");
i_frame.getContentPane().add(i_from);
// i_frame.add(Box.createRigidArea(new Dimension(2,0)));
i_frame.setLocationRelativeTo(null);
i_frame.setSize(200,200);
i_frame.setVisible(true);
to add more components, change the LayoutManager:
JFrame i_frame = new JFrame("Select Locations");
JLabel i_from = new JLabel("From");
Container pane = i_frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS)); // any LayoutManager you need
pane.add(i_from);
pane.add(Box.createRigidArea(new Dimension(2,0))); // space after JLabel ?
i_frame.setSize(200,200); // better done before setLocationRelativeTo
i_frame.setLocationRelativeTo(null);
i_frame.setVisible(true);
Note: if you "just" want to add an empty border to your label, use a setBorder() instead of the RigidArea:
i_from.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
[]]
For adding JLabel to JFrame see the line of code see here
public class JLabelExample extends JFrame {
JLabel jLabel;
public JLabelExample() {
jLabel = new JLabel();
jLabel.setText("tutorialData.com");
jLabel.setForeground(Color.BLUE);
this.add(jLabel);
}
public static void main(String a[]) {
JLabelExample labelExample = new JLabelExample();
labelExample.setSize(250, 200);
labelExample.setTitle("tutorialData.com");
labelExample.setVisible(true);
}
}
Please refer more on JFrame