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?
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
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);
}
}
Related
I am trying to create an application with several tabbed panes, and to keep the code manageable, I wanted to have the content for these panes in separate classes, in separate .java files.
I have 3 files currently
(i) TestLayout.java
package testlayout;
public class TestLayout
{
public static void main(String[] args)
{
MainFrame mainFrameObject = new MainFrame();
mainFrameObject.displayMainFrame();
}
}
(ii) MainFrame.java
package testlayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
public class MainFrame
{
JFrame masterFrame = new JFrame("JAVA 1.1");
JTabbedPane tabbedPane = new JTabbedPane();
public void displayMainFrame()
{
masterFrame.setSize(1000, 600);
masterFrame.setVisible(true);
masterFrame.setResizable(false);
masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
masterFrame.add(tabbedPane);
DisplayReadMe drmObj = new DisplayReadMe();
drmObj.showReadMe();
//showReadMe();
}
/*
public void showReadMe()
{
JPanel panelReadMe = new JPanel(new GridLayout(10,1,8,8));
panelReadMe.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tabbedPane.addTab("Read Me", null, panelReadMe, "First Tab");
String testreadMeTestMessage = "This is a test message";
JLabel testreadMeLabel = new JLabel(testreadMeTestMessage, SwingConstants.LEFT);
testreadMeLabel.setBorder(BorderFactory.createLineBorder(Color.orange,3));
panelReadMe.add(testreadMeLabel);
}
*/
}
and
(iii) DisplayReadMe.java
package testlayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class DisplayReadMe extends MainFrame
{
public DisplayReadMe()
{
}
public void showReadMe()
{
System.out.println("method showReadMe begins");
JPanel panelReadMe = new JPanel(new GridLayout(10,1,8,8));
panelReadMe.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tabbedPane.addTab("Read Me", null, panelReadMe, "First Tab");
String testreadMeTestMessage = "This is a test message";
JLabel testreadMeLabel = new JLabel(testreadMeTestMessage, SwingConstants.LEFT);
testreadMeLabel.setBorder(BorderFactory.createLineBorder(Color.orange,3));
panelReadMe.add(testreadMeLabel);
System.out.println("method showReadMe ends");
}
}
My query is, when I uncomment the //showReadMe(); and showReadMe method in MainFrame, it works. The tab is added to the JFrame and the test message shows in the box.
But should the
DisplayReadMe drmObj = new DisplayReadMe();
drmObj.showReadMe();
code, not do the same thing? Am I not calling the showReadMe method from the DisplayReadMe class, akin to showReadMe().
I have tried revalidate, repaint and threading and nothing seems to call the method and show the tab and message ?
Any guidance would be gratefully appreciated
Many Thanks
PG
The method is actually working, but the tabbedPane instance in drmObj is different with respect to the JTabbedPane class member in MainFrame. Try to add tabbedPane as a parameter in showReadMe() and refer to that instance whenever adding elements. It should work.
public void showReadMe(JTabbedPane tabbedPane);
...
drmObj.showReadMe(this.tabbedPane);
Hope it helps!
you may not have duplicated method with same parameters, check if you have another showReadMe method who expects nothing as parameter.
if you make an overwrite of the showReadMe, remember that it will make showReadMe of the main class, then will make showReadMe inherited since it cannot go more up.
i donno if i explained it well at all.
I am going through thenewboston's tutorials and I have an unexpected error. I have tried to do everything that Eclipse suggest, but can't figure it out where the problem is.
this is my Main Class
import javax.swing.JFrame;
class Main {
public static void main(String args[]) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
and this is GUI Class
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton reg;
private JButton custom;
public Gui(){
super("The title");
setLayout(new FlowLayout());
reg = new JButton("Regular Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon a = new ImageIcon(getClass().getResource("a.png"));
custom = new JButton("Custom", b);
custom.setRolloverIcon(a);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
}
Thanks brothers for helping me out!
You've posted a couple of different stacktraces with the error on different line numbers but the code seems to have moved around. The error itself is talking about a NullPointerException in a constructor for ImageIcon. Not really anything to do with the JButton so the tags are misleading.
Basically you're looking up an image location of b.png and a.png. If these two files don't exist then you'll get a runtime exception like you have. The quick fix is to add these two images to the project so they are found.
A more robust solution would be to handle the exception and either output a more meaningful error or just carry on without the icon on the gui.
I have this project and it allows users to kind of create there own projects within it and save it off and do much more. Im doing this all in Java using the program Eclipse. Today I mainly wanted to know how would i go about displaying a Hierarchy? Ill be a little more specific, when the user creates a project it ask them where they want to have there project folder. Lets say they choose a folder name JavaProjects and its in there Desktop (I use windows so excuse me if it isnt the same on Mac and Linux) and within that folder they have a Scripts folder and an Art Folder and within there art folder they have a Texture folder and a Logo Folder (Im also coming up with these folders in my head as i make this) How can i have it where in my JPanelEast it display a format kind of like the Package Explorer In Eclipse? Would I be able to just scan the folder they put in and have all the folders and files neatly laid out like that? Or would i have to do something much more out of my knowledge?
If it helps here is my code, minus a menu bar and action listeners
package Engine;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;
#SuppressWarnings("serial")
public class TestProjectBuilder extends JFrame {
JPanel jPanelNorth = new JPanel();
JPanel jPanelSouth = new JPanel();
JPanel jPanelEast = new JPanel();
JPanel jPanelCenter = new JPanel();
JButton jButtonDebug = new JButton("Debug");
JButton jButtonPause = new JButton("Pause");
JButton jButtonRun = new JButton("Run");
JLabel jLabelHeir = new JLabel("");
GridLayout gridLayout1 = new GridLayout(4,1);
public TestProjectBuilder() {
setTitle("Test Project Builder");
setSize(1400, 800);
jPanelNorth.setBackground(Color.DARK_GRAY);
jPanelNorth.setBorder(BorderFactory.createRaisedBevelBorder());
jPanelNorth.setPreferredSize(new Dimension(14, 40));
jPanelNorth.setToolTipText("North Panel");
jPanelNorth.add(jButtonDebug);
jButtonDebug.setHorizontalAlignment(SwingConstants.CENTER);
jPanelNorth.add(jButtonPause);
jButtonPause.setHorizontalAlignment(SwingConstants.CENTER);
jPanelNorth.add(jButtonRun);
jButtonRun.setHorizontalAlignment(SwingConstants.CENTER);
jPanelSouth.setBackground(Color.DARK_GRAY);
jPanelSouth.setBorder(BorderFactory.createTitledBorder(""));
jPanelSouth.setPreferredSize(new Dimension(10,200));
jPanelSouth.setToolTipText("South Panel");
jPanelEast.setBackground(Color.DARK_GRAY);
jPanelEast.setBorder(BorderFactory.createEtchedBorder());
jPanelEast.setPreferredSize(new Dimension(300,10));
jPanelEast.setToolTipText("East Panel");
jPanelCenter.setBackground(Color.GRAY);
jPanelCenter.setBorder(BorderFactory.createEtchedBorder());
jPanelCenter.setPreferredSize(new Dimension(56,10));
jPanelCenter.setToolTipText("Center Panel");
this.getContentPane().add(jPanelNorth, BorderLayout.NORTH);
this.getContentPane().add(jPanelSouth, BorderLayout.SOUTH);
this.getContentPane().add(jPanelEast, BorderLayout.EAST);
this.getContentPane().add(jPanelCenter, BorderLayout.CENTER);
jPanelCenter.setLayout(gridLayout1);
}
public static void main(String[] args) {
TestProjectBuilder tpb = new TestProjectBuilder();
tpb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tpb.setVisible(true);
}
}
Thanks for all Help in advance
You might start with the code for File Browser GUI.
The Java Tutorial has a section on using TreeViews for this.
http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html
I post the link here, because this a bit to complicated for a simple answer.
im getting an error for line 20(labeled) For .createGLCanvas i get an error saying " The method createGLCanvas(GLCapabilities) is undefined for the type GLDrawableFactory" What does this mean? Did i not import something i was suppose to import?
import javax.media.opengl.*;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import java.awt.Canvas;
import javax.swing.JPanel;
public class Forest{//open forest
public static void main(String[] args)
{
Frame frame = new Frame("Hello World");
20: GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
frame.add(canvas);
frame.setSize(300, 300);
frame.setBackground(Color.WHITE);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
}//close forest
}
It means there is no method called createGLCanvas on GLDrawableFactory. So you need to figure out what to call.
The point is that according to javadoc this method should be supported:
GLCanvas createGLCanvas(GLCapabilities capabilities)
I think that you should check again your classpath. What jar are you working with? Where have you downloaded it from? What is the jar version? Check all this and use appropriate API doc that describes your version of the library when you are writing code.
According to this thread, the correct way to do this is now GLCanvas canvas = new GLCanvas();. This seems to represent an API change.
Why is it that my code is not showing the image that I inserted? there's no compilation error or Syntax error but why is it like that?
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
public class FirstUI extends JFrame{
private JLabel firstlabel;
private JLabel secondLabel;
private JLabel pie;
public FirstUI(){
super("Tittle");
setLayout(new FlowLayout());
firstlabel = new JLabel("Hello World");
firstlabel.setToolTipText("Hello World");
String path = "pie.png";
Icon pie = new ImageIcon(path);
secondLabel = new JLabel("Text with icon",pie,SwingConstants.LEFT);
add(secondLabel);
add(firstlabel);
}
}
main class
import javax.swing.JFrame;
public class FirstUiTest {
public static void main(String[] args){
FirstUI MyUI = new FirstUI();
MyUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyUI.setSize(320,280);
MyUI.setVisible(true);
}
}
if the "pie.png" is in the same Path of FirstUI.class try to use:
Icon pie = new ImageIcon(ImageIO.read( FirstUI.class.getResourceAsStream( "pie.png" ) ) );
I tried this exact code, and it worked. It looks like pie.png cannot be found. If you're using eclipse, put it in the project root (The same folder that has /bin and /src). Otherwise, put it in the same directory where you run the java command from.