CellRenderer - making multiple cells bold - java

I would like to make multiple cells bold, I have arraylist with the cell names that should be bold but doesn't seem to work. My Arraylist with the cells that I want to bold is called bibNumbers, so far tried converting the arralist into array and putting the if statement into a loop and loop results into bolding variable
//edit
added example, so now id want all the 0's and 1's in the column 2 to be bold.
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class as extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
private ArrayList<String> ShadowBibNumber;
/**
* Launch the application.
*/
public static void main(String[] args) {
as frame = new as();
frame.setVisible(true);
}
/**
* Create the frame.
*/
public as() {
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));
ShadowBibNumber = new ArrayList<String>();
ShadowBibNumber.add("0");
ShadowBibNumber.add("1");
JLabel lblNewLabel = new JLabel("New label");
contentPane.add(lblNewLabel, BorderLayout.NORTH);
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.addColumn("column 0");
tableModel.addColumn("column 1");
tableModel.addColumn("column 2");
tableModel.addColumn("column 3");
tableModel.addColumn("column 4");
new JScrollPane(table);
table = new JTable(tableModel);
table.setEnabled(false);
table.setPreferredScrollableViewportSize(new Dimension(200, 100));
JScrollPane scrollPaneForTable = new JScrollPane(table);
contentPane.add(scrollPaneForTable, BorderLayout.CENTER);
for (int i = 0; i < 20; ++i) {
tableModel
.addRow(new Object[] { i, i, i, i, i });
}
table.getColumn("column 2").setCellRenderer(new boldRenderer());
}
class boldRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component cellComponent = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
for (Object x: ShadowBibNumber) {
if (table.getValueAt(row, 2).equals(x)) {
cellComponent.setFont(cellComponent.getFont().deriveFont(
Font.BOLD));
} else {
cellComponent.setFont(cellComponent.getFont().deriveFont(
Font.PLAIN));
}
}
return cellComponent;
}
} }

Related

JScrollPane "stretching" out the panel that I add to it

