The second JFrame is blank and very small - java

I'm new to Swing and I'm having trouble replacing an existing JFrame. I initialize the first JFrame without a problem.
class GererAdgerent :
public class GererAdherent extends JFrame {
private JPanel contentPane;
static GererAdherent frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new GererAdherent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GererAdherent() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnAjouterAdherent = new JButton("Ajouter Adherent");
btnAjouterAdherent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
}
});
btnAjouterAdherent.setBounds(104, 34, 130, 23);
contentPane.add(btnAjouterAdherent);
}
}
But once I try to initialize a different JFRame, I get a blank JFrame without all of it's components (It is created properly when I use AjouterAdherent's main to initialize)
class AjouterAdherent :
public class AjouterAdherent extends JFrame {
JFrame frame;
JTextField txtNom;
static Properties p=new Properties();
static BibliothequeDAORemote proxy;
public static void main(String[] args) throws NamingException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AjouterAdherent() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.menu);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane txtpnAjouterClient = new JTextPane();
txtpnAjouterClient.setFont(new Font("Tahoma", Font.BOLD, 11));
txtpnAjouterClient.setBackground(SystemColor.menu);
txtpnAjouterClient.setEnabled(false);
txtpnAjouterClient.setEditable(false);
txtpnAjouterClient.setForeground(Color.black);
txtpnAjouterClient.setBounds(175, 11, 89, 20);
txtpnAjouterClient.setText("Ajouter Client");
frame.getContentPane().add(txtpnAjouterClient);
JTextPane txtpnNom = new JTextPane();
txtpnNom.setBackground(SystemColor.menu);
txtpnNom.setEnabled(false);
txtpnNom.setEditable(false);
txtpnNom.setForeground(Color.black);
txtpnNom.setBounds(36, 49, 72, 20);
txtpnNom.setText("Nom");
frame.getContentPane().add(txtpnNom);
}
}
Any help would be greatly appreciated!

(It is created properly when I use AjouterAdherent's main to initialize)
So look at the code that works properly:
AjouterAdherent window = new AjouterAdherent();
window.frame.setVisible(true);
I try to initialize a different JFRame, I get a blank JFrame
And look at the code that doesn't work:
AjouterAdherent ajouterAdherent = new AjouterAdherent();
ajouterAdherent.setVisible(true);
What is the difference?
The problem is your class extends JFrame and also creates a new JFrame, so you have two frame instances, one with components and one without.
Don't extend JFrame! Then you won't cause confusion.
You should only extend a class when you add new functionality to the class. Adding components to the frame is not adding new functionality.

Related

Change button's text with an external combobox java

I've two class. Can i change a text of a button in a class "home" with an actionlistener of the combobox in a class "panelGestisciImpianti"? I don't unterstand becasue don't works.
The code is this:
//home
package s;
public class home extends JFrame {
private JPanel contentPane;
private panelImpostazioni panel5= new panelImpostazioni();
private JButton btnImpostazioni = new JButton("no"); //$NON-NLS-1$
public static void main(String[] args) {
home frame = new home();
frame.setVisible(true);
}
public home() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState( JFrame.MAXIMIZED_BOTH) ;
setBounds(0, 0, 1963, 688);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
btnImpostazioni.setBounds(0, 560, 140, 140);
contentPane.add(btnImpostazioni);
btnImpostazioni.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
contentPane.add(panel5);
revalidate();
repaint();
}
});
}
public void changetext() {
btnImpostazioni.setText("yes");
}
}
//panelGestisciImpostazioni
package s;
public class panelImpostazioni extends JPanel {
private JComboBox comboboxLingua = new JComboBox();
static home h=new home();
public panelImpostazioni() {
setBounds(140, 0, 800, 560);
setLayout(null);
comboboxLingua.setBounds(100, 24, 150, 45);
comboboxLingua.setModel(new DefaultComboBoxModel(new String[] {"italiano", "inglese"}));
add(comboboxLingua);
comboboxLingua.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
h.changetext();
}
});
}
}
Thank you.
Its because when you create panel5 you have a new home created.
static home h = new home ().
So when you call changetext method you do it in a new invisible frame.
In order to make this work (it is really really bad) you have to pass your visible "home" as an argument to your panel5. Which means you have to initiate it in home's constructor and not as a field.
public panelImpostazioni(home h)
And in your combobox action listener
h.changetext ()

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!.

How to add an JPanel from an other class to an existing Frame

