Why can't I insert my JTextField inside my JPanel? - java

My JTextField isn't showing on, only the paintComponent
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
private JTextField txt;
public Painel(){
super();
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setLayout(new FlowLayout());
txt = new JTextField();
txt.setBounds(400, 300, 50, 20);
}

You have to set the number of columns in your text field or give a default text to it. The following code should work for you. I have updated the previous answer to use the Gridbag layout. However still you need to set the number of columns in JTextField to render it.
public class TestFrame extends JFrame {
public static void main(String[] args) {
new TestFrame();
}
private TestFrame() throws HeadlessException {
super();
this.setLocationByPlatform(true);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 100, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JTextField textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}
Hope this helps. Happy coding !

Related

Adding Data to JTable from different Class

SO I have this Test Client which will run the program and i want this class to populate a JTable in a JPanel class named StartScreenPlayerPanel.
I've tried several methods which failed.
TestClient Class
import view.MainFrame;
import view.StartScreenPlayerPanel;
public class TestClientGUI {
private static final Logger logger = Logger.getLogger(SimpleTestClientGUI.class.getName());
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
MainFrame mainframe;
StartScreenPlayerPanel startScreenPlayerPanel;
public static void main(String args[]) {
final GameEngine gameEngine = new GameEngineImpl();
//GUI - new MainFrame
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
System.out.println(screenSize);
frame.setMinimumSize(new Dimension(screenSize.width/2, screenSize.height/2));
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
The Class with the JTable - "StartScreenPlayerPanel"
public class StartScreenPlayerPanel extends JPanel {
MainFrame mainframe;
//private JTable table;
private DefaultTableModel model = new DefaultTableModel();
public StartScreenPlayerPanel() {
setLayout(new BorderLayout());
JTable table = new JTable(model);
table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 16));
model.addColumn("Player");
model.addColumn("Initial Points");
//model.addRow(new Object[]{"v1", "v2"});
add(table.getTableHeader(), BorderLayout.NORTH);
add(table, BorderLayout.CENTER);
}
public void setModel(String c1, String c2) {
model.addRow(new Object[]{c1, c2});
model.fireTableDataChanged();
}
public DefaultTableModel getModel() {
return this.model;
}
}
What I tried So far ;
//Add players to GUI
StartScreenPlayerPanel startScreenPlayerPanel = new StartScreenPlayerPanel();
startScreenPlayerPanel.setModel("CoinMaster","TheLoser");
startScreenPlayerPanel.getModel().addRow(new Object[]{"CoinMaster", "TheLoser"});
startScreenPlayerPanel.getModel().setValueAt("TheLoser", 2, 2);
Nothing I tried worked, except doing it in the same class
Thanks For your help.
Edit - Added code for MainFrame:
public class MainFrame extends JFrame {
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private StartScreenPlayerPanel startScreenPlayerPanel;
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
System.out.println(screenSize);
frame.setMinimumSize(new Dimension(screenSize.width/2, screenSize.height/2));
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
startScreenPlayerPanel = new StartScreenPlayerPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, (screenSize.width * 2 / 3), (screenSize.height * 2 / 3));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{1200, 0};
gbl_contentPane.rowHeights = new int[]{74, 0, 446, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JLabel lblTitle = new JLabel("The Coin Game",SwingConstants.CENTER);
lblTitle.setFont(new Font("Arial", Font.PLAIN, (int)screenSize.width/30));
GridBagConstraints gbc_lblTitle = new GridBagConstraints();
gbc_lblTitle.gridwidth = 2;
gbc_lblTitle.insets = new Insets(0, 0, 5, 0);
gbc_lblTitle.anchor = GridBagConstraints.NORTH;
gbc_lblTitle.fill = GridBagConstraints.HORIZONTAL;
gbc_lblTitle.gridx = 0;
gbc_lblTitle.gridy = 0;
contentPane.add(lblTitle, gbc_lblTitle);
JPanel StartScreenBtnPanel = new JPanel();
GridBagConstraints gbc_StartScreenBtnPanel = new GridBagConstraints();
gbc_StartScreenBtnPanel.gridwidth = 0;
gbc_StartScreenBtnPanel.insets = new Insets(0, 0, 5, 0);
gbc_StartScreenBtnPanel.fill = GridBagConstraints.BOTH;
gbc_StartScreenBtnPanel.gridx = 0;
gbc_StartScreenBtnPanel.gridy = 1;
contentPane.add(StartScreenBtnPanel, gbc_StartScreenBtnPanel);
StartScreenBtnPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnAddPlayer = new JButton("Add Player");
btnAddPlayer.setFont(new Font("Tahoma", Font.PLAIN, 16));
StartScreenBtnPanel.add(btnAddPlayer);
JButton btnStartGame = new JButton("Start Game");
btnStartGame.setFont(new Font("Tahoma", Font.PLAIN, 16));
StartScreenBtnPanel.add(btnStartGame);
GridBagConstraints gbc_playerPanel = new GridBagConstraints();
gbc_playerPanel.gridwidth = 2;
gbc_playerPanel.insets = new Insets(0, 0, 5, 0);
gbc_playerPanel.fill = GridBagConstraints.BOTH;
gbc_playerPanel.gridx = 0;
gbc_playerPanel.gridy = 2;
contentPane.add(startScreenPlayerPanel, gbc_playerPanel);
}
public StartScreenPlayerPanel getStartScreenPlayerPanel() {
return startScreenPlayerPanel;
}
}

