Java add components on WindowBuilder built GUI - java

I built a Java-Swing GUI using the WindowBuilder in Eclipse. But when I try to add new components using the .add() and .revalidate() nothing happens.
If someone could help to fix this issue I realy would apreciate it.
package Frame;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
public class TestFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame window = new TestFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnSampleButton = new JButton("Sample Button");
btnSampleButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.add(new JButton("BTN"));
frame.revalidate();
frame.repaint();
}
});
frame.getContentPane().setLayout(null);
btnSampleButton.setBounds(110, 126, 185, 112);
frame.getContentPane().add(btnSampleButton);
}
}

Try following:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
TestFrame window = new TestFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnSampleButton = new JButton("Sample Button");
btnSampleButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JButton btn = new JButton("BTN");
btn.setSize(btn.getPreferredSize());
btn.setLocation(new Point(1, 1));
frame.add(btn);
frame.revalidate();
frame.repaint();
}
});
frame.getContentPane().setLayout(null);
btnSampleButton.setBounds(110, 126, 185, 112);
frame.getContentPane().add(btnSampleButton);
}
}
Try to learn Layout Managers. When you use an appropriate layout manager you shouldn't set the component's size/location.

Related

JDateChooser does not appear

I have a Java app and a JDateChooser. The problem comes when I start my app.
The JDateChooser remains invisible and appears only after I iconify my app.
What I should do?
dateChooser = new JDateChooser();
dateChooser.setDateFormatString("d/MM/yyyy");
dateChooser.setBounds(48, 68, 102, 20);
JTextFieldDateEditor editor = (JTextFieldDateEditor) dateChooser.getDateEditor();
editor.setEditable(false);
frmCodfiscextractor.getContentPane().add(dateChooser);
Also, I put this at the end of the class :
frmCodfiscextractor.repaint();
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.alee.laf.WebLookAndFeel;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class demo {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
demo window = new demo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public demo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setDateFormatString("d/MM/yyyy");
dateChooser.setBounds(74, 193, 181, 30);
JTextFieldDateEditor editor=(JTextFieldDateEditor)dateChooser.getDateEditor();
editor.setEditable(false);
frame.getContentPane().add(dateChooser);
frame.repaint();
}
}
Use latest version of JCalender. It is correct working in my case.

Why i can't change variable in JLabel?

i just wanna count in jlabel. i think, I have tried all the solutions posted in the site but i couldn't find out any solution. I am a beginner, have one month to learn Java. I am sorry if my question is too stupid.
package asdf;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JLabel;
public class asd extends JFrame implements ActionListener {
int a=0; // variable
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asd window = new asd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public asd() {
super();
Timer time=new Timer(1000, this);
time.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(20, 11, 137, 111);
frame.getContentPane().add(panel);
panel.setLayout(null);
***JLabel Jtable = new JLabel();
Jtable.setBounds(0, 25, 127, 58);
Jtable.setText("" + a);
panel.add(Jtable);***
System.out.println(a); //it is counting on console but in Jlabel variable is not.
}
#Override
public void actionPerformed(ActionEvent arg0) {
a++;
initialize();
}
}
i just wanna count in jlabel. i think, I have tried all the solutions posted in the site but i couldn't find out any solution. I am a beginner, have one month to learn Java. I am sorry if my question is too stupid.
You shouldn't be initializing a new frame and all the components on every action performed, you should only be updating the text of the label. You can do this via JLabel.setText https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setText(java.lang.String)
int a = 0; // variable
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
asd window = new asd();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public asd() {
super();
Timer time=new Timer(1000, this);
time.start();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private JLabel label;
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(20, 11, 137, 111);
frame.getContentPane().add(panel);
panel.setLayout(null);
label = new JLabel();
label.setBounds(0, 25, 127, 58);
label.setText("" + a);
panel.add(label);
System.out.println(a); //it is counting on console but in Jlabel variable is not.
}
#Override
public void actionPerformed(ActionEvent arg0) {
a++;
label.setText("" + a);
}

Why is my java application blurry? [duplicate]

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.
This is what I wish to make
This is what is being showed when I run my java application. (see the text on my button and the text in text box is java)
I am using Eclipse Luna on Windows 7.
PS: My labels are blurry in Java was not of any help
public class DownloadManager {
private JFrame frame;
private JTable table;
private JTextField txtUrl;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DownloadManager window = new DownloadManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DownloadManager() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 752, 514);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
table.setBounds(47, 190, 629, 250);
frame.getContentPane().add(table);
txtUrl = new JTextField();
txtUrl.setBounds(47, 84, 391, 34);
frame.getContentPane().add(txtUrl);
txtUrl.setColumns(10);
JButton btnDownload = new JButton("Download");
btnDownload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnDownload.setBounds(534, 78, 99, 47);
frame.getContentPane().add(btnDownload);
}
}
EDIT:
Solution suggested by Swing rendering appears broken in JDK 1.8, correct in JDK 1.7 i.e. changing the energy control settings of NVIDIA GeForce 630M to maximum performance did not help.
Use a layout manager to mitigate the artifacts shown above. The example below nests one JPanel in another.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class DownloadManager {
private JFrame frame;
private JTable table;
private JTextField txtUrl;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DownloadManager window = new DownloadManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DownloadManager() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = new JTable() {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
};
frame.add(new JScrollPane(table), BorderLayout.CENTER);
txtUrl = new JTextField(12);
txtUrl.setColumns(10);
JButton btnDownload = new JButton("Download");
btnDownload.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
}
});
JPanel panel = new JPanel();
panel.add(txtUrl);
panel.add(btnDownload);
frame.add(panel, BorderLayout.NORTH);
frame.pack();
}
}