I have my MainWindow class where menue buttons and everything else are. In the middle of it is an Panel called content. I want to load JPanels from other classes into this field. But when i start the code below nothing shows up.
MainWindow Class:
public class MainWindow {
private JFrame frame;
private JScrollPane Content;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel TopPanel = new JPanel();
scrollPane.setColumnHeaderView(TopPanel);
JLabel lblNewLabel = new JLabel("Made by " + Globals.Author);
TopPanel.add(lblNewLabel);
JButton btnHome = new JButton("Home");
TopPanel.add(btnHome);
btnHome.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
Content.add(new Home());
}
});
Content = new JScrollPane();
scrollPane.setViewportView(Content);
}
}
JPanel Class:
public class Home extends JPanelContentTemplate {
/**
* Create the panel.
*/
protected void InitializeComponents(){
setLayout(new BorderLayout(0, 0));
JPanel OptionsMenuePanel = new JPanel();
add(OptionsMenuePanel, BorderLayout.WEST);
JPanel ConentPanel = new JPanel();
add(ConentPanel, BorderLayout.CENTER);
ConentPanel.setLayout(new GridLayout(1, 2, 0, 0));
JLabel lblConnectedWith = new JLabel("Connected With:");
ConentPanel.add(lblConnectedWith);
JTextPane textServerIP = new JTextPane();
ConentPanel.add(textServerIP);
}
#Override
protected void Refresh() {
// TODO Auto-generated method stub
}
}
The InitializeComponents method comes from an Self Created Superclass:
public abstract class JPanelContentTemplate extends JPanel {
/**
* Create the panel.
*/
public JPanelContentTemplate() {
InitializeComponents();
}
protected abstract void InitializeComponents();
protected abstract void Refresh();
}
I also tried an repaint etc.
Thanks for Help
Nothing shows up because you add nothing but empty JScrollPanes to your GUI:
Content = new JScrollPane();
scrollPane.setViewportView(Content);
Here is a simple example of adding separate panels to the MainFrame. I hope it helps you.
public class SidePanel extends JPanel {
private JLabel label;
public SidePanel() {
setBorder(BorderFactory.createEtchedBorder());
label = new JLabel("Hello");
setVisible(true);
/* More Code Goes Here */
}
}
public class CenterPanel extends JPanel {
/* Center Panel Code */
}
public class MainFrame extends JFrame {
private SidePanel sidePanel;
private CenterPanel centerPanel;
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
sidePanel = new SidePanel();
centerPanel = new CenterPanel();
add(sidePanel, BorderLayout.WEST);
add(centerPanel, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
}
/* Main App */
public static void main(String [] args) {
try {
/* Lambda Expression */
SwingUtitlities.InvokeLater(() -> new MainFrame());
} catch(Exception ex) {
ex.printStackTrace();
}
}

Connecting 2 Jframes with JUNG

I have created 2 JFrames and tying to link two JFrames Windows together. But i couldnt get the content in the 2nd Jframe. Can anyone help me with this?
My first Frame:
public class Main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnOpenTheJung = new JButton("Open the JUNG window");
btnOpenTheJung.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
**JungLearning1 frame1 = new JungLearning1();
frame1.setVisible(true);
frame1.setSize(600, 400);**
}
});
btnOpenTheJung.setBounds(172, 99, 145, 34);
contentPane.add(btnOpenTheJung);
}}
My second Frame:
public class JungLearning1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DirectedSparseGraph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("Vertex1");
g.addVertex("Vertex2");
g.addVertex("Vertex3");
g.addEdge("Edge1", "Vertex1", "Vertex2");
g.addEdge("Edge2", "Vertex1", "Vertex3");
g.addEdge("Edge3", "Vertex3", "Vertex1");
VisualizationImageServer<String, String> vs = new VisualizationImageServer<String, String>(
new CircleLayout<String, String>(g), new Dimension(
200, 200));
**JFrame frame1 = new JFrame();
frame1.getContentPane().add(vs);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);**
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JungLearning1() {
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);
}}
I want to display the 2nd frame along with the content when clicking the butten from the first window

Open new screen or page on button click (java-eclipse)

I have a home page with title and a few buttons I cannot get a new window to open when i click on the button. Here is the code i have for the home page aswell as the class with next screen i am attempting to open trimed for what seems relevant. The NewTicketWindow class is also attached it is plain at the moment. Any help is appreciated.
public class Home
{
private JFrame frame;
JInternalFrame internalFrame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Home window = new Home();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Home()
{
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);
JLabel title1 = new JLabel("City of Murphy");
JLabel title2 = new JLabel("Traffic Ticket Input System");
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
JButton payTicketButton = new JButton("Make a Payment");
JButton reportButtton = new JButton("Ticket Report");
JButton exitButton = new JButton("Exit");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}
second class (the screen i want to open upon newticket button being pressed
public class NewTicketWindow extends JFrame
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NewTicketWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
}
just add these lines into your action performed code -
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
The ActionListener of newTicketButton should create the new frame by calling the constructor of NewTicketWindow (same thing you are doing in the main of NewTicketWindow):
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
NewTicketWindow newTicketWindow = new NewTicketWindow();
newTicketWindow.setVisible(true);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
Also you need to add the newTicketButton to the home window:
frame.add(newTicketButton);

Categories