-2

I am just trying to use the class Box (Java API) using Eclipse Neon. When I import javax.swing.Box, the class seems not to exist anymore.

If I call the function like this:

Box myBox = Box.createHorizontalBox();

Eclipse shows an error: "The method createHorizontalBox() is undefined for the type Box"

Is class Box (and functions) not included in javax.swing? Any idea what is wrong?

user812786
  • 4,302
  • 5
  • 38
  • 50
pctripsesp
  • 53
  • 10
  • It still exists and works. – Boann Jan 17 '17 at 18:12
  • 1
    It does exist, see [the docs](https://docs.oracle.com/javase/8/docs/api/javax/swing/Box.html). Do you possibly have a different `Box` class defined in your project that it's calling? What happens if you call it explicitly, like `javax.swing.Box.createHorizontalBox()`? – user812786 Jan 17 '17 at 18:12
  • Are you sure you have imported `javax.swing.Box` and not one of the other Box classes such as `javafx.scene.shape.Box`? – greg-449 Jan 17 '17 at 18:15
  • 1
    For what it’s worth, Java does not remove public methods (or public fields or public classes) from any java or javax package. This is a part of a fundamental concept in OO known as a *contract.* (In theory, deprecated APIs can be removed in later releases, but so far it hasn’t happened.) – VGR Jan 17 '17 at 18:23

2 Answers2

0

Ok I was trying to do that inside a class called "Box", so I just changed the name of the class and all worked find.

Hope to be helpful to anybody else

pctripsesp
  • 53
  • 10
0

Yes, it is still.

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());
    Box rowTwo = Box.createHorizontalBox();
    rowTwo.add(new JLabel("Password"));
    rowTwo.add(new JPasswordField());
    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
HadiHero
  • 1
  • 2
  • See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jan 17 '17 at 19:46