java draw image over another

Hey I am trying to make a basic launcher and for the design I got an image. I want to make the exit button to be shown over the launcher image so you can see it. I dont know how I do it so I need help with it. Here is my code:
package launcher;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
*
* #author Daniel <Skype: daniel.gusdal>
*
* Current Date: 2. feb. 2014 Current Time: 21:46:52
* Project: 742 client. File Name: Launcher.java
*
*/
public class Launcher extends JFrame {
/**
* Generated serialVersionUID
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
static Point mouseDownCompCoords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mouseDownCompCoords = null;
final Launcher frame = new Launcher();
frame.setResizable(true);
frame.setUndecorated(true);
frame.setBackground(new Color(0, 255, 0, 0));
frame.setVisible(true);
frame.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}
public void mousePressed(MouseEvent e) {
mouseDownCompCoords = e.getPoint();
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
});
frame.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x
- mouseDownCompCoords.x, currCoords.y
- mouseDownCompCoords.y);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Launcher() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 841, 593);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Design = new JLabel("New label");
Design.setIcon(new ImageIcon(getClass().getResource("Launcher3.png")));
Design.setBounds(-158, -22, 1047, 592);
contentPane.add(Design);
ImageIcon img = new ImageIcon(getClass().getResource(
"Playnow.png"));
final JButton Playnow = new JButton(img);
Playnow.setBackground(null);
Playnow.setOpaque(false);
Playnow.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
Playnow.setIcon(new ImageIcon(getClass().getResource(
"PlaynowHover.png")));
}
#Override
public void mouseClicked(MouseEvent e) {
Playnow.setIcon(new ImageIcon(getClass().getResource(
"PlaynowHover.png")));
System.out.println(Playnow.getIcon());
}
#Override
public void mouseExited(MouseEvent e) {
Playnow.setIcon(new ImageIcon(getClass().getResource(
"Playnow.png")));
}
});
Playnow.setBounds(258, 442, 301, 46);
contentPane.add(Playnow);
final JButton Exit = new JButton();
Exit.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(Exit.getIcon());
System.exit(0);
}
});
Exit.setIcon(new ImageIcon(Launcher.class.getResource("Exit.png")));
Exit.setOpaque(false);
Exit.setBounds(766, 59, 21, 21);
contentPane.add(Exit);
}
}
Don't set bounds. Learn to use Layout Managers.
Paint the image on a JPanel instead of using a JLabel
Put the JButton on the JPanel. Give that JPanel a GridBadLayout
Override the getPreferredSize() of the JPanel.
To switch between images you can use a flag like boolean icon1, icon2, icon3;. and repaint the JPanel the mouseXxx. Something like this
public void mouseClicked(MouseEvent e) {
icon1 = true;
icon2 = false;
icon3 = false;
imagePanel.repaint();
}
....
protected void paintComponent(Graphics g) {
if (icon1) {
g.drawImage(image1, .....);
}
if (icon2) { ... }
if (icon3) { ... }
}
pack() your frame.
Use Java naming convention. variables start with lower case letters.
Here's an example
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.*;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestButtonOverImage {
public TestButtonOverImage() {
JFrame frame = new JFrame("Test Card");
frame.add(new ImagePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestButtonOverImage();
}
});
}
public class ImagePanel extends JPanel {
BufferedImage img;
public ImagePanel() {
setLayout(new GridBagLayout());
add(new JButton("StackOverflow Button"));
try {
img = ImageIO.read(new URL("http://d8u1nmttd4enu.cloudfront.net/designs/logo-stackoverflow-logo-design-99designs_447080~36d200d82d83d7b2e738cebd2a48de07180cef3a_largecrop"));
} catch (MalformedURLException ex) {
Logger.getLogger(TestButtonOverImage.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestButtonOverImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 100, 100, 300, 300, this);
}
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}
}

How to add scrollbar in JFrame with null layout?

I want to add a vertical scroll-bar on my JFrame with null layout.
Is it possible or not? please help!
Just set the JScrollPane as ContentPane for JFrame as it is described here:
public class TabbedPaneTest {
public static void main(String [] a) {
final JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.setContentPane(pane);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
}
}
In eclipse IDE you can use following code
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
public class Test {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane jsp = new JScrollPane(container);
container.setPreferredSize(new Dimension(500, 250));
container.setLayout(null);
JLabel lblHelloWorld = new JLabel("Hello World");
lblHelloWorld.setBounds(10, 11, 101, 14);
container.add(lblHelloWorld);
frame.getContentPane().add(jsp);
}
}

Categories