I’ve just started using Java Layout manager, it’s been pretty difficult to get the GUI to behave as desired, so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class ex1 extends JFrame {
private javax.swing.JButton AddBtn,perviousBtn,NextBtn;
private javax.swing.JLabel LatLabel, LongLabel, EvlLabel;
private javax.swing.JTextField LatText,LongText,EvlText;
String columns[] = { "ID", "Name", "Age", "Gender" };
Object data[][] = { { "0", "Tom", new Integer(20), "Male" },
{ "1", "Tina", new Integer(18), "Female" },
{ "2", "Raj", new Integer(19), "Male" },
{ "3", "Tina", new Integer(18), "Female" },
{ "4", "Raj", new Integer(19), "Male" },
{ "5", "Tina", new Integer(18), "Female" },
{ "6", "Raj", new Integer(19), "Male" },
{ "7", "Tina", new Integer(18), "Female" },
{ "8", "Raj", new Integer(19), "Male" },
{ "9", "Tina", new Integer(18), "Female" },
{ "10", "Raj", new Integer(19), "Male" }
};
public void addComponentsToPane(final Container pane) {
JPanel compsToExperiment = new JPanel();
GridLayout experimentLayout = new GridLayout(3, 3);
compsToExperiment.setLayout(experimentLayout);
JPanel Table = new JPanel();
Table.setLayout(new GridLayout(8, 1));
JPanel nav = new JPanel();
nav.setLayout(new GridLayout(8, 3));
LatLabel = new javax.swing.JLabel("Latitude: ");
LongLabel = new javax.swing.JLabel("Longitude: ");
EvlLabel = new javax.swing.JLabel("Elevation: ");
LatText = new javax.swing.JTextField();
LongText = new javax.swing.JTextField();
EvlText = new javax.swing.JTextField();
AddBtn = new javax.swing.JButton("Add");
perviousBtn = new javax.swing.JButton("Next");
NextBtn = new javax.swing.JButton("pervious");
// perviousBtn.setPreferredSize(new Dimension(60, 60));
compsToExperiment.add(LatLabel);
compsToExperiment.add(LongLabel);
compsToExperiment.add(EvlLabel);
compsToExperiment.add(LatText);
compsToExperiment.add(LongText);
compsToExperiment.add(EvlText);
compsToExperiment.add(AddBtn);
// jButton1.addActionListener((ActionListener) this);
// this.add(jButton1);
JTable table = new JTable(data, columns);
Table.add(table.getTableHeader());
Table.add(table);
nav.add(NextBtn);
nav.add(perviousBtn);
pane.add(compsToExperiment, BorderLayout.NORTH);
pane.add(nav, BorderLayout.EAST);
pane.add(Table, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety, this method is invoked
* from the event dispatch thread.
*/
private static void createAndShowGUI() {
ex1 frame = new ex1();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane(frame.getContentPane());
frame.setSize(500, 500);
//frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Also I’m having issue to get table to display all the entry, it will only show the first 3 entries. Can you please help me to get the GUI tidy and with table issue.
Take a look at these lines:
You should read the documentation on GridLayout carefully to use it in your code especially if you are using several layouts just like you do in your code. I suggest you design your components and their layouts on a scratch paper before you attempt to code them. Here, I changed your
Table‘s layout’s parameters to this:And it displayed all the data entries. Here is what API says on
GridLayout:Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.
One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.