I have an applet with a main view and multiple additional pop-up JFrames.
One JFrame (Report() ) takes more than 20 seconds to load because of an SQL query.
I would like to display a pop-up JFrame which would warn the user, that the requested window is loading, but the content of the pop-up JFrame is displayed only after the asked JFrame was loaded.
This is the code that I have:
JFrame frame = new JFrame("Loading");
JLabel label = new JLabel("My label");
label.setFont(new java.awt.Font("Arial", 1, 16));
label.setText("<html> Please wait while </br> report loads </br>");
JPanel top = new JPanel();
top.add(label);
top.setBackground(Color.WHITE);
frame.add(top);
frame.setBackground(Color.WHITE);
frame.pack();
frame.setSize(200, 200);
frame.setVisible(true);
new Report();
In Report I do not have a main() method, I initialize it from the constructor directly with:
// setting up the cross-platform look and feel
preInitComponents();
// setting up the UI
initComponents();
// visualizing the jframe
pack();
setVisible(true);
Any suggestions what am I doing wrong?
Related
I Have an already given JFrame frame, that I want to show it (insert it) into a tab of JTabbedPane, but that was not possible explicitely like that:
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainTabs.addTab("Editor", null, frame, null);
And the error was:
java.lang.IllegalArgumentException: adding a window to a container
I tried some solutions like to insert the frame into a JPanel but also in vain. I have the idea to convert it into an InternalJFrame but I don't have any idea about that.
Is that any solution to insert that frame?
UPDATE:
I tried that soluion:
mainTabs.addTab("Editor", null, frame.getContentPane(), null);
But i lost the JMenuBar that I added.
You can't add JFrame(or another top-level component) to another component/container, but you can use getContentPane() method of frame, to get main panel of your frame and add that to JTabbedPane tab. Like next:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
frame.add(new JButton("button"));
tabs.addTab("1", frame.getContentPane());
Also you can change JFrame to JPanel and use that.
Read about JInternalFrame, top-level containers.
EDIT: getContentPane() doesn't return any decorations or JMenuBar, this components you need to add manually, like in next example with menu:
JTabbedPane tabs = new JTabbedPane();
JFrame frame = new JFrame();
JMenuBar bar = new JMenuBar();
bar.add(new JMenu("menu"));
frame.setJMenuBar(bar);
frame.add(new JButton("button"));
JPanel tab1 = new JPanel(new BorderLayout());
tab1.add(frame.getJMenuBar(),BorderLayout.NORTH);
tab1.add(frame.getContentPane());
tabs.addTab("1", tab1);
I can't seem to add a JButton to a JPanel.
I have a PropWindow (JFrame) that has a PropView (JPanel) in it. the PropView-JPanel seems to be added correctly because I can draw shapes on it with paint().
But when I use this to try adding a button it just won't show up att all :/
JButton testButton;
public PropView(int width, int height) {
super(true);
setLayout(null);
setSize(width, height);
//TestButton
testButton = new JButton("Test");
testButton.setLocation(10,10);
testButton.setSize(100, 50);
testButton.setVisible(true);
add(testButton);
setFocusable(true);
setVisible(true);
}
The JFrame and the JPanel are both 250x600 px.
I can't tell from the code snippet you posted but just in case: make sure you call pack () on the frame after you have added the panel or any other components.
Also, it's usually discouraged to extend a JPanel or JFrame, unless you have a good reason to do it, just a heads up.
Here you have a short tutorial about displaying frames:
And some sample code in it that might help:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
Make sure you added PropPanel to PropWindow using myPropWindow.getContentPane().add(myPropPanel), not just myPropWindow.add(myPropPanel).
I just want to make a JFrame that will say "Hello world", nothing big, no interaction needed. How do I do this?
I can create the JFrame, however I do not know how to put a JPanel with simple text in it.
Here is what I got so far
JFrame frame = new JFrame("Relief Valve");
frame.setResizable(false);
frame.setLocation(500,300);
JPanel p1 = new JPanel();
frame.setVisible(true);
Instead of creating the JPanel, try:
JLabel label = new JLabel("this is my text");
frame.add(label);
frame.pack();
JFrame window = new JFrame("Hello World App");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
window.add(new JLabel("Hello World"), BorderLayout.CENTER);
window.pack();
window.setVisible(true);
window.setLocationRelativeTo(null);
I'm currently on a mobile device but I'll be happy to document that when I get on a computer, feel free to ask any questions though.
You need to create a JLabel (witch is from Swing library) the code for that is:
JLabel label = new JLabel("Hello world");
if u want to set it to a specific loaction you need to create a render method :
public void render(Graphics g){
g.drawString(label,x,y);
}
the x and y is the position of the string with your label.
In order to create a JFrame with a simple text, you have to create a label and attach it to your frame.
Let's assume you have a JFrame created:
JFrame myFrame = new JFrame("My Frame");
Let's create the text label:
JLabel myLabel = new JLabel("Text");
To change a text of already created label:
myLabel.setText("New Text");
And eventually to clear the label:
myLabel.setText("");
Let's connect all the dots:
myFrame.add(myLabel, BorderLayout.CENTER);
myFrame.pack();
To learn more about JFrames, check: https://javatutorial.net/swing-jframe-basics-create-jframe
Alright so here's the problem:
I created this small guessing game and wanted to make it gui based...
but it appears in two different windows -
first window is the menu(button and label)
second window is activated by the button and has the game in it.
Is there a way for me to have both windows in one- AS IN the first window gets overwritten by the second window?
public class Skeleton extends JFrame implements ActionListener
{
JPanel glass = new JPanel();
JButton btn = new JButton("Start");
TextField tf = new TextField();
JLabel label = new JLabel("Enter Guess Here: ");
JLabel answerLabel = new JLabel("Answer:...");
Board bob =new Board();
public Skeleton()
{
setIconImage(new ImageIcon("icon.png").getImage());
getContentPane().setBackground(Color.darkGray);
getContentPane().setForeground(Color.black);
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
add(label);
label.setForeground(Color.black);
add(tf);
tf.setText("");
add(btn);
btn.setBackground(Color.green);
btn.addActionListener(this);
add(answerLabel);
answerLabel.setForeground(Color.black);
setTitle("Guessing Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}
public void actionPerformed(ActionEvent e)
{//GAME FRAME
JFrame frame = new JFrame();
int userGuess= Integer.parseInt(tf.getText());
frame.add(bob);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300,285);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(new ImageIcon("icon.png").getImage());
...........
This is obviously not the full code, just the part that uses the GUI.
NOTE: I do have some other within this actionevent which occur after the button is clicked.
Also this is my first game ever :D (if i can call it one)
You create new JFrame into "actionPerformed" method, so it's normal to have two frames (the Skeleton instance and the new one).
Just use "this" instead of a new JFrame instance to keep only one JFrame instance.
Each JFrame will create a new window. If you want to display everything into the same window, create only one JFrame and change its content.
You can change the whole content of a JFrame by setting its contentPane (frame.setContentPane(...)) or by adding/removing Panel to it.
I suggest to take a look at JPanel since you will use it a lot.
Hello I have a problem with the focus
mytext= new JTextField();
mytext.requestFocus(true);
gc.fill =GridBagConstraints.HORIZONTAL ;
gc.gridx =3; gc.gridy=4;
gbl.setConstraints(mytext,gc);
jContentPane.add(mytext);
I tried
mytext.requestFocus();
too
and how can I auto-select the text in the textfield so the text is marked?
From the Swing Tutorial
If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:
//...Where initialization occurs...
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new BorderLayout());
//...Create a variety of components here...
//Create the component that will have the initial focus.
JButton button = new JButton("I am first");
panel.add(button);
frame.getContentPane().add(panel); //Add it to the panel
frame.pack(); //Realize the components.
//This button will have the initial focus.
button.requestFocusInWindow();
frame.setVisible(true); //Display the window.
As for selecting all the text you should use...
mytext.selectAll();
As for getting focus, maybe you should try the requestFocus function after everything has been added to jContentPane.