How to resize JPanel when JFrame is maximized?

I am new to java swing, recently I try to create a swing app to format text.
When I click the maximum button, the text panel's length does not resize, and the middle panel takes large space.
And seems setResizable(false) does not work
Code
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private JTextArea fromTextArea;
private JTextArea toTextArea;
public MainFrame() {
super("jFormatter");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
fromTextArea = createTextArea();
lines.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.LIGHT_GRAY));
lines.setEditable(false);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
lines.setFont(f);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
fromTextAreaScrollPanel.getViewport().add(fromTextArea);
fromTextAreaScrollPanel.setRowHeaderView(lines);
mainPanel.add(fromTextAreaScrollPanel);
// control panel
mainPanel.add(createCtrlPanel());
toTextArea = createTextArea();
mainPanel.add(createTextAreaPanel(toTextArea));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private JPanel createCtrlPanel() {
final JComboBox jComboBox = new JComboBox(formatters.keySet().toArray());
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JButton fmtButton = new JButton("Format >>");
JPanel ctrPanel = new JPanel(new GridBagLayout());
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
//gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
return ctrPanel;
}
private JScrollPane createTextAreaPanel(JTextArea textArea) {
JScrollPane fromTextAreaScrollPanel = new JScrollPane(textArea);
//fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
return fromTextAreaScrollPanel;
}
private JTextArea createTextArea() {
JTextArea textArea = new JTextArea(20, 40);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
textArea.setFont(f);
//fromTextArea.setLineWrap(true);
//textArea.setBackground(Color.LIGHT_GRAY);
textArea.setMargin(new Insets(0, 10, 0, 10));
return textArea;
}
public static void main(String[] args) {
new MainFrame();
}
}
result:
You should probably use BorderLayout or GridBagLayout for this. BoxLayout just lays out components one after the other at their preferred size. It doesn't make any attempt to resize the components or make them fill their parent.
Try to make a layout like this:
Code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
gbc_scrollPane.weightx=1;
contentPane.add(scrollPane, gbc_scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 2;
gbc_panel.insets = new Insets(0, 0, 5, 5);
//gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 2;
gbc_comboBox.gridy = 0;
gbc_comboBox.weightx=0.0;
panel.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton(">>");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 1;
panel.add(btnNewButton, gbc_btnNewButton);
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 2;
gbc_scrollPane_1.gridy = 0;
gbc_scrollPane_1.weightx=1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
JTextArea textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
pack();
}
}

JLabel: setPreferredSize manually but getPreferredSize after setText