I have to make a scrollable list where I can add a panel with 3 labels many times.
I kind of made it work but the first panels are stretched and occupy all the area of the JScrollPane and I can't figure out how to fix this, I tried changing layouts many times but still didn't manage to fix it.
I want the added panel to occupy a fixed size but I can't figure this out. Example in this picture:
https://i.stack.imgur.com/LNznP.png
The one on the left is the one that I get and the one on the right (edited) is how I want it to work.
This is my first day of Swing so the code is very likely a mess, sorry in advance.
Here is the code:
public class MainWindow extends JFrame implements ActionListener{
private JPanel viewportPanel;
private JButton addButton,remButton;
private JScrollPane scrollPane;
private int counter = 0;
private JLabel dateLabel,dateLabel_1,dateLabel_2;
public MainWindow(boolean run) {
//BUTTONS
addButton = new JButton("Add");
addButton.setLocation(521, 11);
addButton.setSize(101, 100);
addButton.addActionListener(this);
remButton = new JButton("Remove");
remButton.setLocation(521, 122);
remButton.setSize(101, 100);
remButton.addActionListener(this);
//SCROLLPANE
scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 11, 501, 211);
add(scrollPane);
//PANELS
viewportPanel = new JPanel(new GridLayout(0,1));
scrollPane.setViewportView(viewportPanel);
//FRAME
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 273);
setResizable(false);
//setIconImage(new ImageIcon("epic.png").getImage());
setLayout(null);
if(run) setVisible(true);
add(addButton);
add(remButton);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addButton) {
//LABELS
dateLabel = new JLabel("DATE");
dateLabel.setFont(new Font("Tahoma", Font.BOLD, 28));
dateLabel.setSize(500,500);
dateLabel_1 = new JLabel("LABEL1");
dateLabel_1.setFont(new Font("Tahoma", Font.BOLD, 22));
dateLabel_1.setBounds(10, 45, 481, 30);
dateLabel_2 = new JLabel("LABEL2");
dateLabel_2.setFont(new Font("Tahoma", Font.ITALIC, 22));
dateLabel_2.setBounds(10, 45, 481, 30);
//PANEL WITH ALL THE STUFF
JPanel componentPanel = new JPanel();
componentPanel.setLayout(new GridLayout(3, 1));
componentPanel.setMaximumSize(new Dimension(50,50));
componentPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
componentPanel.setBackground(Color.gray);
componentPanel.add(dateLabel);
componentPanel.add(dateLabel_1);
componentPanel.add(dateLabel_2);
viewportPanel.add(componentPanel); //add panel with labels to viewportpanel
counter++;
}
if(e.getSource() == remButton) {
Component[] componentList = viewportPanel.getComponents();
int lastElement = (componentList.length);
viewportPanel.remove(--lastElement);
--counter;
}
viewportPanel.revalidate();
viewportPanel.repaint();
}
}
Some help would be amazing!
First off, never do this:
setLayout(null);
Next, if you want things compressed at the top of a container, then use a layout that does this. such as a BorderLayout with the compressed items placed into a JPanel (perhaps one that uses a GridLayout) that is placed BorderLayout.PAGE_START
Actually, it looks as if your best solution is to us a JList, one that uses a custom renderer that places your time JLabel and two text JLabels into a JPanel and displays this in the list. So let's explore that.
First create a class to hold the data that is displayed by the JList, which looks to be a date and two lines of text:
public class ListItem {
private LocalDate date;
private String text1;
private String text2;
public ListItem(LocalDate date, String text1, String text2) {
super();
this.date = date;
this.text1 = text1;
this.text2 = text2;
}
public LocalDate getDate() {
return date;
}
public String getText1() {
return text1;
}
public String getText2() {
return text2;
}
}
Then let's create a renderer that a JList can use to display the above information in a JPanel. This is more complicated and requires that the class create a JPanel that places the labels where we want them, perhaps using a GridLayout with 1 column and 3 rows, plus some gaps between the rows: setLayout(new GridLayout(3, 1, 2 * gap, 2 * gap));. The class must implement the ListCellRenderer<T> interface which has one method: public Component getListCellRendererComponent(...). Java will pass in the paramters into this method, including a ListItem value, and we will use that value to fill in the JLabels that we add into this JPanel. Edited to highlight selected items.
import java.awt.*;
import java.time.format.DateTimeFormatter;
import javax.swing.*;
import javax.swing.border.Border;
#SuppressWarnings("serial")
public class ListItemRenderer extends JPanel implements ListCellRenderer<ListItem> {
private static final Color LIGHT_BLUE = new Color(204, 255, 255);
private static final Color REDDISH_GREY = new Color(205, 126, 121);
private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
private static final int GAP = 2;
private Border emptyBorder = BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP);
private Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);
private Border redBorder = BorderFactory.createLineBorder(REDDISH_GREY, 2);
private JLabel dateLabel = new JLabel();
private JLabel text1Label = new JLabel();
private JLabel text2Label = new JLabel();
public ListItemRenderer() {
dateLabel.setFont(dateLabel.getFont().deriveFont(Font.BOLD, 14f));
text1Label.setFont(text1Label.getFont().deriveFont(Font.BOLD));
text2Label.setFont(text1Label.getFont().deriveFont(Font.ITALIC));
int gap = 2;
setLayout(new GridLayout(0, 1, 2 * gap, 2 * gap));
setBorder(BorderFactory.createCompoundBorder(emptyBorder, blackBorder));
add(dateLabel);
add(text1Label);
add(text2Label);
}
#Override
public Component getListCellRendererComponent(JList<? extends ListItem> list, ListItem value, int index,
boolean isSelected, boolean cellHasFocus) {
if (value != null) {
String dateText = dateFormatter.format(value.getDate());
dateLabel.setText(dateText);
text1Label.setText(value.getText1());
text2Label.setText(value.getText2());
} else {
dateLabel.setText("");
text1Label.setText("");
text2Label.setText("");
}
if (isSelected ) {
setBackground(LIGHT_BLUE);
setBorder(BorderFactory.createCompoundBorder(emptyBorder, redBorder));
} else {
setBackground(null);
setBorder(BorderFactory.createCompoundBorder(emptyBorder, blackBorder));
}
return this;
}
}
And now the main program that puts this all together, edited to show removing items:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Random;
import javax.swing.*;
#SuppressWarnings("serial")
public class MainPanel extends JPanel {
DefaultListModel<ListItem> listModel = new DefaultListModel<>();
private JList<ListItem> jList = new JList<>(listModel);
private JScrollPane scrollPane = new JScrollPane(jList);
private JButton addButton = new JButton("Add");
private JButton removeButton = new JButton("Remove");
public MainPanel() {
jList.setPrototypeCellValue(new ListItem(LocalDate.now(),
"This is text 1 for testing. This is text 1 for testing. This is text 1 for testing",
"This is text 2 for testing. This is text 2 for testing. This is text 2 for testing"));
jList.setVisibleRowCount(4);
jList.setCellRenderer(new ListItemRenderer());
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 3, 3));
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
addButton.setMnemonic(KeyEvent.VK_A);
removeButton.setMnemonic(KeyEvent.VK_R);
addButton.addActionListener(e -> addEvent());
removeButton.addActionListener(e -> removeEvent());
JPanel rightPanel = new JPanel();
rightPanel.add(buttonPanel);
int gap = 5;
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
setLayout(new BorderLayout());
add(scrollPane);
add(rightPanel, BorderLayout.LINE_END);
}
private void removeEvent() {
int[] selectedIndices = jList.getSelectedIndices();
for (int i = selectedIndices.length - 1; i >= 0; i--) {
listModel.remove(selectedIndices[i]);
}
}
private void addEvent() {
// this adds random stuff to the JList
String text1 = "Some random text: " + randomText();
String text2 = "Some random text: " + randomText();
listModel.addElement(new ListItem(LocalDate.now(), text1, text2));
// TODO: change this so that it adds *real* data to the JList
}
private String randomText() {
Random random = new Random();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 2 + random.nextInt(3); i++) {
for (int j = 0; j < 3 + random.nextInt(5); j++) {
char c = (char) ('a' + random.nextInt('z' - 'a'));
builder.append(c);
}
builder.append(" ");
}
return builder.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Output would look like:

