Swing JTable does not show anything - java

I added Jtable to my JPanel, it is simple code but screen shows nothing. Help Help Help Help Help Help Help Help .Here is my code:
public class AddProduct extends JFrame {
private JPanel contentPane;
Connection con;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddProduct frame = new AddProduct();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddProduct() {
con = MySQLConnection.getConnection();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 832, 466);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
String[] column_headers = {"Team", "Pointers", "Goal Diff"};
String[][] team_statics = {{"Man Utd", "65", "40"}, {"Chelsea", "63", "38"}};
table = new JTable();
table.setBounds(0, 297, 501, -296);
contentPane.add(table);
}
}
I am exactly doing what I watch in video.

Related

JScrollPane scroll only appears at bottom

I have this JFrame that has a JScrollPane and a JTextArea inside it, it works okay but the scroll button only appears at the bottom, I want it to show at the top as default. How can I change this?
public class frameMyPasswords extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frameMyPasswords frame = new frameMyPasswords();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frameMyPasswords() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scrollPane.getViewport().setViewPosition( new Point(0, 0) );
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setValue(0);
scrollPane.setBounds(10, 45, 414, 194);
contentPane.add(scrollPane);
JTextArea txtField = new JTextArea();
scrollPane.setViewportView(txtField);
txtField.setLineWrap(true);
txtField.setEditable(false);
txtField.setToolTipText("");
txtField.setText("LONG TEXT");
}
}

The second JFrame is blank and very small

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.

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

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

Switching from one Jtable to another Jtable every x seconds

I have a simple gui, that is suppose to shows table(JTable). How to make it switch to table1(JTable) after x amount of seconds, and then back to table after x amount of seconds and have that run in a loop. I know setting the other table is nothing more than putting this line in:
scrollPane.setViewportView(table1);
but how to switch from one to the other and back periodically. Guess I have to use the Timer but need some help with this one.thanks
Here is the simple example code:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
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(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
}
}
THE FINAL RESULT:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
private Timer timer;
private Timer timer1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
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(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
ActionListener action = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer.stop();
scrollPane.setViewportView(table1);
timer1.start();
}
};
ActionListener action1 = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer1.stop();
scrollPane.setViewportView(table);
timer.start();
}
};
timer = new Timer(3000, action);
timer.start();
timer1 = new Timer(3000, action1);
}
}
here is the working example of what i wanted.thanks everyone for help

Categories