Adding resources to a Java project - java

I'm trying to add an icon to my JButton, but I keep getting an NullPointerException meaning that the image i specified cannot be found.
Both my classes and the buttonremoterefresh.png are directly inside the src folder (the classes are inside the default package). I have been googling this around since last night and no matter what I try, I can't manage to load the resource.
public class InfiltratorClient {
private MainWindow mw;
public static void main(String[] args) {
new InfiltratorClient();
}
public InfiltratorClient () {
mw = new MainWindow();
}
}
public class MainWindow extends JFrame {
private JPanel contentPane;
private InfiltratorClient n;
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
this.setSize(650, 600);
setVisible(true);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(258, 228, 140, 105);
contentPane.add(btnNewButton);
//In this Line i get the exception
ImageIcon icon = new ImageIcon(MainWindow.class.getResource("buttonremorerefresh.png"));
btnNewButton.setIcon(icon);
repaint();
revalidate();
}
}

use this code
JButton button = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("buttonremorerefresh.png"));
button.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}

Related

JScrollPane scroll only appears at bottom

I have this JFrame that has a JScrollPane and a JTextArea inside it, it works okay but the scroll button only appears at the bottom, I want it to show at the top as default. How can I change this?
public class frameMyPasswords extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frameMyPasswords frame = new frameMyPasswords();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frameMyPasswords() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scrollPane.getViewport().setViewPosition( new Point(0, 0) );
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setValue(0);
scrollPane.setBounds(10, 45, 414, 194);
contentPane.add(scrollPane);
JTextArea txtField = new JTextArea();
scrollPane.setViewportView(txtField);
txtField.setLineWrap(true);
txtField.setEditable(false);
txtField.setToolTipText("");
txtField.setText("LONG TEXT");
}
}

Scroll Bar does not adjust to JPanel dimensions, Java JScrollPane

I am trying to get a scroll bar for a Panel that will get a big number of lines, however I am stuck whit this.
As you can see in my window (click here to open the picture), Scroll bars does not adjust to the panel's dimensions
Below you can see the code i am working with:
public class Config extends JFrame {
private static JPanel contentPane;
private static JScrollPane paneSiteScroll;
private static JPanel paneSite;
private JTextField txtURL;
private JButton btnAdd;
private JLabel lblName;
private JTextField txtName;
private JPanel paneBar;
/**
* Launch the application.
*/
public static void config() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Config frame = new Config();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #throws Throwable
*/
public Config() throws Throwable {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 550);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Start top Add Bar
paneBar = new JPanel();
paneBar.setBounds(0, 0, 700, 35);
contentPane.add(paneBar);
paneBar.setLayout(null);
JButton btnBack = new JButton("Back");
btnBack.setBounds(1, 3, 65, 28);
paneBar.add(btnBack);
btnBack.setVerticalAlignment(SwingConstants.TOP);
JLabel lblUrl = new JLabel("URL:");
lblUrl.setBounds(80, 9, 35, 20);
paneBar.add(lblUrl);
txtURL = new JTextField();
txtURL.setBounds(115, 5, 310, 26);
paneBar.add(txtURL);
txtURL.setColumns(10);
lblName = new JLabel("Name:");
lblName.setBounds(435, 9, 47, 20);
paneBar.add(lblName);
txtName = new JTextField();
txtName.setBounds(480, 5, 155, 26);
paneBar.add(txtName);
txtName.setColumns(10);
btnAdd = new JButton("Add");
btnAdd.setBounds(635, 3, 61, 29);
paneBar.add(btnAdd);
//End top Add Bar
//Start Scroll and Panel configuration
paneSiteScroll = new JScrollPane();
paneSite = new JPanel();
paneSite.setLayout(null);
paneSite.setBounds(10, 40, 400, 600);
/*Testing different dimensions configuration*/
paneSiteScroll.setSize(new Dimension(300, 300));
paneSiteScroll.setBounds(10, 40, 300, 200);
paneSiteScroll.setPreferredSize(new Dimension(400, 300));
/*Testing different dimensions configuration*/
paneSiteScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
paneSiteScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
paneSiteScroll.getViewport().add(paneSite);
contentPane.add(paneSiteScroll);
paneSiteScroll.setVisible(true);
paneSite.setVisible(true);
//End Scroll and Panel configuration
try {
Categories.categories(paneSite); //GET CATEGORIES
} catch (Throwable e) {
e.printStackTrace();
}
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!txtURL.getText().isEmpty() || !txtName.getText().isEmpty()) {
InsertDB insert = new InsertDB();
try {
insert.insertDB("INSERT INTO sites (siUrl, siName) VALUES ('" + txtURL.getText() +"'"
+ ", '" + txtName.getText() + "')",
conn);
} catch (Exception e1) {
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null, "URL or Name cannot be empty!");
}
}
});
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Back action button
dispose();
}
});
}
}
This is my first question, I do not know what else to add, also i visited many question from stak overflow with similar problems but i couldn't figure it out.
Thanks in advance.
Finally fixed it, I restored the layout to null and added paneSite.setPreferredSize(new Dimension(600, 600)); after paneSite.setbounds. This make it work.

