Java JButton it is not running truly - java

I added a jbutton with eclipse but it is not seeing correctly. How do I fix it?
this is screen shot:
public class GUITest // test class
{
// block the warnings
public static void main(String[] args) //main
{
System.out.println("start of main");
/* stackoverflow want to add more details */
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Click here");
panel.add(button);
button.addActionListener(new Action());
System.out.println("end of main");
/* stackoverflow want to add more details */
}
public static class Action implements ActionListener //actionlistener
{
public void actionPerformed (ActionEvent e)
{ /* stackoverflow want to add more details */
JFrame frame2 = new JFrame("Clicked");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("You clicked me!");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label); // some more details
}
}
}

I have used this and It will working fine. Can you please check your Graphics Driver or other things.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Ashwin Parmar
*/
public class GUITest {
public static void main(String[] args) {
System.out.println("Start");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Click here");
panel.add(button);
button.addActionListener(new Action());
frame.setVisible(true);
System.out.println("End");
}
public static class Action implements ActionListener{
#Override
public void actionPerformed(ActionEvent ae) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
JFrame frame2 = new JFrame("Clicked");
frame2.setSize(200,200);
JLabel label = new JLabel("You have clicked me!");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
frame2.setVisible(true);
}
}
}

Always create and modify the UI from within the context of the Event Dispatching Thread
Wherever possible, call setVisible last, after you have established the basic UI. You will be required to call revalidate and repaint if you add components after the window is made visible.
For example...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("Click here");
panel.add(button);
button.addActionListener(new Action());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
Do the same thing within your ActionListener.
See Initial Threads for more details

Related

how to call JFrame when I put the mouse on JLabel And how to close the Same JFrame When i remove the mouse from JLabel?

In my program I try to show the JFrame when I put the mouse on JLabel and close the JFrame when I remove the mouse from JLabel.
How can I do it?
I tried below way, but I am getting flashing windows continuously(popup and close continuously)
public class NewJFrame extends javax.swing.JFrame {
NewJFrame1 frame = new NewJFrame1();
public NewJFrame() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//======================================================================
jLabel1.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
frame.setVisible(true);
}
});
jLabel1.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent e)
{
frame.setVisible(false); //Hide window
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Testing ");
//======================================================================
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
public javax.swing.JLabel jLabel1;
}
In between these lines//============ I have the main code
How to display JFrame when I put mouse on JLabel and how to close the JFrame when I remove the mouse from the JLabel ?
When I remove the below code , and when I place the mouse on JLabel I am getting JFrame popup , but I need to close the JFrame popup when I remove the mouse from JLabel.
jLabel1.addMouseListener(new MouseAdapter()
{
public void mouseExited(MouseEvent e)
{
frame.setVisible(false); //Hide window
}
});
Your code is completly wrong, cannot read and understand cleary what do you try.
The problem is you create the new JFrame over your label, so the focus of your mouse changes to the new JFrame and the listener of JLabel say that the new window to dissapear and again and again. To fix this, set the new position out the current window.
Here is the code of the main method:
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Main ex = new Main();
ex.setVisible(true);
});
}
Then add components to panel with:
JPanel panel = new JPanel();
this.add(panel);
JLabel jLabel1 = new JLabel("Label");
JTextField jTextField1 = new JTextField("Field");
JButton jButton1 = new JButton("Button");
panel.add(jLabel1);
panel.add(jTextField1);
panel.add(jButton1);
Then create the new splashing JFrame and set the current one as well. Since your main class extends JFrame, you can use the keyword this.
JFrame frame = new JFrame();
frame.setSize(300, 200);
frame.setVisible(false);
this.setTitle("Title");
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
And finally let the listeners do the job from your code.
You need to add the JLabel (as well as the other components) to the JFrame.
Once you do that, and the JLabel shows in the JFrame, you can use the JLabel's listeners.
EDITED:
A. Here is you code with some minimal changes to show / hide a 2nd JFrame:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class NewJFrame extends javax.swing.JFrame {
javax.swing.JFrame frame ;
//this will not compile and not needed
//NewJFrame1 frame = new NewJFrame1();
public NewJFrame() {
initComponents();
}
private void initComponents() {
frame = getAJFrame();
//set a layout manger
getContentPane().setLayout(new GridLayout(3, 1));
setLocationRelativeTo(null);
jLabel1 = new javax.swing.JLabel();
//add component
getContentPane().add(jLabel1);
JTextField jTextField1 = new javax.swing.JTextField();
//add component
getContentPane().add(jTextField1);
JButton jButton1 = new javax.swing.JButton();
//add component
getContentPane().add(jButton1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.addMouseListener(new MouseAdapter()
{
#Override
public void mouseEntered(MouseEvent e)
{
frame.setVisible(true);
frame.pack();
}
});
jLabel1.addMouseListener(new MouseAdapter()
{
#Override
public void mouseExited(MouseEvent e)
{
frame.setVisible(false); //Hide window
}
});
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Testing ");
//set a size to the frame
setPreferredSize(new Dimension(200,100));
pack();
}
/**
*#return
*/
private JFrame getAJFrame() {
JFrame f = new JFrame("A JFrame");
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
f.getContentPane().setPreferredSize(new Dimension(150,150));
f.getContentPane().setBackground(Color.BLUE);
setVisible(false);
pack();
return f;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
NewJFrame frame = new NewJFrame();
frame.setVisible(true);
}
});
}
public javax.swing.JLabel jLabel1;
}
B. If you are trying to show / hide NewJFrame itself: you will not be able to make the JFrame visible again, using mouseEntered. When the JFrame (and JLabel within it) are setVisible(false) it will not generate mouse events.
The frame becomes invisible when mouse exits the JLabel. You will need to make it visible again using a different technique.
C. See The Use of Multiple JFrames: Good or Bad Practice?

