1

I want to add a status bar into my Java application in netbeans.

I googled about it and I found this article:

How can I create a bar in the bottom of a Java app, like a status bar?

I did the same as in that article but I had an error.

This is the code I tried:

public void run() {

    PersonelMainForm personelMainForm = new PersonelMainForm();

    personelMainForm.setExtendedState(
        personelMainForm.getExtendedState()|JFrame.MAXIMIZED_BOTH );

    // create the status bar panel and shove it down the bottom of the frame
    statusPanel = new JPanel();
    statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
    PersonelMainForm.add(statusPanel, BorderLayout.SOUTH);
    statusPanel.setPreferredSize(new Dimension(PersonelMainForm.getWidth(), 16));
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
    JLabel statusLabel = new JLabel("status");
    statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
    statusPanel.add(statusLabel);

    personelMainForm.setVisible(true);
}

and this is the error message for the line PersonelMainForm.add(statusPanel, BorderLayout.SOUTH); :

non-static method add(java.awt.Component,java.lang.Object) cannot be referenced from a static context

and this is the error message for the line statusPanel.setPreferredSize(new Dimension(PersonelMainForm.getWidth(), 16)); :

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - non-static method getWidth() cannot be referenced from a static context

Community
  • 1
  • 1
Aimad Majdou
  • 563
  • 4
  • 13
  • 22

3 Answers3

0
 PersonelMainForm.add(statusPanel, BorderLayout.SOUTH);

Here you are trying to use an static add method, which does not exist (remember, identifiers begiining with capital letters must be classes). From your code, it looks like you already have an instance created, with proper capitalization for instances (lowercase), so just change it to:

 personelMainForm.add(statusPanel, BorderLayout.SOUTH);

Do the same for the other error.

Remember that in java capitalization is important (an uppercase and lowercase identifier are not the same). You can only call a method from a class identifier if it is static.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

Consider to build your application on top of the NetBeans Platform (a RCP based on Swing). It comes with StatusBar support and much more:

https://netbeans.apache.org/kb/docs/platform/

http://wiki.netbeans.org/BookNBPlatformCookbookCH0211

Puce
  • 37,247
  • 13
  • 80
  • 152
  • nothing in the world is quite as anticipatory and then a source of disillusionment as a broken link.... – Jason S Oct 07 '22 at 03:08
  • 1
    found it in archive.org: https://web.archive.org/web/20170719185422/http://wiki.netbeans.org/BookNBPlatformCookbookCH0211 – Jason S Oct 07 '22 at 03:13
  • As NetBeans moved to Apache some links seem now to be broken. I fixed the first link, I haven't found the second yet. That said, please note that Java Swing is a legacy technology. Consider JavaFX for Java based GUIs. – Puce Oct 11 '22 at 12:02
  • OMG I've seen both and I'd much rather use Swing for the simplicity. – Jason S Oct 11 '22 at 13:40
0

You simply mistyped p into P

Change

PersonelMainForm.add(/* ... */)
PersonelMainForm.getWidth()

To

personelMainForm.add(/* ... */)
personelMainForm.getWidth()
johnchen902
  • 9,531
  • 1
  • 27
  • 69