When adding a custom JPanel to my JFrame nothing appears

I am doing my first Java Swing app, I am using Windows Builder for this.
I have a MainFrame that extends JFrame.
public class MainFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
PanelTest panelTest = new PanelTest();
frame.getContentPane().add(panelTest);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
Then I have in another class my JPanel
package gui;
public class PanelTest extends JPanel {
/**
* Create the panel.
*/
public PanelTest() {
setBounds(100, 100, 1225, 835);
//getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
//getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel lblCreacion = new JLabel("Hello");
lblCreacion.setBounds(180, 16, 427, 20);
contentPanel.add(lblCreacion);
}
}
I don't get it, the panel works fine in Windows Builder but once I add it, nothing appears, there is already a layout in the JFrame so I don't know what is missing.
Thank you.
The problem is that it does not work when you add the element after making it visible with frame.setVisible(true); .
Code should be like this:
PanelTest panelTest = new PanelTest();
frame.getContentPane().add(panelTest);
frame.setVisible(true);
Thanks!.

Scrolling Text Pane JFrame Java

I'm trying to create a sort of log of all the keys hit, at the moment I just need to figure out how to either:
Link the position of the "text" to the scroll bar to the right
OR
Add a different component which is suited better to hold large amounts of multiple line text.
What am I doing wrong here? Thanks!
public class MacroMakerGui extends JFrame {
public static final long serialVersionUID = 1L;
public static JPanel contentPane;
public static JTextField textField = new JTextField();;
public static MacroKeyListener keylistener = new MacroKeyListener(textField);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MacroMakerGui frame = new MacroMakerGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MacroMakerGui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 126, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JButton btnNewButton = new JButton("Record Macro");
btnNewButton.setBounds(10, 220, 99, 30);
contentPane.add(btnNewButton, null);
textField.setBounds(10, 189, 99, 20);
contentPane.add(textField);
textField.setColumns(10);
JEditorPane editorPane = new JEditorPane();
editorPane.setBounds(10, 11, 84, 153);
contentPane.add(editorPane);
JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(93, 11, 17, 153);
contentPane.add(scrollBar);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
btnNewButton.addKeyListener(keylistener);
}
});
}
}
Instead of JScrollBar, use JScrollPanel. Add that to the contentPane, and add your editorPane as a chiled of the JScrollPanel.

JFrame not displaying, nothing is displaying

I have no idea why my JFrame isn't displaying and its really bugging me and i feel like I'm missing something easy and I'm just not seeing it. So any help would be awesome!
here is the code:
import java.awt.*;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.GroupLayout.*;
public class Lorenzo_ChatClient_class extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/** Create the frame. */
public Lorenzo_ChatClient_class() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JFrame frame = new JFrame();
frame.setSize(100,100);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField_1 = new JTextField();
textField_1.setBounds(0, 244, 450, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(351, 6, 99, 122);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setBounds(351, 124, 99, 122);
contentPane.add(btnNewButton_1);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBorder(UIManager.getBorder("EditorPane.border"));
textArea_1.setBounds(6, 6, 345, 240);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textField = new JTextField();
textField.setColumns(10);
}
}
In your main method ,
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
In your constructor,
setBounds(100, 100, 450, 300);
JFrame frame = new JFrame();
frame.setSize(100,100);
You have taken class reference and JFrame names are same "frame".
So apparently I read over your code too quickly, because you do have Lorenzo_ChatClient_class extending JFrame. That changes the solution drastically.
I think your main method looks fine. You shouldn't be calling overridable methods in a constructor; it leads to a lot of bugs. Try changing the code to this:
import java.awt.*;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.GroupLayout.*;
import javax.swing.border.*;
class Lorenzo_ChatClient_class extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Lorenzo_ChatClient_class frame = new Lorenzo_ChatClient_class();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Lorenzo_ChatClient_class() {
super();
frameSetup();
setVisible(true); // or just keep it in your main method
}
private void frameSetup() {
this.setBounds(100, 100, 450, 300);
this.setSize(100, 100);
this.contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField_1 = new JTextField();
textField_1.setBounds(0, 244, 450, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("Send");
btnNewButton.setBounds(351, 6, 99, 122);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Quit");
btnNewButton_1.setBounds(351, 124, 99, 122);
contentPane.add(btnNewButton_1);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBorder(UIManager.getBorder("EditorPane.border"));
textArea_1.setBounds(6, 6, 345, 240);
contentPane.add(textArea_1);
JTextArea textArea = new JTextArea();
textField = new JTextField();
textField.setColumns(10);
}
}
I just tried it out and this works for me, although it's a small frame. Not sure what your reasoning for having setbounds(100, 100, 450, 300) followed immediately by setSize(100,100). You're setting the size to 450x300, and then immediately resetting the size to 100x100. I deleted the setSize(100, 100) line and the JFrame opened up a lot nicer.

Categories