JFrame class not working in Main

I'm trying to write a Java program that declares a JFrame class and creates an object of that class in the main. For the life of me I cannot get the JFrame title, JPanel, and JButtons to appear when I create the "MyButtons" object in the main. All I get is a blank JPanel.
import java.awt.*;
import javax.swing.*;
import java.util.*;
class MyButtons extends JFrame
{
public MyButtons()
{
JFrame frame = new JFrame("MyButtons");
JPanel panel = new JPanel();
frame.add(panel);
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
panel.add(b1);
panel.add(b2);
}
}
class TestMyButtons
{
public static void main(String [] args)
{
MyButtons go = new MyButtons();
go.setSize(200,75);
go.setLocation(200,300);
go.setVisible(true);
go.setResizable(true);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
As you extend JFrame so you don't need to create another JFrame instance at constructor. Simply add panel to MyButtons which already inherited JFrame.
public MyButtons(){
//JFrame frame = new JFrame("MyButtons"); Commentted this
JPanel panel = new JPanel();
//frame.add(panel); And this
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
panel.add(b1);
panel.add(b2);
add(panel); // Add panel to MyButtons frame
}
You're making two instances of JFrame, MyButtons and JFrame frame = new JFrame("MyButtons");, to which you are adding all your components...
It's very rare that you would ever need to extend directly from a top level container like JFrame, instead, extend from something like JPanel and then add that to an instance of JFrame instead...
MyButtons...
import javax.swing.JButton;
import javax.swing.JPanel;
class MyButtons extends JPanel {
public MyButtons() {
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
add(b1);
add(b2);
}
}
TestMyButtons ...
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
class TestMyButtons {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyButtons());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

I want a JLabel to appear after pressing a JButton

I need help with this code, I'm trying to make a simple cookie clicker type game, I have most the code done, but for some reason, when I try to add the JLabel to the frame, it creates an error, I was hoping one of you guys could help me out, I'm fairly new to Java, thanks for the help!
//Variables
static int clicked = 0;
private FlowLayout layout;
private Container container;
public static void main(String [] args) {
//Declaring the buttons, panels, etc...
JButton button = new JButton("Click");
JPanel panel = new JPanel();
panel.add(button);
final JFrame frame = new JFrame("Button Pressed");
frame.setSize(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(panel);
//Action Listener Code
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
clicked++;
System.out.println("Button pressed " + clicked + " times!");
}
}
}
You can add an JLabel then update its text when button is clicked.
Note: call JFrame.setVisible(true) in the end when all the component is added.
sample code:
// Declaring the buttons, panels, etc...
JButton button = new JButton("Click");
final JLabel label = new JLabel();
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
clicked++;
label.setText("Button pressed " + clicked + " times!");
}
});
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
final JFrame frame = new JFrame("Button Pressed");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
Find more examples here and here
The basic principle is relatively easy. In order to add something to something else, you first need to have access (or a reference to) the thing you want to add to.
While there are a number of ways you might achieve this, the simplest might be to use an instance/class field. This field would then be accessible from anywhere within the class, for example
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ClickTest {
public static void main(String[] args) {
new ClickTest();
}
private JPanel panel;
public ClickTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
//Declaring the buttons, panels, etc...
JButton button = new JButton("Click");
panel = new JPanel();
panel.add(button);
final JFrame frame = new JFrame("Button Pressed");
frame.setSize(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(panel);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
panel.add(new JLabel("You clicked me"));
panel.revalidate();
}
});
}
});
}
}
Take a look at Creating a GUI With JFC/Swing and Understanding Class Members for more details

