How to set Line Border color and width with Java?

Java 8Object Oriented ProgrammingProgramming

To set Line Border color and width, use the LineBorder. At first, set a panel wherein we need to set the line border −

JPanel panel = new JPanel();

Now, create a border and set on the panel created above −

Border border = new LineBorder(Color.ORANGE, 4, true);
panel.setBorder(border);

The following is an example to set LineBorder color and width −

package my;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      Border border = new LineBorder(Color.ORANGE, 4, true);
      panel.setBorder(border);
      String[][] rec = {
         { "1", "Steve", "AUS" },
         { "2", "Virat", "IND" },
         { "3", "Kane", "NZ" },
         { "4", "David", "AUS" },
         { "5", "Ben", "ENG" },
         { "6", "Eion", "ENG" },
      };
      String[] header = { "Rank", "Player", "Country" };
      JTable table = new JTable(rec, header);
      table.setShowHorizontalLines(true);
      table.setGridColor(Color.orange);
      table.setCellSelectionEnabled(true);
      panel.add(new JScrollPane(table));
      frame.add(panel);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

Output


raja
Published on 04-Jun-2019 07:10:31
Advertisements