This question already has answers here:
Swing rendering appears broken in JDK 1.8, correct in JDK 1.7
(8 answers)
Closed 8 years ago.
I created a GUI with one JButton in it but it just shows a blurry button.
Here you see what I mean.
This is the code of my main class:
Gui gui = new Gui();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 300);
gui.setVisible(true);
Code of gui class:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Gui extends JFrame {
public Gui() {
super("testing buttons");
getContentPane().setLayout(new FlowLayout());
JButton btnClickMe = new JButton("Click me");
getContentPane().add(btnClickMe);
}
}
Does anyone know how to fix this?
There is no reason to call
super("testing buttons");
To set the title, just use:
gui.setTitle("testing buttons");
What operating system are you running?
Related
I have a Java Swing app with some javaFX features. I am using Java 8.
I have some troubles with "scale and layout settings", if the "size of text, apps and other items" is 100% everything works fine. But if the percentage is 150% (which is the recommended size for my pc) as soon as this instruction is run:
PlatformImpl.startup
It seems that the "internal layout" of my app changes to 100%.
Is there anyone with the same problem? Is there a way to set the scale percentage (and maybe a way to retrieve it beforehand)?
EDIT:
I am using the latest jre/jdk 8 available (1.8 update 311)
Here is a minimal reproducible example:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.sun.javafx.application.PlatformImpl;
import javafx.application.Platform;
public class JavaFXTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton();
btn.addActionListener(e ->
//This is the culprit
PlatformImpl.startup(() -> System.out.println("javaFX started"))
);
JPanel panel = new JPanel();
JLabel label = new JLabel("Some character for testing");
panel.add(btn);
panel.add(label);
frame.getContentPane().add(panel);
// Display the window.
frame.pack();
frame.setVisible(true);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Loading ImageIcon from JAR or file system
(1 answer)
Closed 6 years ago.
So while making the following program I am getting the errors as mentioned below:
I don't know why the IDE is not able to find my images
and the images are in the same folder as the .java files.
I am using eclipse neon
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui_22.gui.<init>(gui.java:24)
at gui_22.MAIN.main(MAIN.java:6)
The code is as follows:
Main Class
import javax.swing.JFrame;
public 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);
}
}
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;// to create buttons
private JButton custom;// Same as above
public gui(){
super("The Title"); //Allows access to the superclass.
setLayout(new FlowLayout());
reg = new JButton("reg Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon x = new ImageIcon(getClass().getResource("a.png"));
custom = new JButton("Custom",b);
custom.setRolloverIcon(x);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);//adding a handler for the button
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand()));
}
}
}
In my Java applet I have JDialog with couple of radioButtons.
The problem is that this applet is of dimensions 700x300 and the JDialog appears in the center of screen. I want this JDialog to appear in the center of JApplet layout. My constructor invocation looks like this:
final JDialog dialog = new JDialog(
SwingUtilities.windowForComponent(GUIComponentContainer.getInstance().getDocumentTable()),
I18nCommonsImpl.constants.selectCertificate(), ModalityType.APPLICATION_MODAL);
This method:
GUIComponentContainer.getInstance().getDocumentTable()
returns JTable component which is a child of my JApplet.
I also used JDialog "setLocationRelativeTo" method:
dialog.setLocationRelativeTo(GUIComponentContainer.getInstance().getApplet());
None of these seem to work. Is there any other way to accomplish my goal?
I searched along SO for similar questions, but didn't see any working solutions.
Thanks in advance.
Getting the Window for the applet works for me, at least when the applet is launched in Eclipse. For example:
import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class AppletCentering extends JApplet {
#Override
public void init() {
final JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("Press Me") {
#Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(panel);
System.out.println("win class: " + win.getClass().getCanonicalName());
JDialog dialog = new JDialog(win, "My Dialog", ModalityType.APPLICATION_MODAL);
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true);
}
}));
add(panel);
}
}
But having said this, your question begs the question: why are you even coding for an applet? These have been out of favor for years, and just recently has lost support from Oracle who has decided to finally drop applet browser plug in support.
This question already has an answer here:
Only one component shows up in JFrame
(1 answer)
Closed 7 years ago.
I am trying to read a file with a scanner and create a JButton for each new line in the file. After I create a button, I add it to the frame. However once I run the program, only the most recent button appears. I'm not sure why creating the buttons in a loop causes this to happen. If anyone has an explanation for why this happens, that would be very much appreciated, thanks!
import java.awt.FlowLayout;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class PointOfSale extends JFrame {
ArrayList<JButton> menuButtons = new ArrayList<>();
public PointOfSale(File menu) throws IOException{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
Scanner sc = new Scanner(menu);
while (sc.hasNextLine()){
String name = sc.nextLine();
JButton menuButton = new JButton(name);
frame.add(menuButton);
menuButtons.add(menuButton);
}
sc.close();
frame.pack();
frame.setVisible(true);
}
}
Your code does not respect layout managers as the JFrame uses a BorderLayout which will only display one button. Read the layout manager tutorials, use different ones, here perhaps a GridLayout, and you'll likely have your problem solved.
//public class PointOfSale extends JFrame {
public class PointOfSale {
There is no need for your class to extend JFrame since you create the JFrame in the class.
Then you need to use:
frame.setLayout(new FlowLayout());
since this is the frame you add the buttons to.
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.