java swing panel problems

I am having problems adding a panel to my main JFrame and hiding it right away, only making it visilble when a button it pressed. here is my code. Looking for any insight as to what the problem is. Also the label I try to add to the panel doesnt show up either.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cis2430_a4;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* #author Tristan
*/
public class MainWindow extends JFrame implements ActionListener{
public static final int WIDTH = 600;
public static final int HEIGHT = 700;
private JPanel addPanel;
public MainWindow()
{
super("Day Planner");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
add(intro1, BorderLayout.NORTH);
JLabel intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
add(intro2, BorderLayout.CENTER);
JMenu commands = new JMenu("Commands");
JMenuItem addOption = new JMenuItem("Add");
addOption.addActionListener(this);
commands.add(addOption);
JMenuItem searchOption = new JMenuItem("Search");
searchOption.addActionListener(this);
commands.add(searchOption);
JMenuBar menuBar = new JMenuBar();
menuBar.add(commands);
setJMenuBar(menuBar);
JButton button = new JButton("Add");
button.addActionListener(this);
add(button, BorderLayout.SOUTH);
//add panel
addPanel = new JPanel();
addPanel.setLayout(new BorderLayout());
addPanel.setSize(600,400);
addPanel.setBackground(Color.CYAN);
addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
add(addPanel, BorderLayout.CENTER);
addPanel.setVisible(false);
}
#Override
public void actionPerformed(ActionEvent ae)
{
/*String menuChoice = ae.getActionCommand();
if (menuChoice.equals("Add")){
addPanel.setVisible(true);
}*/
add(addPanel);
//addPanel.setVisible(true);
}
}
I have no issue with your example.
You may want to...
1- Make sure you've launched your UI in the context of the Event Dispatching Thread
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MainWindow frame = new MainWindow();
frame.setVisible(true);
}
});
}
2- Try calling repaint after addPanel.setVisible(true)
3- Try calling invalidate after addPanel.setVisible(true) but before repaint if that doesn't work.
Much better solution is to use Card Layout for this kind of work
UPDATED
After spending some time reading through the code, what I think you seem to be concerned about is the fact that you're "intro" label isn't showing up...
This easily explained. Only one component can exists at any given position within a BorderLayout, so when you add you addPanel, even though it's invisible, it will clobber the intro2 label (effectively removing it from the container).
Below is an example using CardLayout
public class CardWindow extends JFrame implements ActionListener {
public static final int WIDTH = 600;
public static final int HEIGHT = 700;
private JPanel addPanel;
private JPanel cardPane;
private CardLayout cardLayout;
private final JLabel intro2;
public CardWindow() {
super("Day Planner");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
cardPane = new JPanel((cardLayout = new CardLayout()));
add(cardPane, BorderLayout.CENTER);
JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
add(intro1, BorderLayout.NORTH);
intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
cardPane.add(intro2, "intro");
JMenu commands = new JMenu("Commands");
JMenuItem addOption = new JMenuItem("Add");
addOption.addActionListener(this);
commands.add(addOption);
JMenuItem searchOption = new JMenuItem("Search");
searchOption.addActionListener(this);
commands.add(searchOption);
JMenuBar menuBar = new JMenuBar();
menuBar.add(commands);
setJMenuBar(menuBar);
JButton button = new JButton("Add");
button.addActionListener(this);
add(button, BorderLayout.SOUTH);
//add panel
addPanel = new JPanel();
addPanel.setLayout(new BorderLayout());
addPanel.setSize(600, 400);
addPanel.setBackground(Color.CYAN);
addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
addPanel.setVisible(false);
cardPane.add(addPanel, "Add");
cardLayout.show(cardPane, "intro");
}
#Override
public void actionPerformed(ActionEvent ae) {
String menuChoice = ae.getActionCommand();
System.out.println(menuChoice);
if (menuChoice.equals("Add")) {
cardLayout.show(cardPane, "Add");
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
CardWindow frame = new CardWindow();
frame.setVisible(true);
}
});
}
}
Labels are showing up because you have added a panel after adding the labels on the frame so basically the panels are overlapping the labels.
Also to show different panels you can use
panel.setVisible(true); //For the panel you want to show and false for others
or you can use CardLayout which makes panels as cards and shows one of them at a time.
Just edited the code a little but it seems to work -
public class MainWindow extends JFrame implements ActionListener{
public static final int WIDTH = 600;
public static final int HEIGHT = 700;
private JPanel addPanel;
public static void main(String[] args) {
MainWindow mainWindow = new MainWindow();
mainWindow.setVisible(true);
}
public MainWindow()
{
super("Day Planner");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JLabel intro1 = new JLabel("Welcome to your Day Planner", JLabel.CENTER);
add(intro1, BorderLayout.NORTH);
JLabel intro2 = new JLabel("Please choose an option from the menu bar above.", JLabel.CENTER);
add(intro2, BorderLayout.CENTER);
JMenu commands = new JMenu("Commands");
JMenuItem addOption = new JMenuItem("Add");
addOption.addActionListener(this);
commands.add(addOption);
JMenuItem searchOption = new JMenuItem("Search");
searchOption.addActionListener(this);
commands.add(searchOption);
JMenuBar menuBar = new JMenuBar();
menuBar.add(commands);
setJMenuBar(menuBar);
JButton button = new JButton("Add");
button.addActionListener(this);
add(button, BorderLayout.SOUTH);
//add panel
addPanel = new JPanel();
addPanel.setLayout(new BorderLayout());
addPanel.setSize(600,400);
addPanel.setBackground(Color.CYAN);
addPanel.add(new JLabel("add panel"), BorderLayout.CENTER);
add(addPanel, BorderLayout.CENTER);
addPanel.setVisible(false);
}
#Override
public void actionPerformed(ActionEvent ae)
{
String menuChoice = ae.getActionCommand();
if (menuChoice.equals("Add")){
addPanel.setVisible(true);
}
add(addPanel);
//addPanel.setVisible(true);
}
}

