I am currently trying to include a JTable to my JPanel. I was not having any problems with my code previously until I started to code the table. Sometimes when I run my program everything will appear, sometimes just the panels appear, and sometimes/majority of the time nothing will appear. I just don't understand why it is not consistent. There are no errors in the console. I have included below my ViewPage.java, Page.java, and Main.java.
`
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ViewPage extends Page implements ActionListener
{
JPanel panel1, panel2, panel3;
JLabel searchLabel, repairsLabel, dateLabel;
JButton goButton, recentButton, oldestButton, homeButton;
JComboBox dropDown;
JTextField textField;
JTable tabel;
JScrollPane scrollpane;
public ViewPage()
{
panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.setBounds(0,0,1280,120);
panel2 = new JPanel();
panel2.setBackground(Color.green);
panel2.setBounds(0,120,1280,480);
panel3 = new JPanel();
panel3.setBackground(Color.red);
panel3.setBounds(0,600,1280,120);
//Panel 1 components
repairsLabel = new JLabel("Repairs");
repairsLabel.setFont(new Font("Serif", Font.BOLD, 50));
searchLabel = new JLabel("Search :");
searchLabel.setFont(new Font("Serif", Font.PLAIN, 25));
goButton = new JButton("GO");
goButton.setFocusable(false);
goButton.addActionListener(this);
textField = new JTextField();
String[] filters = {" ", "customerID", "First_Name", "Last_Name"};
dropDown = new JComboBox(filters);
dropDown.addActionListener(this);
panel1.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 0);
gbc.weightx = 5.5;
gbc.gridx = 0;
gbc.gridy = 0;
panel1.add(repairsLabel, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0.5;
gbc.gridx = 1;
gbc.gridy = 0;
panel1.add(searchLabel, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 50);
gbc.weightx = 3;
gbc.gridx = 2;
gbc.gridy = 0;
panel1.add(dropDown, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 50);
gbc.gridx = 3;
gbc.gridy = 0;
gbc.ipadx = 100;
panel1.add(textField, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 10, 1, 100);
gbc.gridx = 4;
gbc.gridy = 0;
gbc.ipadx = 1;
panel1.add(goButton, gbc);
//Panel 2 components
panel2.setLayout(new GridBagLayout());
String[][] data = new String[50][8];
String[] headings = {"customer_ID", "Date", "First Name", "Last Name", "Phone #", "OS", "PC Type", "Problem"};
JTable table = new JTable(data, headings);
table.setEnabled(false);
table.setPreferredScrollableViewportSize(new Dimension(1000,400));
table.setFillsViewportHeight(true);
scrollpane = new JScrollPane(table);
panel2.add(scrollpane);
//Panel 3 componets
panel3.setLayout(new GridBagLayout());
GridBagConstraints gbc2 = new GridBagConstraints();
dateLabel = new JLabel("Date Filter: ");
dateLabel.setFont(new Font("Serif", Font.PLAIN, 25));
recentButton = new JButton("Most Recent");
oldestButton = new JButton("Oldest");
homeButton = new JButton("Home");
gbc2.fill = GridBagConstraints.HORIZONTAL;
gbc2.insets = new Insets(1, 10, 1, 0);
gbc2.weightx = 5.5;
gbc2.gridx = 0;
gbc2.gridy = 0;
panel3.add(dateLabel, gbc);
frame.add(panel1);
frame.add(panel2);
frame.add(panel3);
}
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == dropDown)
{
System.out.println(dropDown.getSelectedItem());
}
}
}
`
`
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public abstract class Page
{
private final static int pageWidth = 1280;
private final static int pageHeight = 720;
protected JFrame frame;
public Page()
{
frame = new JFrame();
frame.setTitle("Computer Repair Store");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(pageWidth, pageHeight);
frame.setLayout(null);
frame.setVisible(true);
}
public int getpageWidth()
{
return pageWidth;
}
public int getpageHeight()
{
return pageHeight;
}
}
`
`
public class Main {
public static void main(String[] args) {
ViewPage viewPage = new ViewPage();
}
}
`
Ouput:
enter image description here
This is what I am expecting to output which has only appeared 2-3 times out of the 50 times I have tried running the program.
enter image description here
I want to add a picture that will be in the panel and also on labels but I don't know how to do that.
my code:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel mainPanel = new JPanel();
panel.setLayout(new GridLayout(5, 5));
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
{
JLabel label = new JLabel();
label.setPreferredSize(new Dimension(50, 50));
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel.add(label);
}
mainPanel.add(panel);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
My Picture:
https://i.stack.imgur.com/w0Ssy.png
What I want to make:
https://i.stack.imgur.com/ZLPqF.png
If you're going to use components do to this job, then you're going to need to do some juggling with the layout managers. Because of it's flexibility, I would use GridBagLayout, in fact, I'm pretty sure it's the only inbuilt layout that will allow you to do something like this...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePane extends JPanel {
public GamePane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
GridPane gridPane;
if (y == 9) {
if (x == 9) {
gridPane = new GridPane(1, 1, 1, 1);
} else {
gridPane = new GridPane(1, 1, 1, 0);
}
} else if (x == 9) {
gridPane = new GridPane(1, 1, 0, 1);
} else {
gridPane = new GridPane(1, 1, 0, 0);
}
gbc.gridx = x;
gbc.gridy = y;
add(gridPane, gbc);
}
}
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(new ShipPane(1, 4), gbc, 0);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new ShipPane(3, 1), gbc, 0);
}
}
public class Configuration {
public static int GRID_SIZE = 50;
}
public class GridPane extends JPanel {
public GridPane(int top, int left, int bottom, int right) {
setBorder(new MatteBorder(top, left, bottom, right, Color.DARK_GRAY));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(Configuration.GRID_SIZE, Configuration.GRID_SIZE);
}
}
public class ShipPane extends JPanel {
private int gridWidth, gridHeight;
public ShipPane(int gridWidth, int gridHeight) {
this.gridWidth = gridWidth;
this.gridHeight = gridHeight;
setOpaque(false);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(Configuration.GRID_SIZE * gridWidth, Configuration.GRID_SIZE * gridHeight);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(255, 0, 0, 128));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
Now, I'm just using a panel, but without much work, you could add a JLabel to the ShipPane
Now, having said that. I think I would approach this problem from an entirely "custom painting" route
The way to do this is to use JLabel.setIcon
JLabel l = ...;
l.setIcon(myIcon);
You can make an Icon from an Image using ImageIcon.
You can get an image by loading it from disk/url, or by creating your own by creating a BufferedImage and drawing to its Graphics object.
This is my first database simulator GUI, I'm having a hard time navigating with TAB through the user input text fields. My desire is to navigate in this order: From name to 1st last name, to 2nd last name, to age, to phone number, to address and finally to e-mail. I honestly just copied the code for a custom Focus Traversal Policy and was hoping it would work but it isn't. I don't know if I should even call it on the Listener class or is this redundant?
Here's the code, I'm sorry it got so long =S (omitted libraries to comply w/post max length):
public class UserFormNew {
private JTextField idtf;
private JTextField nametf;
private JTextField ln1tf;
private JTextField ln2tf;
private JTextField agetf;
private JTextField pntf;
private JTextField addresstf;
private JTextField emtf;
private JTable t;
private DefaultTableModel dtm;
private JTableHeader header;
private int idCount = 0;
private int rowSelected;
private boolean ed = false;
private String[] selRowInfoGotten;
private List<Component> compList = new ArrayList<Component>();
private JPanel dataIn;
public UserFormNew() {
// Containers
JFrame mainF = new JFrame();
JPanel mainP = new JPanel(new BorderLayout(10, 5));
dataIn = new JPanel(new GridBagLayout());
JPanel table = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
JPanel buttons = new JPanel(new BorderLayout(20, 10));
// Fonts
Font plain = new Font("Tahoma", Font.PLAIN, 12);
Font plaint = new Font("Tahoma", Font.PLAIN, 11);
Font bold12 = new Font("Tahoma", Font.BOLD, 12);
// Data-In
GridBagConstraints gbc = new GridBagConstraints();
Insets topLeftLabel = new Insets(20, 20, 2, 2);
Insets midLeftLabel = new Insets(2, 20, 2, 2);
Insets botLeftLabel = new Insets(2, 20, 20, 2);
Insets topMidTF = new Insets(20, 2, 2, 20);
Insets midMidTF = new Insets(2, 2, 2, 20);
Insets botMidTF = new Insets(2, 2, 20, 20);
Insets topMidLabel = new Insets(20, 30, 2, 2);
Insets midMidLabel = new Insets(2, 30, 2, 2);
Insets botMidLabel = new Insets(2, 30, 20, 2);
Insets topRightInsets = new Insets(20, 2, 2, 20);
Insets midRightInsets = new Insets(2, 2, 2, 20);
Insets botRightInsets = new Insets(2, 2, 10, 20);
JLabel id = new JLabel("Id");
id.setFont(bold12);
gbc.insets = topLeftLabel;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(id, gbc);
JLabel name = new JLabel("Name");
name.setFont(bold12);
gbc.insets = midLeftLabel;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(name, gbc);
JLabel ln1 = new JLabel("1\u00B0 Lastname");
ln1.setFont(bold12);
gbc.insets = midLeftLabel;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln1, gbc);
JLabel ln2 = new JLabel("2\u00B0 Lastname");
ln2.setFont(bold12);
gbc.insets = botLeftLabel;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln2, gbc);
JLabel age = new JLabel("Age");
age.setFont(bold12);
gbc.insets = topMidLabel;
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(age, gbc);
JLabel pn = new JLabel("Phone Number");
pn.setFont(bold12);
gbc.insets = midMidLabel;
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(pn, gbc);
JLabel address = new JLabel("Address");
address.setFont(bold12);
gbc.insets = midMidLabel;
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(address, gbc);
JLabel em = new JLabel("E-Mail");
em.setFont(bold12);
gbc.insets = botMidLabel;
gbc.gridx = 2;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(em, gbc);
JLabel emptyTop = new JLabel(" ");
gbc.insets = topRightInsets;
gbc.gridx = 5;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyTop, gbc);
JLabel emptyMid1 = new JLabel(" ");
gbc.insets = midRightInsets;
gbc.gridx = 5;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyMid1, gbc);
JLabel emptyMid2 = new JLabel(" ");
gbc.insets = midRightInsets;
gbc.gridx = 5;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyMid2, gbc);
JLabel emptyBot = new JLabel(" ");
gbc.insets = botRightInsets;
gbc.gridx = 5;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emptyBot, gbc);
idtf = new JTextField("");
idtf.setFont(plain);
idtf.setEditable(false);
idtf.setHorizontalAlignment(JTextField.RIGHT);
idtf.addKeyListener(new NumericList());
gbc.insets = topMidTF;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(idtf, gbc);
nametf = new JTextField("");
nametf.setFont(plain);
nametf.setEditable(true);
nametf.addKeyListener(new AlphabeticList());
gbc.insets = midMidTF;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(nametf, gbc);
ln1tf = new JTextField("");
ln1tf.setFont(plain);
ln1tf.setEditable(true);
ln1tf.addKeyListener(new AlphabeticList());
gbc.insets = midMidTF;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln1tf, gbc);
ln2tf = new JTextField("");
ln2tf.setFont(plain);
ln2tf.setEditable(true);
ln2tf.addKeyListener(new AlphabeticList());
gbc.insets = botMidTF;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0.4;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(ln2tf, gbc);
agetf = new JTextField("");
agetf.setFont(plain);
agetf.setEditable(true);
agetf.addKeyListener(new NumericList());
gbc.insets = topMidTF;
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(agetf, gbc);
pntf = new JTextField("");
pntf.setFont(plain);
pntf.setEditable(true);
pntf.addKeyListener(new PhoneList());
gbc.insets = midMidTF;
gbc.gridx = 3;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.1;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(pntf, gbc);
addresstf = new JTextField("");
addresstf.setFont(plain);
addresstf.setEditable(true);
addresstf.addKeyListener(new AddressList());
gbc.insets = midMidTF;
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(addresstf, gbc);
emtf = new JTextField("");
emtf.setFont(plain);
emtf.setEditable(true);
emtf.addKeyListener(new EmailList());
gbc.insets = botMidTF;
gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridwidth = 3;
gbc.weightx = 0.7;
gbc.weighty = 0.2;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
dataIn.add(emtf, gbc);
// My Traversal Policy
compList.add(nametf);
compList.add(ln1tf);
compList.add(ln2tf);
compList.add(agetf);
compList.add(pntf);
compList.add(addresstf);
compList.add(emtf);
dataIn.setFocusTraversalPolicy(new MyFocusTraversalPolicy());
// Table
String[][] data = {};
String[] headers = { "Id", "Name", "First Lastname", "Second Lastname", "Age", "Phone Number", "Address",
"E-Mail" };
dtm = new DefaultTableModel(data, headers);
t = new JTable(dtm);
t.setFont(plaint);
header = t.getTableHeader();
header.setFont(bold12);
// Table Design
int[] columnsWidth = { 65, 120, 120, 120, 40, 100, 300, 220 };
int i = 0;
for (int width : columnsWidth) {
TableColumn column = t.getColumnModel().getColumn(i++);
column.setMinWidth(width);
column.setMaxWidth(width);
column.setPreferredWidth(width);
}
// Columns Text Alignment
DefaultTableCellRenderer rendererLeft = new DefaultTableCellRenderer();
DefaultTableCellRenderer rendererCenter = new DefaultTableCellRenderer();
TableColumnModel colModel = t.getColumnModel();
TableColumn[] col = new TableColumn[t.getColumnCount()];
for (int j = 0; j < t.getColumnCount(); j++) {
col[j] = colModel.getColumn(j);
switch (j) {
case 0: {
rendererCenter.setHorizontalAlignment(JLabel.CENTER);
col[j].setCellRenderer(rendererCenter);
}
break;
case 1: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 2: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 3: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 4: {
rendererCenter.setHorizontalAlignment(JLabel.CENTER);
col[j].setCellRenderer(rendererCenter);
}
break;
case 5: {
rendererCenter.setHorizontalAlignment(JLabel.CENTER);
col[j].setCellRenderer(rendererCenter);
}
break;
case 6: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
case 7: {
rendererLeft.setHorizontalAlignment(JLabel.LEFT);
col[j].setCellRenderer(rendererLeft);
}
break;
}
}
// Insert table into ScrollPane
JScrollPane sp = new JScrollPane(t);
table.add(sp);
t.setPreferredScrollableViewportSize(new Dimension(1085, 230));
// Buttons
JPanel eastP = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
JButton save = new JButton("Save");
JButton del = new JButton("Delete");
JButton edit = new JButton("Edit");
JButton clr = new JButton("Clear");
save.setFont(bold12);
del.setFont(bold12);
edit.setFont(bold12);
clr.setFont(bold12);
save.addActionListener(new Save());
del.addActionListener(new Delete());
edit.addActionListener(new Edit());
clr.addActionListener(new Clear());
t.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
rowSelected = t.getSelectedRow();
}
});
eastP.add(save);
eastP.add(del);
eastP.add(edit);
eastP.add(clr);
buttons.add(eastP, BorderLayout.EAST);
// Add to main
mainP.add(dataIn, BorderLayout.NORTH);
mainP.add(table, BorderLayout.CENTER);
mainP.add(buttons, BorderLayout.SOUTH);
mainF.setContentPane(mainP);
mainF.pack();
mainF.setVisible(true);
mainF.setResizable(false);
mainF.setLocationRelativeTo(null);
mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new UserFormNew();
}
});
}
//Focus Traversal Policy Custom Class
private class MyFocusTraversalPolicy extends FocusTraversalPolicy {
public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
int currentPosition = compList.indexOf(aComponent);
currentPosition = (currentPosition + 1) % compList.size();
return (Component) compList.get(currentPosition);
}
public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
int currentPosition = compList.indexOf(aComponent);
currentPosition = (compList.size() + currentPosition - 1) % compList.size();
return (Component) compList.get(currentPosition);
}
public Component getFirstComponent(Container cntnr) {
return (Component) compList.get(0);
}
public Component getLastComponent(Container cntnr) {
return (Component) compList.get(compList.size() - 1);
}
public Component getDefaultComponent(Container cntnr) {
return (Component) compList.get(0);
}
}
// Listener Classes
public class Save implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String name = nametf.getText();
String ln1 = ln1tf.getText();
String ln2 = ln2tf.getText();
String age = agetf.getText();
String pn = pntf.getText();
String address = addresstf.getText();
String em = emtf.getText();
// Autogenerate id
idCount++;
// Data Into Table
if (idCount == 0) {
t.setValueAt(Integer.toString(idCount), 0, 0);
t.setValueAt(name, 0, 1);
t.setValueAt(ln1, 0, 2);
t.setValueAt(ln2, 0, 3);
t.setValueAt(age, 0, 4);
t.setValueAt(pn, 0, 5);
t.setValueAt(address, 0, 6);
t.setValueAt(em, 0, 7);
} else if (ed == true) {
t.setValueAt(name, rowSelected, 1);
t.setValueAt(ln1, rowSelected, 2);
t.setValueAt(ln2, rowSelected, 3);
t.setValueAt(age, rowSelected, 4);
t.setValueAt(pn, rowSelected, 5);
t.setValueAt(address, rowSelected, 6);
t.setValueAt(em, rowSelected, 7);
ed = false;
} else {
String[] newRow = { Integer.toString(idCount), name, ln1, ln2, age, pn, address, em };
dtm.addRow(newRow);
}
idtf.setText("");
nametf.setText("");
ln1tf.setText("");
ln2tf.setText("");
agetf.setText("");
pntf.setText("");
addresstf.setText("");
emtf.setText("");
}
}
public class Delete implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
dtm.removeRow(rowSelected);
}
}
public class Edit implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
ed = true;
selRowInfoGotten = new String[t.getColumnCount()];
for (int i = 0; i < t.getColumnCount(); i++) {
selRowInfoGotten[i] = (String) t.getValueAt(rowSelected, i);
}
idtf.setText(selRowInfoGotten[0]);
nametf.setText(selRowInfoGotten[1]);
ln1tf.setText(selRowInfoGotten[2]);
ln2tf.setText(selRowInfoGotten[3]);
agetf.setText(selRowInfoGotten[4]);
pntf.setText(selRowInfoGotten[5]);
addresstf.setText(selRowInfoGotten[6]);
emtf.setText(selRowInfoGotten[7]);
}
}
public class Clear implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (ed == false) {
idtf.setText("");
nametf.setText("");
ln1tf.setText("");
ln2tf.setText("");
agetf.setText("");
pntf.setText("");
addresstf.setText("");
emtf.setText("");
} else {
nametf.setText("");
ln1tf.setText("");
ln2tf.setText("");
agetf.setText("");
pntf.setText("");
addresstf.setText("");
emtf.setText("");
}
}
}
public class AlphabeticList implements KeyListener {
#Override
public void keyTyped(KeyEvent e) {
// Letters only
char testChar = e.getKeyChar();
if (!Character.isAlphabetic(testChar)) {
e.consume();
}
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
nametf.getFocusTraversalPolicy();
ln1tf.getFocusTraversalPolicy();
ln2tf.getFocusTraversalPolicy();
}
}
#Override
public void keyReleased(KeyEvent e) { }
}
public class NumericList implements KeyListener {
#Override
public void keyTyped(KeyEvent e) {
// Numbers only
char testChar = e.getKeyChar();
if (!Character.isDigit(testChar)) {
e.consume();
}
// 3-digit ages max
String ageText = agetf.getText();
int ageLength = ageText.length();
if (ageLength >= 3) {
e.consume();
}
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
agetf.getFocusTraversalPolicy();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
public class AddressList implements KeyListener {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
addresstf.getFocusTraversalPolicy();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
public class PhoneList implements KeyListener {
#Override
public void keyTyped(KeyEvent e) {
// Numbers only
char testChar = e.getKeyChar();
if (!Character.isDigit(testChar)) {
e.consume();
}
String pnText = pntf.getText();
int pnLength = pnText.length();
// Add hyphens
if (pnLength == 3 || pnLength == 7) {
pntf.setText(pnText + "-");
}
// 10-digit numbers
if (pnLength >= 12) {
e.consume();
}
// Erase even hyphens
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
String pnTxt = pntf.getText();
int start = 0;
int end = pnLength - 2;
String pnErased = pnTxt.substring(start, end);
System.out.println(
"PNLength: " + pnLength + ", Start: " + start + ", End: " + end + ", PNErased: " + pnErased);
pntf.setText(pnErased);
}
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
pntf.getFocusTraversalPolicy();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
public class EmailList implements KeyListener {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_TAB) {
emtf.getFocusTraversalPolicy();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
}
Fortunately, I have a much smaller example user form where I added a focus traversal policy.
The code has two parts. While I'm constructing the GUI, I set my focus traversal policy. I put all of the JTextFields into a List in the order that I want them traversed. Every editable field in the JPanel must be included in this List.
I also wrote a class that extends FocusTraversalPolicy. I made it private and included it in the view class. That seems like an appropriate place to put the class. You can put it in a separate file if you want.
Here's the code. You can compile it, run it, and see that tabbing through the form follows the columns. This is what we like to call a minimal, reproducible example.
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
public class UserForm {
// top, left, bottom, right
private static final Insets topInsets = new Insets(10, 10, 10, 10);
private static final Insets topCenterInsets = new Insets(10, 0, 10, 10);
private static final Insets middleInsets = new Insets(0, 10, 10, 10);
private static final Insets middleCenterInsets = new Insets(0, 0, 10, 10);
public UserForm() {
JFrame frame = new JFrame("User Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Miscelaneous
Font plain = new Font("Tahoma", Font.PLAIN, 12);
Font bold11 = new Font("Tahoma", Font.BOLD, 11);
Font bold12 = new Font("Tahoma", Font.BOLD, 12);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
int gridy = 0;
// Data-In Form Components
JLabel idl = new JLabel("Id:");
idl.setFont(bold12);
addComponent(mainPanel, idl, 0, gridy, 1, 1, topInsets, GridBagConstraints.LINE_START, GridBagConstraints.NONE);
JTextField idtf = new JTextField(40);
idtf.setFont(plain);
addComponent(mainPanel, idtf, 1, gridy, 1, 1, topCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel agl = new JLabel("Age:");
agl.setFont(bold12);
addComponent(mainPanel, agl, 2, gridy, 1, 1, topCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField agtf = new JTextField(40);
agtf.setFont(plain);
addComponent(mainPanel, agtf, 3, gridy++, 1, 1, topCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel namel = new JLabel("Name:");
namel.setFont(bold12);
addComponent(mainPanel, namel, 0, gridy, 1, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField nametf = new JTextField(40);
nametf.setFont(plain);
addComponent(mainPanel, nametf, 1, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel adl = new JLabel("Address:");
adl.setFont(bold12);
addComponent(mainPanel, adl, 2, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField adtf = new JTextField(40);
adtf.setFont(plain);
addComponent(mainPanel, adtf, 3, gridy++, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel pnl = new JLabel("Phone Number:");
pnl.setFont(bold12);
addComponent(mainPanel, pnl, 0, gridy, 1, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField pntf = new JTextField(40);
pntf.setFont(plain);
addComponent(mainPanel, pntf, 1, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel ln1l = new JLabel("First Lastname:");
ln1l.setFont(bold12);
addComponent(mainPanel, ln1l, 2, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField ln1tf = new JTextField(40);
ln1tf.setFont(plain);
addComponent(mainPanel, ln1tf, 3, gridy++, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel ln2l = new JLabel("Second Lastname:");
ln2l.setFont(bold12);
addComponent(mainPanel, ln2l, 0, gridy, 1, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField ln2tf = new JTextField(40);
ln2tf.setFont(plain);
addComponent(mainPanel, ln2tf, 1, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel eml = new JLabel("E-Mail:");
eml.setFont(bold12);
addComponent(mainPanel, eml, 2, gridy, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.NONE);
JTextField emtf = new JTextField(40);
emtf.setFont(plain);
addComponent(mainPanel, emtf, 3, gridy++, 1, 1, middleCenterInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
List<Component> order = new ArrayList<>(8);
order.add(idtf);
order.add(nametf);
order.add(pntf);
order.add(ln2tf);
order.add(agtf);
order.add(adtf);
order.add(ln1tf);
order.add(emtf);
MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy(order);
frame.setFocusTraversalPolicy(newPolicy);
// JTable Creation
String[] headline = { "Id", "Name", "First Lastname", "Second Lastname", "Age", "Address", "Phone Number",
"E-Mail" };
String[][] data = { { "", "", "", "", "", "", "", "" }, { "", "", "", "", "", "", "", "" } };
JTable dataShow = new JTable(data, headline);
dataShow.setPreferredScrollableViewportSize(new Dimension(900, 300));
dataShow.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(dataShow);
addComponent(mainPanel, scrollPane, 0, gridy++, 4, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
// Table Personalization
dataShow.getTableHeader().setFont(bold11);
new HeaderRenderer(dataShow);
// buttons Components
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
JButton save = new JButton("Save");
save.setFont(bold12);
addComponent(buttonPanel, save, 0, 0, 1, 1, topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton del = new JButton("Delete");
del.setFont(bold12);
addComponent(buttonPanel, del, 1, 0, 1, 1, topInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
JButton edit = new JButton("Edit");
edit.setFont(bold12);
addComponent(buttonPanel, edit, 2, 0, 1, 1, topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
addComponent(mainPanel, buttonPanel, 0, gridy++, 4, 1, middleInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
// set content pane
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
// For future table filling
/*
* String ids = idtf.getText(); String names = nametf.getText(); String ln1s =
* ln1tf.getText(); String ln2s = ln2tf.getText(); String ags = agtf.getText();
* String ads = adtf.getText(); String pns = pntf.getText(); String ems =
* emtf.getText();
*/
}
private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth,
int gridheight, Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill,
insets, 0, 0);
container.add(component, gbc);
}
public static void main(String[] args) {
// invoke runnable for thread safety
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new UserForm();
}
});
}
// LEFT alignment renderer
private class HeaderRenderer implements TableCellRenderer {
DefaultTableCellRenderer renderer;
public HeaderRenderer(JTable table) {
renderer = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.LEFT);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
}
}
private class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy {
private List<Component> componentList;
public MyOwnFocusTraversalPolicy(List<Component> componentList) {
this.componentList = componentList;
}
#Override
public Component getComponentAfter(Container aContainer, Component aComponent) {
int index = getComponentIndex(aComponent);
index++;
index = (index >= componentList.size()) ? 0 : index;
return componentList.get(index);
}
#Override
public Component getComponentBefore(Container aContainer, Component aComponent) {
int index = getComponentIndex(aComponent);
index--;
index = (index < 0) ? index + componentList.size() : index;
return componentList.get(index);
}
#Override
public Component getFirstComponent(Container aContainer) {
return componentList.get(0);
}
#Override
public Component getLastComponent(Container aContainer) {
return componentList.get(componentList.size() - 1);
}
#Override
public Component getDefaultComponent(Container aContainer) {
return componentList.get(0);
}
private int getComponentIndex(Component test) {
for (int i = 0; i < componentList.size(); i++) {
Component component = componentList.get(i);
if (component.equals(test)) {
return i;
}
}
return -1;
}
}
}
I am going to have some behavior I want to be coupled with some other jcomboboxes, so I am using setModel() to fill in the arrays as values. While doing this and running the gui, I noticed after selecting a different element than the first one, that other element is not showing in the combo box. For example...
As you can see it doesn't say charTwo, even though that is what I selected. Here is the code.
Note: Line#101 is where the setModel() happens...
import org.apache.commons.lang3.ArrayUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class TestGui extends JFrame {
private final String[] guiCharSelDefault = {"--- Select Character ---"};
private final String[] characters = {"charOne", "charTwo", "charThree", "charFour"};
private final String[] GuiCharSel = (String[]) ArrayUtils.addAll(guiCharSelDefault, characters);
private JComboBox charCombo = createStandardCombo(GuiCharSel);
private BackgroundPanel backgroundFrame = createBackgroundFrame("../images/Background.png");
private JPanel topFrame = createTopFrame();
private JScrollPane topFrameScroll = createTopScrollPane();
private JPanel centerFrame = createCenterFrame();
//**************************************************************************************
// Constructor
TestGui(){
setContentPane(backgroundFrame);
add(topFrameScroll, BorderLayout.NORTH);
add(centerFrame, BorderLayout.CENTER);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//**************************************************************************************
// Support Methods
private static GridBagConstraints setGbc(int gridx, int gridy, int gridWidth, int gridHeight, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets){
GridBagConstraints gbc = new GridBagConstraints();
if (anchorLocation.toUpperCase().equals("NORTHWEST")){
gbc.anchor = GridBagConstraints.NORTHWEST;
} else if (anchorLocation.toUpperCase().equals("NORTH")){
gbc.anchor = GridBagConstraints.NORTH;
} else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
gbc.anchor = GridBagConstraints.NORTHEAST;
} else if (anchorLocation.toUpperCase().equals("WEST")){
gbc.anchor = GridBagConstraints.WEST;
} else if (anchorLocation.toUpperCase().equals("EAST")){
gbc.anchor = GridBagConstraints.EAST;
} else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
gbc.anchor = GridBagConstraints.SOUTHWEST;
} else if (anchorLocation.toUpperCase().equals("SOUTH")){
gbc.anchor = GridBagConstraints.SOUTH;
} else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
gbc.anchor = GridBagConstraints.SOUTHEAST;
} else {
gbc.anchor = GridBagConstraints.CENTER;
}
gbc.gridx = gridx; // column
gbc.gridy = gridy; // row
gbc.gridwidth = gridWidth; // number of columns
gbc.gridheight = gridHeight; // number of rows
gbc.ipadx = ipadx; // width of object
gbc.ipady = ipady; // height of object
gbc.weightx = weightx; // shifts rows to side of set anchor
gbc.weighty = weighty; // shifts columns to side of set anchor
gbc.insets = insets; // placement inside cell
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
return gbc;
}
private Insets setInsets(int top, int left, int bottom, int right){
Insets insets = new Insets(top,left,bottom,right);
return insets;
}
//**************************************************************************************
// Interactive Object Methods
private JComboBox createStandardCombo(String[] defaultValues){
JComboBox comboBox = new JComboBox(defaultValues);
DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER);
comboBox.setRenderer(dlcr);
comboBox.setPrototypeDisplayValue("X" + guiCharSelDefault + "X");
return comboBox;
}
//**************************************************************************************
// Object Action Methods
private void setCharComboAction(){
charCombo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String charName = ((JComboBox)(e.getSource())).getSelectedItem().toString();
if (!(charName.equals(guiCharSelDefault[0]))){
charCombo.setModel(new DefaultComboBoxModel(characters));
}
}
}
);
}
//**************************************************************************************
// Panel Methods
private BackgroundPanel createBackgroundFrame(String imgLocName){
Image backgroundImg = null;
try {
backgroundImg = ImageIO.read(getClass().getResource(imgLocName));
System.out.println("File: " + imgLocName.toString());
} catch (Exception e) {
System.out.println("Cannot read file: " + e);
}
BackgroundPanel bgPanel = new BackgroundPanel(backgroundImg, BackgroundPanel.SCALED, 0.0f, 0.0f);
return bgPanel;
}
private JPanel createTopFrame(){
JPanel pnl = new JPanel();
pnl.setLayout(new GridBagLayout());
setCharComboAction();
pnl.add(charCombo, setGbc(0,0, 1,1, 0,0, "WEST", 0, 0, setInsets(0, 10, 0, 0)));
pnl.setOpaque(false);
return pnl;
}
private JScrollPane createTopScrollPane(){
JScrollPane scrollPane = new JScrollPane(backgroundFrame);
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Border lineBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(224,224,224));
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
scrollPane.setBorder(compoundFinal);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
scrollPane.getViewport().setView(topFrame);
return scrollPane;
}
private JPanel createCenterFrame() {
JPanel pnl = new JPanel();
Border raisedBevel = BorderFactory.createRaisedBevelBorder();
Color lineColor = new Color(224, 224, 224);
Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, lineColor);
Border loweredBevel = BorderFactory.createLoweredBevelBorder();
Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);
TitledBorder topFrameTitle = BorderFactory.createTitledBorder(compoundFinal, "Stuff");
topFrameTitle.setTitleJustification(TitledBorder.CENTER);
pnl.setBorder(topFrameTitle);
pnl.setLayout(new GridBagLayout());
pnl.setOpaque(false);
return pnl;
}
//**************************************************************************************
public static void main(String[] args) {
new TestGui();
}
}
Is there a way I can have it keep the selected item after selecting it?
Why you don't restore the selection???
private void setCharComboAction(){
charCombo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String charName = ((JComboBox)(e.getSource())).getSelectedItem().toString();
if (!(charName.equals(guiCharSelDefault[0]))){
DefaultComboBoxModel model = new DefaultComboBoxModel(characters);
model.setSelectedItem(charName);
charCombo.setModel(model);
}
}
}
);
}
I see that GridBagLayout positions it's children with center alignment within cells. How to align left or right?
UPDATE
Constructing code (I know I could reuse c)
// button panel
JPanel button_panel = new JPanel();
button_panel.add(ok_button);
button_panel.add(cancel_button);
// placing controls to dialog
GridBagConstraints c;
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
add(inputSource_label, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
add(inputSource_combo, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
add(output_label, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
add(output_combo, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
add(button_panel, c);
When using GridBagLayout for a tabular display of JLabel : JTextField, I like to have a method that makes my GridBagConstraints for me based on the x, y position. For example something like so:
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = (x == 0) ? GridBagConstraints.BOTH
: GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
The following code makes a GUI that looks like this:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class GridBagEg {
private static void createAndShowGui() {
PlayerEditorPanel playerEditorPane = new PlayerEditorPanel();
int result = JOptionPane.showConfirmDialog(null, playerEditorPane,
"Edit Player", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
// TODO: do something with info
for (PlayerEditorPanel.FieldTitle fieldTitle :
PlayerEditorPanel.FieldTitle.values()) {
System.out.printf("%10s: %s%n", fieldTitle.getTitle(),
playerEditorPane.getFieldText(fieldTitle));
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
enum FieldTitle {
NAME("Name"), SPEED("Speed"), STRENGTH("Strength");
private String title;
private FieldTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
};
private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
public PlayerEditorPanel() {
setLayout(new GridBagLayout());
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Player Editor"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagConstraints gbc;
for (int i = 0; i < FieldTitle.values().length; i++) {
FieldTitle fieldTitle = FieldTitle.values()[i];
gbc = createGbc(0, i);
add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);
gbc = createGbc(1, i);
JTextField textField = new JTextField(10);
add(textField, gbc);
fieldMap.put(fieldTitle, textField);
}
}
private GridBagConstraints createGbc(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
gbc.fill = (x == 0) ? GridBagConstraints.BOTH
: GridBagConstraints.HORIZONTAL;
gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
gbc.weightx = (x == 0) ? 0.1 : 1.0;
gbc.weighty = 1.0;
return gbc;
}
public String getFieldText(FieldTitle fieldTitle) {
return fieldMap.get(fieldTitle).getText();
}
}
In this example, I display the JPanel in a JOptionPane, but it could just as easily be displayed in a JFrame or JApplet or JDialog or ...
For example
public class DimsPanel extends JPanel
{
public static void main(String[] args){
JFrame main = new JFrame("Dims");
JPanel myPanel = new DimsPanel();
main.setContentPane(myPanel);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setSize(400, 400);
main.setLocationRelativeTo(null);
main.setVisible(true);
}
JButton ok_button = new JButton("OK"), cancel_button = new JButton("Cancel");
JLabel inputSource_label = new JLabel("Input source:"),
output_label = new JLabel("Output:");
JComboBox inputSource_combo = new JComboBox(new String[]{"A", "B", "C"}),
output_combo = new JComboBox(new String[]{"A", "B", "C"});
public DimsPanel(){
super(new BorderLayout());
Box main = new Box(BoxLayout.Y_AXIS);
Dimension labelsWidth = new Dimension(100, 0);
JPanel inputPanel = new JPanel(new BorderLayout());
inputSource_label.setPreferredSize(labelsWidth);
inputPanel.add(inputSource_label, BorderLayout.WEST);
inputPanel.add(inputSource_combo);
JPanel outputPanel = new JPanel(new BorderLayout());
output_label.setPreferredSize(labelsWidth);
outputPanel.add(output_label, BorderLayout.WEST);
outputPanel.add(output_combo);
// button panel
JPanel button_panel = new JPanel();
button_panel.add(ok_button);
button_panel.add(cancel_button);
main.add(inputPanel);
main.add(outputPanel);
add(main, BorderLayout.NORTH);
add(button_panel);
}
}
You can run and see it. Resizing works like a charm and the layout code has only 18 lines.
The only disadvantage is that you need to specify the width of the labels by hand. If you really don't want to see setPreferredSize() in the code, then be my guest and go with GridBag. But I personally like this code more.