Changing JTable header height

Happy new year everyone, i've a problem chaning the height of a JTable header, I appreciate if someone can help. The Method I'am using is changing also the backgroundcolor etc.
Thanks
public static void ChangeJTableBackgroundColor(JTable InTable){
JTable mytable = InTable;
Color mycolor = new Color(248, 201, 171);
mytable.setOpaque(true);
mytable.setFillsViewportHeight(true);
mytable.setBackground(mycolor);
Color mycolorhead = new Color(249, 168, 117);
mytable.getTableHeader().setBackground(mycolorhead);
mytable.getTableHeader().setPreferredSize(new Dimension(1,50));
}
There are lots of ways you "might" increase the height of the header, which you choose will depend on what you want to achieve. One thing to keep in mind though, is trying to find a solution which respects the diverse rendering environments which you program might need to run in.
You could...
Simple change the font size. This might sound silly, but you'd be surprised at how simple it really is, for example...
DefaultTableModel model = new DefaultTableModel(10, 10);
JTable table = new JTable(model);
JTableHeader header = table.getTableHeader();
header.setFont(header.getFont().deriveFont(30f));
You could...
Take advantage of Swing's inbuilt HTML support. This example sets up a HTML table with a defined cell height
DefaultTableModel model = new DefaultTableModel(10, 10);
JTable table = new JTable(model);
TableColumnModel columnModel = table.getColumnModel();
String prefix = "<html><body><table><tr><td height=100>";
String suffix = "</td></tr></table></body><html>";
for (int col = 0; col < columnModel.getColumnCount(); col++) {
TableColumn column = columnModel.getColumn(col);
String text = prefix + Character.toString((char)('A' + col)) + suffix;
System.out.println(text);
column.setHeaderValue(text);
}
You could...
Just supply your own TableCellRenderer as the default cell renderer for the table header. This is a little tricky, as it's difficult to mimic the default renderer used by the current look and feel and the UIManager doesn't help. Instead, you need to consider using a "proxy" approach, where by you apply the changes you need to the existing header renderer instead.
DefaultTableModel model = new DefaultTableModel(10, 10);
JTable table = new JTable(model);
JTableHeader header = table.getTableHeader();
TableCellRenderer proxy = header.getDefaultRenderer();
header.setDefaultRenderer(new TableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component comp = proxy.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (comp instanceof JLabel) {
JLabel label = (JLabel) comp;
label.setBorder(new CompoundBorder(label.getBorder(), new EmptyBorder(50, 0, 50, 0)));
}
return comp;
}
});
As far as solutions go, this is probably my preferred, as it takes into account more of the variables involved in determine the preferred size of the column header itself
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.table.*;
public class TableHeaderHeightTest {
private static int HEADER_HEIGHT = 32;
private JTable makeTable() {
JTable table = new JTable(new DefaultTableModel(2, 20));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return table;
}
public JComponent makeUI() {
JPanel p = new JPanel(new GridLayout(2,1));
JTable table1 = makeTable();
//Bad: >>>>
JTableHeader header = table1.getTableHeader();
//Dimension d = header.getPreferredSize();
//d.height = HEADER_HEIGHT;
//header.setPreferredSize(d); //addColumn case test
header.setPreferredSize(new Dimension(100, HEADER_HEIGHT));
p.add(makeTitledPanel("Bad: JTableHeader#setPreferredSize(...)", new JScrollPane(table1)));
//<<<<
JTable table2 = makeTable();
JScrollPane scroll = new JScrollPane(table2);
scroll.setColumnHeader(new JViewport() {
#Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = HEADER_HEIGHT;
return d;
}
});
// //or
// table2.setTableHeader(new JTableHeader(table2.getColumnModel()) {
// #Override public Dimension getPreferredSize() {
// Dimension d = super.getPreferredSize();
// d.height = HEADER_HEIGHT;
// return d;
// }
// });
p.add(makeTitledPanel("Override getPreferredSize()", scroll));
final List<JTable> list = Arrays.asList(table1, table2);
JPanel panel = new JPanel(new BorderLayout());
panel.add(p);
panel.add(new JButton(new AbstractAction("addColumn") {
#Override public void actionPerformed(ActionEvent e) {
for(JTable t: list) {
t.getColumnModel().addColumn(new TableColumn());
JTableHeader h = t.getTableHeader();
Dimension d = h.getPreferredSize();
System.out.println(d);
}
}
}), BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
return panel;
}
private static JComponent makeTitledPanel(String title, JComponent c) {
JPanel p = new JPanel(new BorderLayout());
p.add(c);
p.setBorder(BorderFactory.createTitledBorder(title));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TableHeaderHeightTest().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
Try this

How to do getValueAt() when using an AbstractTableModel

I'm trying to make an program that shows all files in the temerary folder using an AbstractTableModel but how do i code the getValueAt() method.
The names of the files should be in the first column and the file path should be in the second column.
I nulled it for now but can someone show me hoe i should code it?
This is what i got so far:
public class UI extends JFrame {
private JPanel contentPane;
private JTable table;
File[] dir = new File(System.getProperty("java.io.tmpdir")).listFiles();
public static void main(String[] args) {
public UI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 542, 422);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
tabbedPane.setBounds(0, 0, 526, 384);
contentPane.add(tabbedPane);
JPanel panel = new JPanel();
tabbedPane.addTab("Temp Files", null, panel, null);
panel.setLayout(null);
final AbstractTableModel myAbstractTableModel = new AbstractTableModel() {
#Override
public int getColumnCount() {
return 2;
}
#Override
public int getRowCount() {
return dir.length;
}
#Override
public Object getValueAt(int arg0, int arg1) {
return null;
}
};
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 402, 334);
panel.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
table.setModel(myAbstractTableModel);
}
}
In your example, arg0 is the row and arg1 is the col, so dir[arg0] is the File for each row. In your implementation of getValueAt(), return file.getName() or file.getPath() as indicated by the col value. EnvTableTest is a related example that uses the more descriptive parameter names.

