Why is my java application blurry? [duplicate] - java

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();
}
}

Related

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);
}

Java add components on WindowBuilder built GUI

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.

label unclear text when change its text

unfortunately I can't handle the change of txt when the button is clicked, I try to write a txt and overtime that I click the button, this txt value should change and allotting seems right, the only problem is that the printed number is not obvious and it seems some part of previous txt remains with it.
package berGenerator;
import java.awt.EventQueue;
public class sscce {
private JFrame frame;
private final Action action = new SwingAction();
private static int i = 555;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
sscce window = new sscce();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public sscce() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 550, 401);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton Next = new JButton("Next");
Next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
Next.setAction(action);
Next.setBounds(167, 290, 198, 64);
frame.getContentPane().add(Next);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "Next Timeslot/scheduler");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
i = i+1;
frame.getContentPane().validate();
frame.getContentPane().repaint();
String from = String.valueOf(i);
System.out.println("sw is: "+from);
JTextArea TextArea11 = new JTextArea("");
TextArea11.setText(from);
TextArea11.setForeground(Color.GREEN);
TextArea11.setBounds(6, 66, 87, 16);
frame.getContentPane().add(TextArea11);
}
}
}
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify.
Layout managers are fundamental to the Swing API, you should make the time to learn how to use them, they will solve more problems than you think they create.
See Laying Out Components Within a Container for more details.
You're creating multiple instances of JTextArea and adding to the frame, but you're not removing any, you're running into a potential z-ordering problem at best and a major resource management issue at worst.
Instead, simply create a single instance of JTextArea, add it to the frame (just like you did your button) and simply update it when the Action is triggered, for example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import static javax.swing.Action.SHORT_DESCRIPTION;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
private JFrame frame;
private final Action action = new SwingAction();
private static int i = 555;
private JTextArea textArea;
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();
}
}
});
}
public Test() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea(10, 20);
textArea.setText(String.valueOf(i));
frame.add(new JScrollPane(textArea));
JButton next = new JButton("Next");
next.setAction(action);
frame.getContentPane().add(next, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "Next Timeslot/scheduler");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
i = i + 1;
String from = String.valueOf(i);
System.out.println("sw is: " + from);
textArea.setText(from);
textArea.setForeground(Color.GREEN);
}
}
}

JFreeChart dial in NetBeans JButton

I'm trying to make a dial from JFreeChart work while I press a JButton made in NetBeans. The problem is that although the code seems ok while outside the JButton, it gives me errors in the program when I put it inside.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
}
public class DemoChartProblem {
private final DefaultValueDataset dataset = new DefaultValueDataset(50);
private final JFrame frame = new JFrame();
public void main(String[] args) throws Exception {
new DemoChartProblem();
}
public DemoChartProblem() {
frame.setPreferredSize(new Dimension(300, 300));
frame.add(buildDialPlot(0, 30, 5));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
}
private ChartPanel buildDialPlot(int minimumValue, int maximumValue,
int majorTickGap) {
DialPlot plot = new DialPlot(dataset);
plot.setDialFrame(new StandardDialFrame());
plot.addLayer(new DialValueIndicator(0));
plot.addLayer(new DialPointer.Pointer());
StandardDialScale scale = new StandardDialScale(minimumValue,
maximumValue, -120, -300, majorTickGap, majorTickGap - 1);
scale.setTickRadius(0.88);
scale.setTickLabelOffset(0.20);
plot.addScale(0, scale);
return new ChartPanel(new JFreeChart(plot));
}
}
I guess it's something obvious, but I dont seem to find the problem; any help is appreciated.
Several issues merit attention in your example:
Use Action to encapsulate functionality; this example increments the dataset value with each invocation.
frame.add(new JButton(new AbstractAction("Update") {
#Override
public void actionPerformed(ActionEvent e) {
dataset.setValue(dataset.getValue().intValue() + 1);
}
}), BorderLayout.SOUTH);
The main() method must be static.
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
Consider these alternate ways to control the initial chart size.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;
public class DemoChartProblem {
private final DefaultValueDataset dataset = new DefaultValueDataset(41);
private final JFrame frame = new JFrame();
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(() -> {
new DemoChartProblem();
});
}
public DemoChartProblem() {
frame.add(buildDialPlot(0, 30, 5));
frame.add(new JButton(new AbstractAction("Update") {
#Override
public void actionPerformed(ActionEvent e) {
dataset.setValue(dataset.getValue().intValue() + 1);
}
}), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
private ChartPanel buildDialPlot(int minimumValue, int maximumValue,
int majorTickGap) {
DialPlot plot = new DialPlot(dataset);
plot.setDialFrame(new StandardDialFrame());
plot.addLayer(new DialValueIndicator(0));
plot.addLayer(new DialPointer.Pointer());
StandardDialScale scale = new StandardDialScale(minimumValue,
maximumValue, -120, -300, majorTickGap, majorTickGap - 1);
scale.setTickRadius(0.88);
scale.setTickLabelOffset(0.20);
plot.addScale(0, scale);
return new ChartPanel(new JFreeChart(plot)) {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
};
}
}

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