java - How would I dynamically add swing component to gui on click?

What I am looking to do is a similar principle to adding attachments to emails, you can click a button and a new browse box would open increasing the number of separate attachments you can have.
I'm fairly new so if someone could point me towards an example?
Sample code to add Buttons on the fly dynamically.
panel.add(new JButton("Button"));
validate();
Full code:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
public class AddComponentOnJFrameAtRuntime extends JFrame implements ActionListener {
JPanel panel;
public AddComponentOnJFrameAtRuntime() {
super("Add component on JFrame at runtime");
setLayout(new BorderLayout());
this.panel = new JPanel();
this.panel.setLayout(new FlowLayout());
add(panel, BorderLayout.CENTER);
JButton button = new JButton("CLICK HERE");
add(button, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
this.panel.add(new JButton("Button"));
this.panel.revalidate();
validate();
}
public static void main(String[] args) {
AddComponentOnJFrameAtRuntime acojfar = new AddComponentOnJFrameAtRuntime();
}
}
Resource
public static void main(String[] args) {
final JFrame frame = new JFrame("Test");
frame.setLayout(new GridLayout(0, 1));
frame.add(new JButton(new AbstractAction("Click to add") {
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.add(new JLabel("Bla"));
frame.validate();
frame.repaint();
}
});
}
}));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
Component was not visible until setSize() was called:
component.setSize(100,200);
jPanel.add(component);
jPanel.revalidate();
jPanel.repaint();
panel.add(button);
panel.revalidate();
panel.repaint();
Java : Dynamically add swing components
for Example : count=3
//Java Swing: Add Component above method
public void dya_addcomp(int count)
{
//Dynamicaly Delete Image_icon
BufferedImage Drop_Tablefield = null;
try {
Drop_Tablefield = ImageIO.read(this.getClass().getResource("/images/drop.png"));
} catch (IOException ex) {
msg(" Error: drop and edit icon on Table, "+ex);
}
//count Items: 3 times for loop executed..
for(int i=0;i<count;i++)
{
//cnt++;
//lblcount.setText("Count : "+cnt);
JTextField txtcolnm=new JTextField("",20);
JComboBox cmbtype=new JComboBox();
JTextField txtcolsize=new JTextField("",20);
JButton Drop_Table_Field = new JButton(new ImageIcon(Drop_Tablefield));
cmbtype.addItem("INTEGER"); cmbtype.addItem("FLOAT");
cmbtype.addItem("STRING"); cmbtype.addItem("BOOLEAN");
colnamepanel.add(txtcolnm); colnamepanel.add(cmbtype);
colnamepanel.add(txtcolsize); colnamepanel.add(Drop_Table_Field);
colnamepanel.setAutoscrolls(true);
//refresh panel
colnamepanel.revalidate();
colnamepanel.repaint();
//set the layout on Jpanel
colnamepanel.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
}//end for loop
}//end method

Categories

Resources