1

I'm new to Swing and I want to create a table having this form:

enter image description here

So How to split a cell like the table shows? Have you any useful links or tutorials or an idea?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Amira
  • 3,184
  • 13
  • 60
  • 95
  • 4
    *"Have you any useful links or tutorials or an idea?"* My idea is 'use tables for tabular data' only. Since that is not clearly 'tabular data', use a different component or group of components. As an aside, is that meant to represent a score card for 10-pin bowling? – Andrew Thompson Mar 08 '13 at 10:31
  • 2
    Take a look at [these examplpes](http://www.crionics.com/public/swing_examples/JTableExamples4.html) – MadProgrammer Mar 08 '13 at 10:56
  • @AndrewThompson yes this is what i meant , i want to display The score for players each frame. – Amira Mar 08 '13 at 11:03
  • 1
    @MadProgrammer: I can't decide of the [examples](http://stackoverflow.com/a/14246573/230513) are _venerable_ or _superannuated_. AmiraGL: See also [*How to Use Tables > Concepts: Editors and Renderers*](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender). – trashgod Mar 08 '13 at 13:08

1 Answers1

4

This looks like a bowling score card to me. Based on that assumption, the number of columns is fixed, and the number of entries tends to be 6 or less. Since you likely won't need scrolling, I would recommend a fixed grid of components instead of a JTable.

This could easily be acheived using GridBagLayout. For the name, I'd use a JTextArea. For the 2 scoring cells for each frame, I'd use JTextFields. For the bottom 2-column-span component that holds the frame's score I'd probably use a JLabel.

Put all of this on a JPanel, and recreate the panel for each bowler.

EDIT: Here's a quick mock-up just to show the concept. Not necessarily visually pretty, but I'll leave that as an exercise for the reader:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class BowlingScoreCard implements Runnable
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new BowlingScoreCard());
  }

  public void run()
  {
    JFrame frame = new JFrame("Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(createScorecard(4), BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }

  private JPanel createScorecard(int numPlayers)
  {
    JPanel p = new JPanel(new GridBagLayout());

    p.add(new JLabel("Player"), gbc(0, 0, 1, 1));
    for (int x = 1; x <= 10; x++)
    {
      p.add(new JLabel(Integer.toString(x)), gbc(x, 0, 1, 1));
    }

    for (int y = 1; y <= numPlayers; y++)
    {
      JTextArea textArea = new JTextArea(2, 10);
      p.add(textArea, gbc(0, y, 1, 1));

      for (int i = 1; i <= 9; i++)
      {
        p.add(createFrame(2), gbc(i, y, 1, 1));
      }

      p.add(createFrame(3), gbc(10, y, 1, 1));
    }

    return p;
  }

  private JPanel createFrame(int entries)
  {
    JLabel label = new JLabel(" ");
    label.setBackground(Color.GRAY);

    JPanel p = new JPanel(new GridBagLayout());
    p.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
    for (int i = 0; i < entries; i++)
    {
      p.add(new JTextField(3), gbc(i, 0, 1, 1));
    }
    p.add(label, gbc(0, 1, 2, 1));
    return p;
  }

  private GridBagConstraints gbc(int x, int y, int colspan, int rowspan)
  {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = colspan;
    gbc.gridheight = rowspan;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.NONE;
    return gbc;
  }
}
splungebob
  • 5,357
  • 2
  • 22
  • 45