I am trying to open up an JFrame with an gif-loading animation from another JFrame. The problem is, that the new JFrame opens but the gif animations just shows up if the loading time is over, so there is a empty JFrame during the whole loading sequence.
I am trying to open up my new JFrame in my current JFrame like this:
Ladescreen loading = new Ladescreen();
loading.setVisible(true);
loading.pack();
And this is how my Loadingscreen is built:
public Ladescreen() {
setTitle("Crawling through files...");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 157, 207);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel picture = new JLabel();
picture.setIcon(new ImageIcon("football.gif"));
panel.add(picture);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("Loading...");
panel_1.add(lblNewLabel);
}
Can you tell me how to fix this up ?
Related
I have Panel that contains JLayeredPane which contains two JPanels and each of them two JLabels.
Panel panel2 = new Panel();
JLayeredPane westP = new JLayeredPane();
westP.setLayout(null);
westP.setPreferredSize(new Dimension(100,400));
westP.setBackground(Color.blue);
JPanel layerOne = new JPanel();
jl1 = new JLabel("player1: ");
layerOne.add(jl1);
layerOne.setBounds(5, 100, 50, 50);
jl1Add = new JLabel("");
layerOne.add(jl1Add);
westP.add(layerOne,0, 0);
similar for another JPanel except
westP.add(LayerTwo,0,1);
and later:
panel2.setLayout(new BorderLayout());
panel2.add(westP);
add(panel2, BorderLayout.WEST);
later when I simply test it, I get lbl1, lbl1Add, lbl2 shown on GUI but not lbl2Add?
if (activeColour==RED)
{
timesPlayedRed++;
jl1Add.setText("");
jl1Add.setText(String.valueOf((timesPlayedRed)));
activeColour=YELLOW;
}
else
{
timesPlayedYellow++;
jl2Add.setText("");
jl2Add.setText(String.valueOf((timesPlayedYellow)));
activeColour=RED;
}
I am just trying out java and netbeans.
I want to add a JButton (panButton) in the lower area of this GUI, but I don’t know to which element I have to add it. I tried to do panButton.add(element) with several elements but nothing works.
I can't understand what is the main element of the panel.
This is how the GUI appears:
While this is the code:
private void buildGUI() {
setTitle("Avanzamento upload");
setIconImage(Toolkit.getDefaultToolkit().getImage(ClientUpload.class.getResource("/clientupload/resources/logoRFI.gif")));
addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
JLabel lblProgressAll = new JLabel("Totale: ");
JLabel lblProgressCurrent = new JLabel("File attuale: ");
JLabel lblTotalBytes = new JLabel("MB totali: ");
JLabel lblCopiedBytes = new JLabel("MB copiati: ");
lblTotalBytesValue = new JLabel("0 MB");
lblCopiedBytesValue = new JLabel("0 MB");
progressButton = new JButton("Interrompi"); //fc
progressAll = new JProgressBar(0, 100);
progressAll.setStringPainted(true);
progressCurrent = new JProgressBar(0, 100);
progressCurrent.setStringPainted(true);
txtDetails = new JTextArea(5, 50);
txtDetails.setEditable(false);
DefaultCaret caret = (DefaultCaret) txtDetails.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JScrollPane scrollPane = new JScrollPane(txtDetails, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel contentPane = (JPanel) getContentPane();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel panProgressLabels = new JPanel(new BorderLayout(0, 5));
JPanel panProgressBars = new JPanel(new BorderLayout(0, 5));
JPanel panCopyLabels = new JPanel(new BorderLayout(0, 5));
JPanel panCopyFields = new JPanel(new BorderLayout(0, 5));
panProgressLabels.add(lblProgressAll, BorderLayout.NORTH);
panProgressLabels.add(lblProgressCurrent, BorderLayout.CENTER);
panCopyLabels.add(lblTotalBytes, BorderLayout.NORTH);
panCopyLabels.add(lblCopiedBytes, BorderLayout.CENTER);
panCopyFields.add(lblTotalBytesValue, BorderLayout.NORTH);
panCopyFields.add(lblCopiedBytesValue, BorderLayout.CENTER);
panProgressBars.add(progressAll, BorderLayout.NORTH);
panProgressBars.add(progressCurrent, BorderLayout.CENTER);
JPanel panProgress = new JPanel(new BorderLayout(0, 5));
panProgress.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Avanzamento"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
JPanel panCopy = new JPanel(new BorderLayout(0, 5));
panCopy.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Stato"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
JPanel panDetails = new JPanel(new BorderLayout());
panDetails.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Dettagli"), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
//fc
JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panProgress.add(panProgressLabels, BorderLayout.LINE_START);
panProgress.add(panProgressBars, BorderLayout.CENTER);
panCopy.add(panCopyLabels, BorderLayout.LINE_START);
panCopy.add(panCopyFields, BorderLayout.CENTER);
panDetails.add(scrollPane, BorderLayout.CENTER);
JPanel panUpper = new JPanel(new BorderLayout());
panUpper.add(panProgress, BorderLayout.NORTH);
panUpper.add(panCopy, BorderLayout.SOUTH);
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
I’d like it appears in the Stato area on the right, or under the Dettagli area.
Can anyone help me?
panButton is a JPanel which is never added to the GUI so if you add something to it, it -also- wont be added to the visible GUI.
You need to add the button to the button panel and add the button panel to the contentPane
JPanel panButton = new JPanel(new BorderLayout());
panButton.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(" "), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
panButton.add(element); //Add button to panel
...
contentPane.add(panUpper, BorderLayout.NORTH);
contentPane.add(panDetails, BorderLayout.CENTER);
contentPane.add(panButton, BorderLayout.CENTER); //Add panel to contentPane (which I assume is added to the frame at some point as I can see it's elements on the UI you posted)
or just add it the 'Stato' area with panCopy.add(element);
Finally I created a new JPanel and I added the button there.
I want to place two labels in the middle of the center of a window. I get it working with 1 label and the following code:
Screenshot: http://abload.de/img/scr1g6u0f.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("center1");
centerPanel.add(label, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Now I want another label next to the first label. I tried to use a flowlabel, but they are placed at the top of the BorderLayout.CENTER
Screenshot: http://abload.de/img/scr2a3u26.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Thanks!
Use a GridBagLayout without constraints:
JPanel centerPanel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel);
It says the constructor JPanel(ImageIcon) is undefined.
Here is partial of my code
public Method_1(Test aa) {
this.aa = aa;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 730, 540);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//JPanel panel = new JPanel();
BufferedImage myPicture = ImageIO.read(new File("method_1.png"));
JPanel picLabel = new JPanel(new ImageIcon(myPicture));
add(picLabel);
picLabel.setBounds(12, 34, 369, 175);
Change:
JPanel picLabel = new JPanel(new ImageIcon(myPicture));
To:
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
There is a class which extends JFrame. then Further JFrame have JPanel named contentPane and this Jpanel contains 2 more jpanel. as Tree shown in picture.
I Want to Center that contentPane in JFrame so that on changing size of JFrame JPanel (contentPane) will remain in center. I have tried with different Layouts but did not come up with right one. is there any way ?
Full page picture is here
Code is this.
public class Purchases extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Purchases frame = new Purchases();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 513, 438);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 477, 193);
contentPane.add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 214, 477, 174);
contentPane.add(panel_1);
panel_1.setLayout(null);
}
This code is Eclipse automatically generated. I did not find where the contentPane is added in JFrame.
Set the frame's layout to GridBagLayout. Wrap your existing content in another JPanel, add this panel to the frame
For example
Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details
You really should avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
I have made a sample for you. Modify it as per your need. Just want to show you what you want, is working.
TestClass()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 513, 438);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
JButton b1 = new JButton("hello");
JPanel panel = new JPanel(new BorderLayout());
//panel.setBounds(10, 10, 477, 193);
panel.add(b1, BorderLayout.CENTER);
contentPane.add(panel, BorderLayout.EAST);
//panel.setLayout(null);
JButton b2 = new JButton("why");
JPanel panel_1 = new JPanel(new BorderLayout());
//panel_1.setBounds(10, 214, 477, 174);
panel_1.add(b2, BorderLayout.CENTER);
contentPane.add(panel_1, BorderLayout.WEST);
//panel_1.setLayout(null);
this.getContentPane().add(contentPane);
}
Hope this will help. :-)
I think you should set the maximum size of the element & set it to align center:
setAlignmentX(JComponent.CENTER_ALIGNMENT);
setMaximumSize(new Dimension(100, 100));
As explained here:
How can I properly center a JPanel ( FIXED SIZE ) inside a JFrame?