i want to set the size of a JLabel to a specific (minimum) size via setPreferredSize().
If the text inside the JLabel doesn't fit anymore, the JLabel should grow.
The problem now is:
after label.setPreferredSize(new Dimension(50,20));
i do setText("new long text"); but the size won't change
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class LabelTest
{
public static void createWindow()
{
JFrame frame = new JFrame();
JLabel label = new JLabel("Text");
System.out.println(label.getPreferredSize().width); // 25
label.setText("New long text");
System.out.println(label.getPreferredSize().width); // 77 - JLabel grows as expected
label.setText("Text");
System.out.println(label.getPreferredSize().width); // 25
label.setPreferredSize(new Dimension(50, 20));
System.out.println(label.getPreferredSize().width); // 50 - JLabel grows to given size as expected
label.setText("New long text");
System.out.println(label.getPreferredSize().width); // 50 - it should be 77 again
frame.add(label);
frame.setPreferredSize(new Dimension(200, 100));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createWindow();
}
});
}
}
So i need a solution to get the new preferred size when a text is set after i already set the preferred size manually.
If you need more information just ask.
Thanks in advance!
You can reach what you want using a LayoutManager
Below is a example with GridBag Layout
public class GridBagExample extends JFrame {
private JPanel contentPane;
private JLabel lblMyLabel;
private JTextField textField;
private JButton btnChangeMyLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
GridBagExample frame = new GridBagExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GridBagExample() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 94);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 344, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
lblMyLabel = new JLabel(" My Label");
GridBagConstraints gbc_lblMyLabel = new GridBagConstraints();
gbc_lblMyLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblMyLabel.anchor = GridBagConstraints.EAST;
gbc_lblMyLabel.gridx = 0;
gbc_lblMyLabel.gridy = 0;
contentPane.add(lblMyLabel, gbc_lblMyLabel);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
btnChangeMyLabel = new JButton("Change My Label");
btnChangeMyLabel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
lblMyLabel.setText("This is the new text of my JLabel");
}
});
GridBagConstraints gbc_btnChangeMyLabel = new GridBagConstraints();
gbc_btnChangeMyLabel.gridwidth = 2;
gbc_btnChangeMyLabel.gridx = 0;
gbc_btnChangeMyLabel.gridy = 1;
contentPane.add(btnChangeMyLabel, gbc_btnChangeMyLabel);
}
Hope it helps.
To set a minimum size, use setMinimumSize(Dimension). In most cases, it is better to place the JLabel in a container with a suitable layout manager, and to set size on it.
Return value of getPreferredSize() is automatically calculated until you set an explicit preferred size. After this, getPreferredSize() will be return with this specific size. If you reset preferred size (by setting it to null), then automatic size will be again available. In your case:
label.setText("Text");
System.out.println(label.getPreferredSize().width); // 25
label.setPreferredSize(new Dimension(50, 20));
System.out.println(label.getPreferredSize().width); // 50
label.setText("New long text");
System.out.println(label.getPreferredSize().width); // 50
label.setPreferredSize(null);
System.out.println(label.getPreferredSize().width); // 77

Too much space between components in Spring layout

I want to create a JFrame by hand and use spring layout to do this. But, my finally output is not good. The space between my rows is so much, and between my radio buttons too:
My code:
public final class NewUserFrame1 extends JFrame {
public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
pack();
}
public JPanel rowComponent() {
JPanel panel = new JPanel();
JLabel fnamelbl = new JLabel("First name");
JLabel lnamelbl = new JLabel("Last Name");
JLabel fntemp = new JLabel();
JLabel lntemp = new JLabel();
JTextField fntf = new JTextField(10);
JTextField lntf = new JTextField(10);
JLabel gndlnl = new JLabel("Gender");
JRadioButton malerb = new JRadioButton("Male");
JRadioButton femalerb = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(malerb);
bgroup.add(femalerb);
JLabel registnm = new JLabel("Registration ID is:");
JLabel showreglbl = new JLabel();
JLabel regtemp = new JLabel();
panel.add(fnamelbl);
panel.add(fntf);
panel.add(fntemp);
panel.add(lnamelbl);
panel.add(lntf);
panel.add(lntemp);
panel.add(gndlnl);
panel.add(malerb);
panel.add(femalerb);
panel.add(registnm);
panel.add(showreglbl);
panel.add(regtemp);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NewUserFrame1 newUserFrame1 = new NewUserFrame1();
}
});
}
}
Now:
Instead of calling setSize call pack on JFrame within your NewUserFrame1 constructor.
public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
//setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
pack();
}
Also change the parameters of SpringUtilities.makeCompactGrid method in following way:
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);//change yPad to 4 instead of 100. It sets the vertical height between two rows
Your code is not compilable (missing imports).
You wrote:
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 100);
The last argument is yPad. Change this to 10 (or lower value if you want), for example:
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 10);
But still - label will be to high etc., but it's a different issue. Keep playing with panel's size and your component's size.
In case of radio buttons - change
panel.add(malerb);
panel.add(femalerb);
To something like:
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radioPanel.add(malerb);
radioPanel.add(femalerb);
panel.add(radioPanel);
panel.add(new JLabel());
The last line is needed because you declared your layout to have 3 columns.

Java gif image consuming CPU and memory

in my application i do intensive use of animated gif images displayed in a JEditorPane (this is a chat).
Now i realyzed that the GIFs consume a lot of CPU (near 100% in some cases), and the memory used continued to increase indefinitely.
How to avoid this? Or, can you suggest another component to replace JEditorPane for better performance?
This is an example that can show the memory increase and the CPU usage.
public class TestCPU extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestCPU frame = new TestCPU();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestCPU() {
setIconImage(Toolkit.getDefaultToolkit().getImage(TestCPU.class.getResource("/images/asd.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
contentPane.add(scrollPane, gbc_scrollPane);
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
String html = "<html>";
for (int i = 0; i < 500; i++) {
html = html + "<img src="+ TestCPU.class.getResource("/images/asd.gif") + ">";
}
editorPane.setText(html);
scrollPane.setViewportView(editorPane);
}
}
the image used in the test

Categories

Resources