How to add JButton after a JTable

I have a JTable with some data like this SSCCE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.*;
import java.awt.Color;
class kanji{
public static void main(String args[]){
JFrame frame = new JFrame("Kanji");
JPanel pane = new JPanel();
JTable table = new JTable();
pane.setLayout(new BorderLayout());
JButton agreg = new JButton("Agregar");
DefaultTableModel model = new DefaultTableModel(get_data(), get_header());
JFrame hk = new JFrame("Historial de Significados");
Image icon = Toolkit.getDefaultToolkit().getImage("JLPT.jpg");
ImageIcon ima = new ImageIcon("JLPT.jpg");
table = new JTable(model){
#Override
public boolean isCellEditable(int row, int col){
switch(col){
case 0:
return false;
case 1:
return false;
case 2:
return true;
default:
return false;
}
}
#Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Boolean.class;
default:
return Boolean.class;
}
}
};
DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setForeground(Color.blue);
setHorizontalAlignment(JLabel.CENTER);
setFont(new Font("Microsoft JhengHei", Font.BOLD, 50));
return this;
}
};
DefaultTableCellRenderer r2 = new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setHorizontalAlignment(JLabel.LEFT);
setFont(new Font("Microsoft JhengHei", Font.BOLD, 13));
return this;
}
};
table.getColumnModel().getColumn(0).setCellRenderer(r);
table.getColumnModel().getColumn(1).setCellRenderer(r2);
TableColumn column = null;
for (int i = 0; i < 3; i++) {
column = table.getColumnModel().getColumn(i);
if (i==0) {
column.setMaxWidth(80);
column.setMinWidth(80);
}
else{
if(i==1){
column.setPreferredWidth(470);
}
else{
column.setMaxWidth(50);
column.setMinWidth(50);
}
}
}
table.setRowHeight(table.getRowHeight()+70);
table.setModel(model);
table.getTableHeader().setReorderingAllowed(false);
JScrollPane scroll = new JScrollPane(table);
pane.add(scroll, BorderLayout.CENTER);
pane.add(agreg, BorderLayout.SOUTH);
frame.add(pane);
frame.setTitle("Historial de Significados");
frame.setSize(1350, 700);
frame.setIconImage(icon);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scroll.setViewportView(table);
}
public static Object [][]get_data(){
Object data[][] = new Object[][]{
{"\u4e00", "Uno, 1", true},
{"\u4e01", "Uno, 1", true},
{"\u4e02", "Uno, 1", true},
{"\u4e03", "Uno, 1", true},
{"\u4e04", "Uno, 1", true},
{"\u4e05", "Uno, 1", true},
{"\u4e06", "Uno, 1", true},
{"\u4e07", "Uno, 1", true},
{"\u4e08", "Uno, 1", true},
{"\u4e09", "Uno, 1", true}
};
return data;
}
public static String []get_header(){
String header [] = new String[]{"KANJI", "SIGNIFICADO", "Agregar"};
return header;
}
}
But I want to add 3 JButton after JTable is created, I want to have a JTable that don't take all JFrame size, in my code I get a Button but I want to have the possibility to set buttons bounds.
I've tried adding a JPanel and with another try with "setPreferredSize" where JScrollPane don't work, I mean I doesn't appear so I can't see all table content; and I also tried with "setSize" that doesn't work for me.
Maybe I have to make a second panel or something like that, I hope you can help me solving this issue.
I guess an Image says more than 1,000 words, so:
This is what I get and have and what I want to have
So in plain english, all I want to know is how to render table and add button at the bottom after rendered table (but I don't want buttons to have all the frame width, I want them away from table, not together as I have them and with the possibility to change their size and position).
private void createUI() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
final JTable table = new JTable(10, 4);
JPanel btnPnl = new JPanel(new BorderLayout());
JPanel topBtnPnl = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JPanel bottombtnPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
topBtnPnl.add(new JButton("Select All"));
bottombtnPnl.add(new JButton("Cancel"));
bottombtnPnl.add(new JButton("Add Selected"));
btnPnl.add(topBtnPnl, BorderLayout.NORTH);
btnPnl.add(bottombtnPnl, BorderLayout.CENTER);
table.getTableHeader().setReorderingAllowed(false);
frame.add(table.getTableHeader(), BorderLayout.NORTH);
frame.add(table, BorderLayout.CENTER);
frame.add(btnPnl, BorderLayout.SOUTH);
frame.setTitle("JTable Example.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
CheckABunch is an example that shows how to update the TableModel based on the current selection.
You could take a look at GridLayout.
In short I've made some changes to your class, and I've got this:
First group all JButtons into a JPanel, which have a GridLayout inside
Also, your JFrame container must have a BorderLayout
So you need add each button to the auxiliary pane and at the end add both JPanels to the frame.
Here's the code:
//Another import sentences
import java.awt.GridLayout;
class kanji{
public static void main(String args[]){
// More code goes here
JPanel allowedOperations = new JPanel(new GridLayout(2, 2));
pane.setLayout(new BorderLayout());
// More code goes here
// All buttons are grouped here
allowedOperations.add(new JButton()); // Here's a trick
allowedOperations.add(agreg);
allowedOperations.add(new JButton("Save selected"));
allowedOperations.add(new JButton("Cancel"));
// Add the pane object to the frame
frame.add(pane, BorderLayout.CENTER);
// And finally add the allowedOperations object to the frame
frame.add(allowedOperations, BorderLayout.SOUTH);
// More code goes here
} //End class
I've just posted the modifications, so you need to group the whole class.
I know it isn't the best approach, but you can improve your code from here. Also note you can create your buttons before added to the allowedOperations pane, as the agreg button

Prevent JComboBox rendering on update JLabel

I'm doing an application that play a video and in a JLabel i'm showing timecode of video. Also have a JCombobox that allows to change subtitles of the video.
That JCombobox has a ListCellRenderer that change default output of each item of combobox.
Problem is that each time the JLabel change its value the JCombobox is rendered again.
I think this is a waste of resources there is some way of change that behavior?.
There is the code relative to the JComboBox. The JLabel is modified by a swing.Timer each second.
JComboBox comboBox = new JComboBox<>();
comboBox.setEditable(false);
DefaultComboBoxModel<File> defaultComboBox = new DefaultComboBoxModel<>();
for (File f : Player.capitulo.getFicheros()) {
if (f.getAbsolutePath().endsWith(".srt")) {
System.out.println(f.getAbsolutePath());
defaultComboBox.addElement(f);
}
}
comboBox.setModel(defaultComboBox);
comboBox.setRenderer(new ListRenderer());
public class ListRenderer implements ListCellRenderer<File> {
protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
#Override
public Component getListCellRendererComponent(JList<? extends File> list,
File value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel renderer = new JLabel();
// JLabel renderer = (JLabel) defaultRenderer
// .getListCellRendererComponent(list, value, index, isSelected,
// cellHasFocus);
if (value != null) {
Path p = value.toPath();
System.out.println(p.getParent());
String language = value.getName().trim().replace(".srt", "");
language = language.substring(language.lastIndexOf(".") + 1,
language.length());
// System.out.println(language.length());
if (language.length() == 3) {
renderer.setText(language);
} else {
renderer.setText("Subtitulo " + (index + 1));
}
}
return renderer;
}
}
UPDATED: Here is an example that reproduce the problem
Ok, here is SSCCE code. Doing the example i noticed that when JLabel es in the same line of JCombobox reproduce the same problem but when is in other line doesn't happen.
public class Example extends JFrame {
private JPanel contentPane;
private JLabel lblNewLabel;
private JComboBox<String> comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example frame = new Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Example() {
initGUI();
}
private void initGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 428, 362);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
lblNewLabel = new JLabel("New label");
contentPane.add(lblNewLabel, BorderLayout.WEST);
Timer t = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Random r = new Random();
lblNewLabel.setText(String.valueOf(r.nextInt()));
}
});
t.start();
comboBox = new JComboBox<>();
comboBox.setRenderer(new ListCellRenderer<String>() {
#Override
public Component getListCellRendererComponent(
JList<? extends String> list, String value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel("");
System.out.println("Pass");
return label;
}
});
contentPane.add(comboBox, BorderLayout.CENTER);
}
}

Categories